Submission #1338214


Source Code Expand

//Do what you can't.

import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
  static long mod = (long)1e9 + 7;
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    PrintWriter w = new PrintWriter(System.out);
    int n = in.nextInt();
    long[] a = new long[n], b = new long[n];
    for (int i = 0; i < n; i++) {
      a[i] = in.nextLong();
      b[i] = in.nextLong();
    }
    long c = 0;
    long[] sum = new long[n + 1];
    for (int i = n - 1; i > -1; i--) {
      a[i] = a[i] + sum[i + 1];
      long temp = (long)Math.ceil(a[i]*1.0 / b[i]) * b[i] - a[i];
      c = c + temp;
      sum[i] = sum[i + 1] + temp;
    }
    w.println(c);
    w.close();
  }
  static String rev(String s) {
    StringBuilder b = new StringBuilder(s);
    return b.reverse().toString();
  }
  static class InputReader {

    private final InputStream stream;
    private final byte[] buf = new byte[8192];
    private int curChar, snumChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
      this.stream = stream;
    }

    public int snext() {
      if (snumChars == -1)
        throw new InputMismatchException();
      if (curChar >= snumChars) {
        curChar = 0;
        try {
          snumChars = stream.read(buf);
        } catch (IOException e) {
          throw new InputMismatchException();
        }
        if (snumChars <= 0)
          return -1;
      }
      return buf[curChar++];
    }

    public int nextInt() {
      int c = snext();
      while (isSpaceChar(c)) {
        c = snext();
      }
      int sgn = 1;
      if (c == '-') {
        sgn = -1;
        c = snext();
      }
      int res = 0;
      do {
        if (c < '0' || c > '9')
          throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = snext();
      } while (!isSpaceChar(c));
      return res * sgn;
    }

    public long nextLong() {
      int c = snext();
      while (isSpaceChar(c)) {
        c = snext();
      }
      int sgn = 1;
      if (c == '-') {
        sgn = -1;
        c = snext();
      }
      long res = 0;
      do {
        if (c < '0' || c > '9')
          throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = snext();
      } while (!isSpaceChar(c));
      return res * sgn;
    }

    public int[] nextIntArray(int n) {
      int a[] = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = nextInt();
      }
      return a;
    }

    public String readString() {
      int c = snext();
      while (isSpaceChar(c)) {
        c = snext();
      }
      StringBuilder res = new StringBuilder();
      do {
        res.appendCodePoint(c);
        c = snext();
      } while (!isSpaceChar(c));
      return res.toString();
    }

    public String nextLine() {
      int c = snext();
      while (isSpaceChar(c))
        c = snext();
      StringBuilder res = new StringBuilder();
      do {
        res.appendCodePoint(c);
        c = snext();
      } while (!isEndOfLine(c));
      return res.toString();
    }

    public boolean isSpaceChar(int c) {
      if (filter != null)
        return filter.isSpaceChar(c);
      return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    private boolean isEndOfLine(int c) {
      return c == '\n' || c == '\r' || c == -1;
    }

    public interface SpaceCharFilter {
      public boolean isSpaceChar(int ch);
    }
  }
}

Submission Info

Submission Time
Task A - Multiple Array
User ashubeckham
Language Java8 (OpenJDK 1.8.0)
Score 300
Code Size 3639 Byte
Status AC
Exec Time 117 ms
Memory 26036 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 2
AC × 18
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, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt AC 110 ms 21972 KB
02.txt AC 113 ms 24116 KB
03.txt AC 117 ms 24116 KB
04.txt AC 114 ms 21812 KB
05.txt AC 116 ms 23348 KB
06.txt AC 114 ms 23860 KB
07.txt AC 107 ms 22084 KB
08.txt AC 113 ms 26036 KB
09.txt AC 105 ms 23892 KB
10.txt AC 117 ms 23988 KB
11.txt AC 109 ms 23380 KB
12.txt AC 103 ms 19908 KB
13.txt AC 108 ms 24020 KB
14.txt AC 113 ms 21700 KB
15.txt AC 68 ms 21204 KB
16.txt AC 68 ms 15828 KB
s1.txt AC 69 ms 21076 KB
s2.txt AC 69 ms 17108 KB