Submission #4031868
Source Code Expand
import java.io.*; import java.util.Arrays; import java.util.StringJoiner; import java.util.StringTokenizer; public class Main { static int N; static int[] A; public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); N = sc.nextInt(); A = sc.nextIntArray(N); System.out.println(solve()); } static long solve() { long ans = 0; if( A[0] != 0 ) return -1; for (int i = 1; i < N; i++) { if( A[i] > i ) return -1; if( A[i] > A[i-1]+1 ) return -1; if( A[i] == A[i-1] + 1 ) { // 0 1 2 みたいなのは最後のやつ(2)だけのコストしかかからない } else { ans += A[i-1]; } } ans += A[N-1]; return ans; } @SuppressWarnings("unused") static class FastScanner { private BufferedReader reader; private StringTokenizer tokenizer; FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } long nextLong() { return Long.parseLong(next()); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArray(int n, int delta) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt() + delta; return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } static void writeLines(int[] as) { PrintWriter pw = new PrintWriter(System.out); for (int a : as) pw.println(a); pw.flush(); } static void writeLines(long[] as) { PrintWriter pw = new PrintWriter(System.out); for (long a : as) pw.println(a); pw.flush(); } static void writeSingleLine(int[] as) { PrintWriter pw = new PrintWriter(System.out); for (int i = 0; i < as.length; i++) { if (i != 0) pw.print(" "); pw.print(i); } pw.println(); pw.flush(); } static int max(int... as) { int max = Integer.MIN_VALUE; for (int a : as) max = Math.max(a, max); return max; } static int min(int... as) { int min = Integer.MAX_VALUE; for (int a : as) min = Math.min(a, min); return min; } static void debug(Object... args) { StringJoiner j = new StringJoiner(" "); for (Object arg : args) { if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg)); else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg)); else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg)); else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg)); else j.add(arg == null ? "null" : arg.toString()); } System.err.println(j.toString()); } static void printSingleLine(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) System.out.print(" "); System.out.print(array[i]); } System.out.println(); } static int lowerBound(int[] array, int value) { int low = 0, high = array.length, mid; while (low < high) { mid = ((high - low) >>> 1) + low; if (array[mid] < value) low = mid + 1; else high = mid; } return low; } static int upperBound(int[] array, int value) { int low = 0, high = array.length, mid; while (low < high) { mid = ((high - low) >>> 1) + low; if (array[mid] <= value) low = mid + 1; else high = mid; } return low; } }
Submission Info
Submission Time | |
---|---|
Task | C - Sequence Growing Easy |
User | kusomushi |
Language | Java8 (OpenJDK 1.8.0) |
Score | 700 |
Code Size | 4953 Byte |
Status | AC |
Exec Time | 283 ms |
Memory | 47624 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 700 / 700 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | s1.txt, s2.txt, s3.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, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, s1.txt, s2.txt, s3.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01.txt | AC | 225 ms | 41668 KB |
02.txt | AC | 197 ms | 43776 KB |
03.txt | AC | 193 ms | 38120 KB |
04.txt | AC | 212 ms | 44124 KB |
05.txt | AC | 192 ms | 39880 KB |
06.txt | AC | 201 ms | 42808 KB |
07.txt | AC | 212 ms | 43860 KB |
08.txt | AC | 212 ms | 41844 KB |
09.txt | AC | 220 ms | 40760 KB |
10.txt | AC | 217 ms | 39492 KB |
11.txt | AC | 207 ms | 41264 KB |
12.txt | AC | 202 ms | 41380 KB |
13.txt | AC | 213 ms | 42044 KB |
14.txt | AC | 202 ms | 41172 KB |
15.txt | AC | 228 ms | 38476 KB |
16.txt | AC | 213 ms | 41640 KB |
17.txt | AC | 206 ms | 44676 KB |
18.txt | AC | 206 ms | 42656 KB |
19.txt | AC | 217 ms | 40800 KB |
20.txt | AC | 201 ms | 44012 KB |
21.txt | AC | 195 ms | 43860 KB |
22.txt | AC | 230 ms | 47624 KB |
23.txt | AC | 201 ms | 45332 KB |
24.txt | AC | 222 ms | 39716 KB |
25.txt | AC | 208 ms | 41860 KB |
26.txt | AC | 213 ms | 44500 KB |
27.txt | AC | 202 ms | 43120 KB |
28.txt | AC | 215 ms | 44412 KB |
29.txt | AC | 210 ms | 44116 KB |
30.txt | AC | 214 ms | 44960 KB |
31.txt | AC | 192 ms | 39940 KB |
32.txt | AC | 207 ms | 44424 KB |
33.txt | AC | 191 ms | 40124 KB |
34.txt | AC | 203 ms | 36756 KB |
35.txt | AC | 211 ms | 42580 KB |
36.txt | AC | 220 ms | 43092 KB |
37.txt | AC | 219 ms | 42844 KB |
38.txt | AC | 227 ms | 41848 KB |
39.txt | AC | 283 ms | 43292 KB |
40.txt | AC | 218 ms | 41440 KB |
41.txt | AC | 73 ms | 19284 KB |
42.txt | AC | 73 ms | 17748 KB |
43.txt | AC | 74 ms | 19796 KB |
44.txt | AC | 74 ms | 21332 KB |
45.txt | AC | 73 ms | 19284 KB |
46.txt | AC | 74 ms | 20692 KB |
s1.txt | AC | 73 ms | 20308 KB |
s2.txt | AC | 73 ms | 20692 KB |
s3.txt | AC | 73 ms | 19028 KB |