結果

問題 No.885 アマリクエリ
ユーザー tentententen
提出日時 2023-04-10 18:19:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,362 ms / 2,000 ms
コード長 2,294 bytes
コンパイル時間 2,859 ms
コンパイル使用メモリ 96,776 KB
実行使用メモリ 69,032 KB
最終ジャッジ日時 2024-04-15 22:28:16
合計ジャッジ時間 11,614 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 328 ms
47,136 KB
testcase_01 AC 1,199 ms
67,692 KB
testcase_02 AC 693 ms
57,904 KB
testcase_03 AC 338 ms
49,080 KB
testcase_04 AC 394 ms
50,536 KB
testcase_05 AC 343 ms
48,312 KB
testcase_06 AC 259 ms
45,388 KB
testcase_07 AC 351 ms
51,288 KB
testcase_08 AC 261 ms
45,276 KB
testcase_09 AC 1,362 ms
69,032 KB
testcase_10 AC 54 ms
37,112 KB
testcase_11 AC 54 ms
36,992 KB
testcase_12 AC 54 ms
37,296 KB
testcase_13 AC 134 ms
41,040 KB
testcase_14 AC 125 ms
41,220 KB
testcase_15 AC 126 ms
41,116 KB
testcase_16 AC 86 ms
38,036 KB
testcase_17 AC 96 ms
38,440 KB
testcase_18 AC 117 ms
39,164 KB
testcase_19 AC 92 ms
38,428 KB
testcase_20 AC 81 ms
37,908 KB
testcase_21 AC 102 ms
38,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        TreeMap<Integer, Long> counts = new TreeMap<>();
        long total = 0;
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            total += x;
            counts.put(x, counts.getOrDefault(x, 0L) + 1);
        }
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            int x = sc.nextInt();
            while (counts.lastKey() >= x) {
                int key = counts.lastKey();
                long value = counts.lastEntry().getValue();
                total -= key * value;
                counts.remove(key);
                key %= x;
                total += key * value;
                counts.put(key, counts.getOrDefault(key, 0L) + value);
            }
            sb.append(total).append("\n");
        }
        System.out.print(sb);
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0