Submission #71867085


Source Code Expand

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    private static final long MOD = 998244353L;

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);

        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        long[] A = new long[N];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            A[i] = Long.parseLong(st.nextToken());
        }

        long[] B = new long[M];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < M; i++) {
            B[i] = Long.parseLong(st.nextToken());
        }

        Arrays.sort(A);
        Arrays.sort(B);

        long[] PB = new long[M + 1];
        PB[0] = 0;
        for (int j = 0; j < M; j++) {
            PB[j + 1] = (PB[j] + B[j]) % MOD;
        }
        
        long totalSumB = PB[M];

        long totalSumS = 0;
        
        int k = 0; 
        
        for (int i = 0; i < N; i++) {
            long Ai = A[i];

            while (k < M && B[k] < Ai) {
                k++;
            }
            
            long sumB_less = PB[k];
            long termA_less = (k * Ai) % MOD;
            
            long part1 = (termA_less - sumB_less + MOD) % MOD;

            long sumB_greater = (totalSumB - sumB_less + MOD) % MOD;
            long count_greater = M - k;
            long termA_greater = (count_greater * Ai) % MOD;
            
            long part2 = (sumB_greater - termA_greater + MOD) % MOD;

            long innerSum = (part1 + part2) % MOD;

            totalSumS = (totalSumS + innerSum) % MOD;
        }

        pw.println(totalSumS);
        pw.flush();
    }
}

Submission Info

Submission Time
Task D - Sum of Differences
User addy
Language Java24 (OpenJDK 24.0.2)
Score 400
Code Size 2069 Byte
Status AC
Exec Time 489 ms
Memory 73208 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 32
Set Name Test Cases
Sample 00-sample-01.txt, 00-sample-02.txt
All 00-sample-01.txt, 00-sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt
Case Name Status Exec Time Memory
00-sample-01.txt AC 57 ms 38648 KiB
00-sample-02.txt AC 51 ms 38712 KiB
01-01.txt AC 107 ms 41832 KiB
01-02.txt AC 107 ms 41764 KiB
01-03.txt AC 85 ms 41404 KiB
01-04.txt AC 88 ms 41528 KiB
01-05.txt AC 88 ms 41756 KiB
01-06.txt AC 90 ms 41412 KiB
01-07.txt AC 92 ms 41924 KiB
01-08.txt AC 108 ms 42356 KiB
01-09.txt AC 114 ms 42292 KiB
01-10.txt AC 85 ms 41264 KiB
01-11.txt AC 108 ms 42192 KiB
01-12.txt AC 106 ms 41960 KiB
01-13.txt AC 99 ms 42280 KiB
01-14.txt AC 109 ms 42432 KiB
01-15.txt AC 50 ms 38848 KiB
01-16.txt AC 50 ms 38700 KiB
01-17.txt AC 442 ms 69008 KiB
01-18.txt AC 436 ms 68492 KiB
01-19.txt AC 301 ms 64428 KiB
01-20.txt AC 297 ms 65428 KiB
01-21.txt AC 410 ms 73036 KiB
01-22.txt AC 285 ms 71184 KiB
01-23.txt AC 282 ms 71048 KiB
01-24.txt AC 412 ms 70420 KiB
01-25.txt AC 489 ms 70632 KiB
01-26.txt AC 265 ms 70624 KiB
01-27.txt AC 419 ms 70100 KiB
01-28.txt AC 358 ms 73208 KiB
01-29.txt AC 314 ms 69564 KiB
01-30.txt AC 399 ms 70580 KiB