Submission #4031778


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() {
        if( N == 1 ) return A[0] == 0 ? 0 : -1;

        long ans = 0;
        for (int i = 0; i < N; i++) {
            if( A[i] > i ) return -1;
            if( i == 0 ) continue;

            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 0
Code Size 4958 Byte
Status WA
Exec Time 251 ms
Memory 46452 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 3
AC × 42
WA × 7
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 251 ms 46216 KB
02.txt AC 220 ms 43296 KB
03.txt AC 216 ms 39056 KB
04.txt AC 196 ms 42324 KB
05.txt WA 217 ms 40988 KB
06.txt WA 214 ms 43124 KB
07.txt AC 213 ms 42344 KB
08.txt AC 204 ms 44500 KB
09.txt AC 208 ms 41092 KB
10.txt AC 201 ms 41820 KB
11.txt WA 199 ms 41516 KB
12.txt WA 205 ms 41968 KB
13.txt WA 207 ms 41084 KB
14.txt WA 209 ms 39792 KB
15.txt AC 216 ms 46452 KB
16.txt AC 210 ms 44216 KB
17.txt AC 205 ms 44204 KB
18.txt AC 211 ms 40476 KB
19.txt AC 202 ms 44244 KB
20.txt AC 218 ms 42788 KB
21.txt AC 233 ms 42760 KB
22.txt AC 197 ms 37484 KB
23.txt AC 196 ms 41860 KB
24.txt AC 217 ms 42148 KB
25.txt AC 218 ms 42252 KB
26.txt AC 232 ms 41132 KB
27.txt AC 210 ms 43096 KB
28.txt AC 213 ms 44116 KB
29.txt AC 211 ms 45060 KB
30.txt AC 207 ms 37688 KB
31.txt AC 204 ms 42032 KB
32.txt AC 194 ms 40276 KB
33.txt AC 183 ms 44016 KB
34.txt AC 193 ms 44780 KB
35.txt AC 200 ms 43428 KB
36.txt AC 207 ms 42496 KB
37.txt AC 212 ms 42196 KB
38.txt AC 217 ms 42200 KB
39.txt AC 218 ms 44292 KB
40.txt AC 211 ms 41816 KB
41.txt AC 72 ms 20180 KB
42.txt AC 73 ms 21076 KB
43.txt AC 73 ms 19924 KB
44.txt AC 73 ms 21076 KB
45.txt WA 73 ms 19028 KB
46.txt AC 73 ms 22484 KB
s1.txt AC 73 ms 19028 KB
s2.txt AC 71 ms 17620 KB
s3.txt AC 73 ms 18772 KB