610B.md (2159B)
1 Source: https://codeforces.com/problemset/problem/610/B 2 3 B. Vika and Squares 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Vika has *n* jars with paints of distinct colors. All the jars are numbered from 1 to *n* and the *i*-th jar contains *a**i* liters of paint of color *i*. 22 23 Vika also has an infinitely long rectangular piece of paper of width 1, consisting of squares of size 1 × 1. Squares are numbered 1, 2, 3 and so on. Vika decided that she will start painting squares one by one from left to right, starting from the square number 1 and some arbitrary color. If the square was painted in color *x*, then the next square will be painted in color *x* + 1. In case of *x* = *n*, next square is painted in color 1. If there is no more paint of the color Vika wants to use now, then she stops. 24 25 Square is always painted in only one color, and it takes exactly 1 liter of paint. Your task is to calculate the maximum number of squares that might be painted, if Vika chooses right color to paint the first square. 26 27 Input 28 29 The first line of the input contains a single integer *n* (1 ≤ *n* ≤ 200 000) — the number of jars with colors Vika has. 30 31 The second line of the input contains a sequence of integers *a*1, *a*2, ..., *a**n* (1 ≤ *a**i* ≤ 109), where *a**i* is equal to the number of liters of paint in the *i*-th jar, i.e. the number of liters of color *i* that Vika has. 32 33 Output 34 35 The only line of the output should contain a single integer — the maximum number of squares that Vika can paint if she follows the rules described above. 36 37 Examples 38 39 Input 40 41 ``` 42 5 43 2 4 2 3 3 44 ``` 45 46 Output 47 48 ``` 49 12 50 ``` 51 52 Input 53 54 ``` 55 3 56 5 5 5 57 ``` 58 59 Output 60 61 ``` 62 15 63 ``` 64 65 Input 66 67 ``` 68 6 69 10 10 10 1 10 10 70 ``` 71 72 Output 73 74 ``` 75 11 76 ``` 77 78 Note 79 80 In the first sample the best strategy is to start painting using color 4. Then the squares will be painted in the following colors (from left to right): 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5. 81 82 In the second sample Vika can start to paint using any color. 83 84 In the third sample Vika should start painting using color number 5.