Problem:
Score : 500 points

Problem Statement

There are N boxes arranged in a circle. The i-th box contains A_i stones.

Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation:

Select one box. Let the box be the i-th box. Then, for each j from 1 through N, remove exactly j stones from the (i+j)-th box. Here, the (N+k)-th box is identified with the k-th box.

Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed.

Constraints

1 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

If it is possible to remove all the stones from the boxes, print YES. Otherwise, print NO.

Sample Input 1

5
4 5 1 2 3

Sample Output 1

YES

All the stones can be removed in one operation by selecting the second box.

Sample Input 2

5
6 9 12 10 8

Sample Output 2

YES

Sample Input 3

4
1 2 3 1

Sample Output 3

NO

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

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

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() ? "YES" : "NO");
    }

    static boolean solve() {
        long sum = 0;
        for (int a : A) {
            sum += a;
        }

        long set;
        if( N % 2 == 0 ) {
            // 1 2 3 4 -> (1+4) * (4/2)
            set = ((long)N + 1) * (N / 2);

        } else {
            set = ((long)N/2 + 1) * N;
        }
        if( sum % set != 0 ) return false;

        long k = sum / set;

        long summ_diff = 0;
        int[] diff = new int[N];
        for (int i = 0; i < N-1; i++) {
            int d = A[i+1] - A[i];
            summ_diff += d;
            diff[i] = d;
        }
        summ_diff += A[0] - A[N-1];
        diff[N-1] = A[0] - A[N-1];
        if( summ_diff != 0 ) return false;

        int b_total = 0;
        for (int i = 0; i < N; i++) {
            // d = (k-b) + b*(1-N)
            // => b = (k-d)/N
            int d = diff[i];
            if( d > k ) return false; // b=0
            if( d < (long)(1-N) * k ) return false; // b=k

            long r = (k-d)%N;
            if( r != 0 ) return false;

            long b = (k-d)/N;
            b_total += b;
        }

        if( b_total != k ) return false;

        return true;
    }

    @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());
        }

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

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

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