結果

問題 No.1645 AB's abs
ユーザー ks2m
提出日時 2021-08-13 21:34:55
言語 Java
(openjdk 23)
結果
AC  
実行時間 371 ms / 2,000 ms
コード長 875 bytes
コンパイル時間 3,021 ms
コンパイル使用メモリ 78,948 KB
実行使用メモリ 56,344 KB
最終ジャッジ日時 2024-10-03 17:35:07
合計ジャッジ時間 12,328 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		sc.close();

		int mod = 998244353;
		Map<Integer, Long> map = new HashMap<>();
		map.put(0, 1L);
		for (int i = 0; i < n; i++) {
			Map<Integer, Long> wk = new HashMap<>();
			for (Integer k : map.keySet()) {
				int next = k + a[i];
				wk.put(next, (wk.getOrDefault(next, 0L) + map.get(k)) % mod);
				next = k - a[i];
				wk.put(next, (wk.getOrDefault(next, 0L) + map.get(k)) % mod);
			}
			map = wk;
		}
		long ans = 0;
		for (Integer k : map.keySet()) {
			long val = Math.abs(k) * map.get(k) % mod;
			ans += val;
		}
		ans %= mod;
		System.out.println(ans);
	}
}
0