Submission #1441106


Source Code Expand

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


public class Main {
  public static void main(String[] args) {
    FastScanner sc = new FastScanner();
    int W = sc.nextInt();
    int H = sc.nextInt();

    long[] p = sc.nextLongList(W);
    long[] q = sc.nextLongList(H);
    
    Arrays.sort(p);
    Arrays.sort(q);
    
    int pPtr = 0;
    int qPtr = 0;
    
    long cost = 0;
    while (W > pPtr || H > qPtr) {
      if (W == pPtr) {
        cost += q[qPtr ++];
      } else if (H == qPtr) {
        cost += p[pPtr ++];
      } else if (q[qPtr] <= p[pPtr]) {
        cost += (W - pPtr + 1) * q[qPtr ++];
      } else {
        cost += (H - qPtr + 1) * p[pPtr ++];
      }
    }
    System.out.println(cost);
  }
}


class FastScanner {
  public static String debug = null;

  private final InputStream in = System.in;
  private int ptr = 0;
  private int buflen = 0;
  private byte[] buffer = new byte[1024];
  private boolean eos = false;

  private boolean hasNextByte() {
    if (ptr < buflen) {
      return true;
    } else {
      ptr = 0;
      try {
        if (debug != null) {
          buflen = debug.length();
          buffer = debug.getBytes();
          debug = "";
          eos = true;
        } else {
          buflen = in.read(buffer);
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (buflen < 0) {
        eos = true;
        return false;
      } else if (buflen == 0) {
        return false;
      }
    }
    return true;
  }

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

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

  private void skipUnprintable() {
    while (hasNextByte() && !isPrintableChar(buffer[ptr]))
      ptr++;
  }

  public boolean isEOS() {
    return this.eos;
  }

  public boolean hasNext() {
    skipUnprintable();
    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() {
    return (int) nextLong();
  }

  public long[] nextLongList(int n) {
    return nextLongTable(1, n)[0];
  }

  public int[] nextIntList(int n) {
    return nextIntTable(1, n)[0];
  }

  public long[][] nextLongTable(int n, int m) {
    long[][] ret = new long[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        ret[i][j] = nextLong();
      }
    }
    return ret;
  }

  public int[][] nextIntTable(int n, int m) {
    int[][] ret = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        ret[i][j] = nextInt();
      }
    }
    return ret;
  }
}

Submission Info

Submission Time
Task C - Gr-idian MST
User hiromi_ayase
Language Java8 (OpenJDK 1.8.0)
Score 500
Code Size 3688 Byte
Status AC
Exec Time 228 ms
Memory 26768 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 30
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt AC 164 ms 24232 KB
02.txt AC 169 ms 22952 KB
03.txt AC 159 ms 24680 KB
04.txt AC 181 ms 24016 KB
05.txt AC 184 ms 26312 KB
06.txt AC 198 ms 23820 KB
07.txt AC 160 ms 26768 KB
08.txt AC 163 ms 24764 KB
09.txt AC 228 ms 23576 KB
10.txt AC 160 ms 23484 KB
11.txt AC 189 ms 24936 KB
12.txt AC 149 ms 25128 KB
13.txt AC 122 ms 23288 KB
14.txt AC 165 ms 25732 KB
15.txt AC 159 ms 26444 KB
16.txt AC 126 ms 24868 KB
17.txt AC 185 ms 24104 KB
18.txt AC 166 ms 21688 KB
19.txt AC 141 ms 18272 KB
20.txt AC 115 ms 21572 KB
21.txt AC 183 ms 26688 KB
22.txt AC 142 ms 24740 KB
23.txt AC 121 ms 24260 KB
24.txt AC 123 ms 24472 KB
25.txt AC 68 ms 19156 KB
26.txt AC 67 ms 17748 KB
27.txt AC 68 ms 21332 KB
28.txt AC 68 ms 19668 KB
s1.txt AC 68 ms 18516 KB
s2.txt AC 68 ms 19668 KB