Submission #5492424


Source Code Expand

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    }

    static class Task {
    	int N;
    	long ans = 0;
    	long[] A, B;
    	
        void solve(InputReader in, PrintWriter out) {
            // write your code here
        	N = in.nextInt();
        	A = new long[N];
        	B = new long[N];
        	for (int i = 0; i < N; ++i) {
        		A[i] = in.nextLong();
        		B[i] = in.nextLong();
        	}
        	
        	for (int i = N - 1; i >= 0; --i) {
        		long mul = (long) Math.ceil((A[i] + ans) / (double)B[i]);
        		ans += B[i]*mul - (A[i] + ans);
        	}
        	
        	out.print(ans);
        }
    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int size) {
            int[] arr = new int[size];
            for (int i = 0; i < size; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        long[] nextLongArray(int size) {
            long[] arr = new long[size];
            for (int i = 0; i < size; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }
    }
}

Submission Info

Submission Time
Task A - Multiple Array
User hatsujime
Language Java8 (OpenJDK 1.8.0)
Score 300
Code Size 2460 Byte
Status AC
Exec Time 236 ms
Memory 45048 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 234 ms 41496 KB
02.txt AC 226 ms 37664 KB
03.txt AC 223 ms 42744 KB
04.txt AC 224 ms 44576 KB
05.txt AC 232 ms 38108 KB
06.txt AC 229 ms 45048 KB
07.txt AC 213 ms 39876 KB
08.txt AC 227 ms 40576 KB
09.txt AC 215 ms 40472 KB
10.txt AC 236 ms 42628 KB
11.txt AC 217 ms 38384 KB
12.txt AC 181 ms 38484 KB
13.txt AC 228 ms 42796 KB
14.txt AC 231 ms 41688 KB
15.txt AC 70 ms 18516 KB
16.txt AC 70 ms 19664 KB
s1.txt AC 71 ms 21460 KB
s2.txt AC 70 ms 21460 KB