Submission #4031720


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 = 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];
            }
        }

        if( A[N-2]+1 == A[N-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 5025 Byte
Status WA
Exec Time 467 ms
Memory 46780 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 3
AC × 33
WA × 16
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 215 ms 42752 KB
02.txt AC 216 ms 43052 KB
03.txt AC 200 ms 44420 KB
04.txt AC 213 ms 42964 KB
05.txt AC 197 ms 45160 KB
06.txt AC 197 ms 41984 KB
07.txt WA 208 ms 43004 KB
08.txt WA 221 ms 41740 KB
09.txt WA 215 ms 41556 KB
10.txt AC 214 ms 43688 KB
11.txt AC 183 ms 41316 KB
12.txt AC 209 ms 37648 KB
13.txt AC 194 ms 43392 KB
14.txt AC 200 ms 39616 KB
15.txt WA 220 ms 42884 KB
16.txt WA 214 ms 40080 KB
17.txt WA 218 ms 44800 KB
18.txt WA 213 ms 42600 KB
19.txt AC 215 ms 44892 KB
20.txt AC 226 ms 42632 KB
21.txt AC 202 ms 39112 KB
22.txt AC 196 ms 44908 KB
23.txt AC 211 ms 41820 KB
24.txt AC 219 ms 42276 KB
25.txt AC 212 ms 43560 KB
26.txt AC 211 ms 40372 KB
27.txt AC 215 ms 41844 KB
28.txt AC 213 ms 44724 KB
29.txt AC 210 ms 41240 KB
30.txt AC 217 ms 42740 KB
31.txt WA 213 ms 41684 KB
32.txt WA 224 ms 42644 KB
33.txt AC 194 ms 42552 KB
34.txt AC 208 ms 38536 KB
35.txt WA 221 ms 42328 KB
36.txt WA 218 ms 46780 KB
37.txt WA 218 ms 43936 KB
38.txt WA 217 ms 43604 KB
39.txt WA 209 ms 43244 KB
40.txt WA 467 ms 43708 KB
41.txt AC 73 ms 17876 KB
42.txt AC 72 ms 19028 KB
43.txt AC 72 ms 21076 KB
44.txt WA 73 ms 20692 KB
45.txt AC 73 ms 19668 KB
46.txt AC 72 ms 19540 KB
s1.txt AC 73 ms 19156 KB
s2.txt AC 73 ms 20948 KB
s3.txt AC 72 ms 19540 KB