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 Haskell (GHC 7.10.3) (judge verdict: Time Limit Exceeded)

import Control.Applicative ((<$>))
import Data.List (sort)

partitionNumber :: Int -> [[Int]]
partitionNumber n = part_int n n [] []
  where
    part_int 0 _ xs ys = reverse xs:ys
    part_int 1 _ xs ys = reverse (1:xs):ys
    part_int n 1 xs ys = reverse (replicate n 1 ++ xs):ys
    part_int n k xs ys =
      let ys' = part_int n (k - 1) xs ys
      in if n - k >= 0 then part_int (n - k) k (k:xs) ys' else ys'

partitions :: Int -> Int -> [[Int]]
partitions x d = filter (\l->length l==d) $ partitionNumber x

rotate n x
  | x == n = 1
  | otherwise = x+1

check :: [Int] -> Int -> [Int] -> Bool
check [] _ _ = True
check (a:as) n xs
  | sum xs == a = check as n $ map (rotate n) xs
  | otherwise = False

solve :: [Int] -> [(Int,Int)] -> Int -> Int -> Bool
solve as ais n d = do
  let ma = minimum as
      starts = map snd $ takeWhile ((==ma).fst) ais
      ss = [(s,xs)|s<-starts,xs<-partitions ma d]
  any (\(s,xs)-> check (drop s as ++ take s as) n xs) ss

main = do
  n <- read <$> getLine :: IO Int
  as <- map read.words <$> getLine :: IO [Int]
  let m = n*(n+1)`div`2
      s = sum as
      ais = sort $ zip as [0..]
  case s `divMod` m of
    (d,0) -> putStrLn $ if solve as ais n d then "YES" else "NO"
    _ -> putStrLn "NO"