Submission #63527019


Source Code Expand

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;

class Main {
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();

        int N = sc.nextInt();
        int M = sc.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = sc.nextInt();
        }
        int[] B = new int[M];
        for (int i = 0; i < M; i++) {
            B[i] = sc.nextInt();
        }

        int indexA = N-1;
        int indexB = M-1;
        int CA = 0;
        int CB = 0;
        Arrays.sort(A);
        Arrays.sort(B);
        long ans = 0;
        while (indexA >= 0 && A[indexA] > 0){
            ans += A[indexA];
            indexA--;
            CA++;
        }
        // System.out.println(indexA + " " + ans);
        while (true){
            if (indexB >= 0 && B[indexB] > 0 && CA > CB){
                ans += B[indexB];
                indexB--;
                CB++;
            }else if (indexA >= 0 && indexB >= 0 && A[indexA] + B[indexB] > 0){
                ans += A[indexA] + B[indexB];
                indexA--;
                indexB--;
                CB++;
            }else {
                break;
            }

            // System.out.println(ans);
        }

        System.out.println(ans);

        MyWriter.flush();
    }

    private static class MyWriter{
        static StringBuilder sb = new StringBuilder();
        protected static void flush(){
            System.out.print(sb);
            sb = new StringBuilder();
        }
        protected static void println(){
            sb.append("\n");
        }
        protected static void println(int i){
            sb.append(i).append("\n");
        }
        protected static void println(long l) {
            sb.append(l).append("\n");
        }
        protected static void println(double d) {
            sb.append(d).append("\n");
        }
        protected static void println(float f) {
            sb.append(f).append("\n");
        }
        protected static void println(char c) {
            sb.append(c).append("\n");
        }
        protected static void println(boolean b) {
            sb.append(b).append("\n");
        }
        protected static void println(Object obj) {
            String s = String.valueOf(obj);
            sb.append(s).append("\n");
        }
        protected static void print(int i){
            sb.append(i);
        }
        protected static void print(long l) {
            sb.append(l);
        }
        protected static void print(double d) {
            sb.append(d);
        }
        protected static void print(float f) {
            sb.append(f);
        }
        protected static void print(char c) {
            sb.append(c);
        }
        protected static void print(boolean b) {
            sb.append(b);
        }
        protected static void print(Object obj) {
            String s = String.valueOf(obj);
            sb.append(s);
        }

        protected static void printInv(long a,long b,long MOD){
            a %= MOD;
            b %= MOD;
            println((a * invGcd(b,MOD)[1])%MOD);
        }
        private static long safeMod(long x, long m){
            x %= m;
            if(x<0) x += m;
            return x;
        }
        private static long[] invGcd(long a, long b){
            a = safeMod(a, b);
            if(a==0) return new long[]{b,0};

            long s=b, t=a;
            long m0=0, m1=1;
            while(t>0){
                long u = s/t;
                s -= t*u;
                m0 -= m1*u;
                long tmp = s; s = t; t = tmp;
                tmp = m0; m0 = m1; m1 = tmp;
            }
            if(m0<0) m0 += b/s;
            return new long[]{s,m0};
        }
        public static long writerGcd(long... a){
            if(a.length == 0) return 0;
            long r = Math.abs(a[0]);
            for(int i=1; i<a.length; i++){
                if(a[i]!=0) {
                    if(r==0) r = Math.abs(a[i]);
                    else r = invGcd(r, Math.abs(a[i]))[0];
                }
            }
            return r;
        }
    }

    private static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;

        private boolean hasNextByte() {
            if (ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return buflen > 0;
            }
        }

        private int readByte() {
            if (hasNextByte()) return buffer[ptr++];
            else return -1;
        }

        private static boolean isPrintableChar(int c) {
            return 33 <= c && c <= 126;
        }

        public boolean hasNext() {
            while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
            return !hasNextByte();
        }

        public String next() {
            if (hasNext()) throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public long nextLong() {
            if (hasNext()) throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) {
                throw new NumberFormatException();
            }
            while (true) {
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                } else if (b == -1 || !isPrintableChar(b)) {
                    return minus ? -n : n;
                } else {
                    throw new NumberFormatException();
                }
                b = readByte();
            }
        }

        public int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
            return (int) nl;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}

Submission Info

Submission Time
Task C - Buy Balls
User AAH_tomato
Language Java (OpenJDK 17)
Score 300
Code Size 6831 Byte
Status AC
Exec Time 386 ms
Memory 67344 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 49
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 01_test_29.txt, 01_test_30.txt, 01_test_31.txt, 01_test_32.txt, 01_test_33.txt, 01_test_34.txt, 01_test_35.txt, 01_test_36.txt, 01_test_37.txt, 01_test_38.txt, 01_test_39.txt, 01_test_40.txt, 01_test_41.txt, 01_test_42.txt, 01_test_43.txt, 01_test_44.txt, 01_test_45.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 41 ms 34340 KiB
00_sample_01.txt AC 42 ms 34352 KiB
00_sample_02.txt AC 42 ms 34232 KiB
01_test_00.txt AC 41 ms 34344 KiB
01_test_01.txt AC 42 ms 34344 KiB
01_test_02.txt AC 42 ms 34552 KiB
01_test_03.txt AC 136 ms 41548 KiB
01_test_04.txt AC 116 ms 37632 KiB
01_test_05.txt AC 111 ms 37376 KiB
01_test_06.txt AC 201 ms 43164 KiB
01_test_07.txt AC 194 ms 49732 KiB
01_test_08.txt AC 192 ms 49760 KiB
01_test_09.txt AC 310 ms 41636 KiB
01_test_10.txt AC 329 ms 56748 KiB
01_test_11.txt AC 331 ms 40928 KiB
01_test_12.txt AC 196 ms 49844 KiB
01_test_13.txt AC 386 ms 67344 KiB
01_test_14.txt AC 317 ms 50648 KiB
01_test_15.txt AC 294 ms 50184 KiB
01_test_16.txt AC 194 ms 48196 KiB
01_test_17.txt AC 277 ms 50276 KiB
01_test_18.txt AC 332 ms 50648 KiB
01_test_19.txt AC 180 ms 42320 KiB
01_test_20.txt AC 323 ms 56728 KiB
01_test_21.txt AC 177 ms 50232 KiB
01_test_22.txt AC 194 ms 48124 KiB
01_test_23.txt AC 279 ms 49924 KiB
01_test_24.txt AC 191 ms 40740 KiB
01_test_25.txt AC 168 ms 44688 KiB
01_test_26.txt AC 311 ms 41424 KiB
01_test_27.txt AC 196 ms 42332 KiB
01_test_28.txt AC 219 ms 48516 KiB
01_test_29.txt AC 176 ms 42440 KiB
01_test_30.txt AC 216 ms 43112 KiB
01_test_31.txt AC 251 ms 40976 KiB
01_test_32.txt AC 307 ms 50560 KiB
01_test_33.txt AC 275 ms 47300 KiB
01_test_34.txt AC 311 ms 41560 KiB
01_test_35.txt AC 181 ms 42580 KiB
01_test_36.txt AC 213 ms 43172 KiB
01_test_37.txt AC 223 ms 40904 KiB
01_test_38.txt AC 326 ms 65752 KiB
01_test_39.txt AC 177 ms 42332 KiB
01_test_40.txt AC 220 ms 52212 KiB
01_test_41.txt AC 142 ms 40868 KiB
01_test_42.txt AC 182 ms 42912 KiB
01_test_43.txt AC 41 ms 34260 KiB
01_test_44.txt AC 143 ms 38256 KiB
01_test_45.txt AC 134 ms 41268 KiB