Submission #994571


Source Code Expand

import java.io.*;
import java.util.*;

public class Main {
	FastScanner in;
	PrintWriter out;

	int[] a;
	long[][] dp;
	long[] sum;

	long getAns(int pos, int value) {
		if (pos == 1) {
			return value;
		}
		if (value <= a[pos]) {
			return dp[pos][value];
		}
		int l = 1, r = pos;
		while (r - l > 1) {
			int mid = (l + r) >> 1;
			long su = sum[pos] - sum[mid - 1];
			if (su < value) {
				r = mid;
			} else {
				l = mid;
			}
		}
		return getAns(l, (int) (value - (sum[pos] - sum[l])));
	}

	void solve() {
		int n = in.nextInt();
		a = new int[n];
		for (int i = 0; i + 1 < n; i++) {
			a[i] = in.nextInt();
		}
		sum = new long[n];
		sum[0] = a[0];
		for (int i = 1; i < n; i++) {
			sum[i] = sum[i - 1] + a[i];
		}
		int m = in.nextInt();
		dp = new long[n][];
		for (int x = 2; x + 1 < n; x++) {
			dp[x] = new long[a[x] + 1];
			for (int y = 0; y <= a[x]; y++) {
				dp[x][y] = getAns(x - 1, a[x] - y);
			}
		}
		for (int i = 0; i < m; i++) {
			int value = in.nextInt();
			out.println(getAns(n - 2, value) + a[0] - a[1]);
		}
	}

	void run() {
		try {
			in = new FastScanner(new File("object.in"));
			out = new PrintWriter(new File("object.out"));

			solve();

			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	void runIO() {

		in = new FastScanner(System.in);
		out = new PrintWriter(System.out);

		solve();

		out.close();
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		public FastScanner(InputStream f) {
			br = new BufferedReader(new InputStreamReader(f));
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				String s = null;
				try {
					s = br.readLine();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (s == null)
					return null;
				st = new StringTokenizer(s);
			}
			return st.nextToken();
		}

		boolean hasMoreTokens() {
			while (st == null || !st.hasMoreTokens()) {
				String s = null;
				try {
					s = br.readLine();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (s == null)
					return false;
				st = new StringTokenizer(s);
			}
			return true;
		}

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

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

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

	public static void main(String[] args) {
		new Thread(null, new Runnable() {
			public void run() {
				new Main().runIO();
			}
		}, "1", 1 << 26).start();
	}
}

Submission Info

Submission Time
Task H - Tokaido
User qwerty787788
Language Java8 (OpenJDK 1.8.0)
Score 1600
Code Size 2784 Byte
Status AC
Exec Time 640 ms
Memory 62672 KB

Judge Result

Set Name sample dataset1 dataset2
Score / Max Score 0 / 0 700 / 700 900 / 900
Status
AC × 2
AC × 20
AC × 37
Set Name Test Cases
sample sample-01.txt, sample-02.txt
dataset1 sample-01.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
dataset2 sample-01.txt, 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, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, 02-15.txt, 02-16.txt
Case Name Status Exec Time Memory
01-01.txt AC 354 ms 29364 KB
01-02.txt AC 101 ms 8432 KB
01-03.txt AC 145 ms 17616 KB
01-04.txt AC 257 ms 34956 KB
01-05.txt AC 312 ms 27340 KB
01-06.txt AC 256 ms 27628 KB
01-07.txt AC 227 ms 35236 KB
01-08.txt AC 328 ms 27336 KB
01-09.txt AC 279 ms 27276 KB
01-10.txt AC 148 ms 17360 KB
01-11.txt AC 242 ms 22644 KB
01-12.txt AC 294 ms 26724 KB
01-13.txt AC 299 ms 28208 KB
01-14.txt AC 290 ms 26992 KB
01-15.txt AC 328 ms 27368 KB
01-16.txt AC 284 ms 28192 KB
01-17.txt AC 243 ms 27604 KB
01-18.txt AC 224 ms 26688 KB
01-19.txt AC 239 ms 26620 KB
02-01.txt AC 293 ms 39624 KB
02-02.txt AC 504 ms 62672 KB
02-03.txt AC 554 ms 57796 KB
02-04.txt AC 552 ms 57656 KB
02-05.txt AC 388 ms 39128 KB
02-06.txt AC 548 ms 57452 KB
02-07.txt AC 533 ms 58272 KB
02-08.txt AC 386 ms 39520 KB
02-09.txt AC 404 ms 38676 KB
02-10.txt AC 551 ms 49884 KB
02-11.txt AC 640 ms 60628 KB
02-12.txt AC 600 ms 60344 KB
02-13.txt AC 617 ms 60792 KB
02-14.txt AC 531 ms 62596 KB
02-15.txt AC 494 ms 45908 KB
02-16.txt AC 542 ms 43404 KB
sample-01.txt AC 101 ms 8420 KB
sample-02.txt AC 104 ms 8424 KB