Dataset Viewer
Auto-converted to Parquet
problem
stringlengths
29
7.23k
tests
stringlengths
101
74M
solutions
sequencelengths
1
6.38k
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) — number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) — numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
{"inputs": ["5 3 10\n1 2 3 4 5\nRGBRR\n", "2 1 15\n5 6\nRG\n", "6 1 21\n4 2 3 5 1 6\nRGBGRB\n", "6 1 21\n6 5 4 3 2 1\nRGBRGB\n", "1 1 10\n10\nR\n", "2 1 10\n5 5\nRG\n", "2 1 10\n5 6\nRR\n", "5 3 10\n1 2 3 4 5\nRGBRG\n", "9 1 6\n1 1 1 3 3 3 2 2 2\nRGGBRRGBB\n", "50 39 2000\n48 43 26 24 46 37 15 30 39 34 4 14 29 34 8 18 40 8 17 37 15 29 2 23 41 7 12 13 36 11 24 22 26 46 11 31 10 46 11 35 6 41 16 50 11 1 46 20 46 28\nBGBBBBBBRGGBBBRRRRBBGRGGRBBRBBBRBBBBBRRGBGGRRRBBRB\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 29 49 33 41 14 25 46 43 7 47 28 25 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "50 32 600\n21 21 18 47 16 11 10 46 9 15 27 5 11 42 29 25 16 41 31 8 12 28 1 24 17 40 45 12 33 32 34 2 45 17 49 17 20 42 15 17 8 29 2 20 4 27 50 1 49 1\nBBRBBGBGBBRBGRRGRGGGBGBRRBBBGGBBBBGBGBRBBGRRGGBRGR\n", "50 37 500\n25 43 15 16 29 23 46 18 15 21 33 26 38 25 2 17 48 50 33 31 3 45 40 12 42 29 37 42 7 11 47 16 44 17 27 46 32 23 14 7 27 25 13 32 43 33 36 39 35 7\nGGBBRGBRRRRBBRGBRRRGGRGGRGGBRRRGBBRRGRGGRBGBGGRGBR\n", "50 4 200\n14 10 50 47 41 9 22 21 42 36 50 10 27 28 39 1 36 12 45 35 17 3 15 25 32 4 34 39 44 34 20 15 18 1 38 25 20 45 24 9 18 15 35 36 12 9 28 4 44 10\nBGBRRBGBRRRGRGRBRGGGRBRRGBBGGRBRRGGRGGGBRRBRGGBGBG\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG\n", "30 28 208\n3 42 42 47 46 44 5 28 35 28 35 44 25 44 47 3 3 35 28 5 3 42 3 46 25 25 5 47 46 3\nBGBBGBBBBGRRGGGBRGRGRRGBBRRRRG\n", "39 21 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 20 29 21 34 36 39 30 34 21 20 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "48 2 259\n25 31 22 30 30 17 31 50 28 30 46 43 4 6 10 22 50 14 5 46 12 6 46 3 17 12 4 28 25 14 5 5 6 14 22 12 17 43 43 10 4 3 31 3 25 28 50 10\nBBBBGGRRBRRBBRGGGBGGRGBRBGRGRGRBBRRBRRGBGBGGGRBR\n", "48 25 323\n39 37 32 4 4 32 18 44 49 4 12 12 12 22 22 37 38 32 24 45 44 37 18 39 45 22 24 22 45 39 4 22 24 22 12 49 4 29 18 38 29 29 38 44 12 12 49 4\nRRRRRBRRGBBRGRGGBGGBGBBBRBRGGGGBBRGRBGGGRBRBBRBG\n", "48 33 357\n18 37 22 21 4 17 39 32 40 43 29 29 50 21 39 43 11 11 4 50 36 40 32 50 18 32 11 36 29 36 22 21 29 43 49 18 17 29 37 40 17 37 49 4 39 49 22 29\nGRGGGGBRBRRGGRGBRGBBGRBRRGBBRRBBBGRBBBBGRGGRRBRG\n", "50 50 2000\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "30 28 208\n3 42 42 47 46 44 5 28 35 28 35 44 25 44 47 3 3 35 28 5 3 42 3 46 25 25 5 47 46 3\nBGBBGBBBBGRRGGGBRGRGRRGBBRRRRG\n", "50 39 2000\n48 43 26 24 46 37 15 30 39 34 4 14 29 34 8 18 40 8 17 37 15 29 2 23 41 7 12 13 36 11 24 22 26 46 11 31 10 46 11 35 6 41 16 50 11 1 46 20 46 28\nBGBBBBBBRGGBBBRRRRBBGRGGRBBRBBBRBBBBBRRGBGGRRRBBRB\n", "50 32 600\n21 21 18 47 16 11 10 46 9 15 27 5 11 42 29 25 16 41 31 8 12 28 1 24 17 40 45 12 33 32 34 2 45 17 49 17 20 42 15 17 8 29 2 20 4 27 50 1 49 1\nBBRBBGBGBBRBGRRGRGGGBGBRRBBBGGBBBBGBGBRBBGRRGGBRGR\n", "48 2 259\n25 31 22 30 30 17 31 50 28 30 46 43 4 6 10 22 50 14 5 46 12 6 46 3 17 12 4 28 25 14 5 5 6 14 22 12 17 43 43 10 4 3 31 3 25 28 50 10\nBBBBGGRRBRRBBRGGGBGGRGBRBGRGRGRBBRRBRRGBGBGGGRBR\n", "1 1 10\n10\nR\n", "9 1 6\n1 1 1 3 3 3 2 2 2\nRGGBRRGBB\n", "5 3 10\n1 2 3 4 5\nRGBRG\n", "6 1 21\n6 5 4 3 2 1\nRGBRGB\n", "2 1 10\n5 5\nRG\n", "2 1 10\n5 6\nRR\n", "50 50 2000\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "48 33 357\n18 37 22 21 4 17 39 32 40 43 29 29 50 21 39 43 11 11 4 50 36 40 32 50 18 32 11 36 29 36 22 21 29 43 49 18 17 29 37 40 17 37 49 4 39 49 22 29\nGRGGGGBRBRRGGRGBRGBBGRBRRGBBRRBBBGRBBBBGRGGRRBRG\n", "48 25 323\n39 37 32 4 4 32 18 44 49 4 12 12 12 22 22 37 38 32 24 45 44 37 18 39 45 22 24 22 45 39 4 22 24 22 12 49 4 29 18 38 29 29 38 44 12 12 49 4\nRRRRRBRRGBBRGRGGBGGBGBBBRBRGGGGBBRGRBGGGRBRBBRBG\n", "39 21 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 20 29 21 34 36 39 30 34 21 20 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 29 49 33 41 14 25 46 43 7 47 28 25 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "50 4 200\n14 10 50 47 41 9 22 21 42 36 50 10 27 28 39 1 36 12 45 35 17 3 15 25 32 4 34 39 44 34 20 15 18 1 38 25 20 45 24 9 18 15 35 36 12 9 28 4 44 10\nBGBRRBGBRRRGRGRBRGGGRBRRGBBGGRBRRGGRGGGBRRBRGGBGBG\n", "6 1 21\n4 2 3 5 1 6\nRGBGRB\n", "50 37 500\n25 43 15 16 29 23 46 18 15 21 33 26 38 25 2 17 48 50 33 31 3 45 40 12 42 29 37 42 7 11 47 16 44 17 27 46 32 23 14 7 27 25 13 32 43 33 36 39 35 7\nGGBBRGBRRRRBBRGBRRRGGRGGRGGBRRRGBBRRGRGGRBGBGGRGBR\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG\n", "50 39 2000\n48 43 26 24 46 37 15 30 39 34 4 14 29 34 8 18 40 8 17 37 15 29 2 23 41 7 12 13 36 11 24 22 26 46 11 31 1 46 11 35 6 41 16 50 11 1 46 20 46 28\nBGBBBBBBRGGBBBRRRRBBGRGGRBBRBBBRBBBBBRRGBGGRRRBBRB\n", "48 2 259\n25 31 22 30 30 17 31 50 28 30 46 43 4 6 10 22 50 14 5 46 12 6 46 3 17 12 4 28 25 21 5 5 6 14 22 12 17 43 43 10 4 3 31 3 25 28 50 10\nBBBBGGRRBRRBBRGGGBGGRGBRBGRGRGRBBRRBRRGBGBGGGRBR\n", "9 1 6\n1 1 2 3 3 3 2 2 2\nRGGBRRGBB\n", "2 1 10\n9 5\nRG\n", "48 33 357\n18 37 22 21 4 17 39 32 40 43 29 29 50 21 39 43 11 11 4 50 36 40 32 50 18 32 11 36 29 36 22 21 29 43 49 18 17 29 37 40 17 37 2 4 39 49 22 29\nGRGGGGBRBRRGGRGBRGBBGRBRRGBBRRBBBGRBBBBGRGGRRBRG\n", "39 21 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 10 29 21 34 36 39 30 34 21 20 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "50 37 500\n25 43 15 16 29 23 46 18 15 21 33 26 38 25 2 17 48 50 33 31 3 23 40 12 42 29 37 42 7 11 47 16 44 17 27 46 32 23 14 7 27 25 13 32 43 33 36 39 35 7\nGGBBRGBRRRRBBRGBRRRGGRGGRGGBRRRGBBRRGRGGRBGBGGRGBR\n", "39 36 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 10 29 21 34 36 39 30 34 21 20 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "30 28 208\n3 42 42 47 46 12 5 28 35 28 35 44 25 44 47 3 3 35 28 5 3 42 3 46 25 25 5 47 46 3\nBGBBGBBBBGRRGGGBRGRGRRGBBRRRRG\n", "9 1 6\n1 1 1 3 3 6 2 2 2\nRGGBRRGBB\n", "5 3 10\n1 2 4 4 5\nRGBRG\n", "39 21 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 20 44 21 34 36 39 30 34 21 20 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "50 4 200\n14 10 50 47 41 9 22 21 42 36 50 10 27 28 39 1 36 12 45 35 17 3 15 25 32 4 34 39 44 34 20 15 18 1 38 25 20 3 24 9 18 15 35 36 12 9 28 4 44 10\nBGBRRBGBRRRGRGRBRGGGRBRRGBBGGRBRRGGRGGGBRRBRGGBGBG\n", "6 1 21\n6 5 4 3 4 1\nRGBRGB\n", "50 50 2000\n1 3 5 7 9 11 13 15 32 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 29 49 23 41 14 25 46 43 7 47 28 25 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "6 1 21\n4 2 3 5 1 6\nRGBRGB\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 11 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG\n", "2 1 24\n5 6\nRG\n", "48 2 259\n25 31 22 30 30 17 31 50 28 30 46 43 4 6 10 22 50 27 5 46 12 6 46 3 17 12 4 28 25 21 5 5 6 14 22 12 17 43 43 10 4 3 31 3 25 28 50 10\nBBBBGGRRBRRBBRGGGBGGRGBRBGRGRGRBBRRBRRGBGBGGGRBR\n", "9 1 6\n1 1 2 5 3 3 2 2 2\nRGGBRRGBB\n", "50 50 2000\n1 3 5 7 9 11 13 15 32 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 29 49 23 15 14 25 46 43 7 47 28 25 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "6 1 21\n4 2 3 5 1 6\nBGRBGR\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 11 44 42 40 38 36 34 32 44 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG\n", "9 1 6\n1 1 1 5 3 3 2 2 2\nRGGBRRGBB\n", "50 50 2000\n1 3 5 7 9 11 13 15 32 19 21 32 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "39 36 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 10 29 21 34 36 39 30 34 21 21 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 29 49 23 15 14 25 46 43 7 47 28 50 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "6 1 21\n5 2 3 5 1 6\nBGRBGR\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 11 44 42 40 38 36 34 32 44 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGGGGGGGGGGGGGGGGGGGGGGGGGRRRRRRRRRRRRRRRRRRRRRRRRR\n", "9 1 6\n1 1 1 5 3 1 2 2 2\nRGGBRRGBB\n", "50 50 2000\n1 3 5 7 9 11 13 15 32 19 21 32 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 1 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 40 49 23 15 14 25 46 43 7 47 28 50 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 17 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 11 44 42 40 38 36 34 32 44 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGGGGGGGGGGGGGGGGGGGGGGGGGRRRRRRRRRRRRRRRRRRRRRRRRR\n", "9 1 6\n1 1 1 5 3 1 4 2 2\nRGGBRRGBB\n", "50 50 2000\n1 3 7 7 9 11 13 15 32 19 21 32 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 1 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 50 2000\n1 3 7 7 9 11 13 15 32 19 21 32 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 1 10 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 50 2000\n1 3 7 7 9 11 13 15 32 19 21 32 25 10 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 1 10 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 50 2000\n1 3 7 7 9 11 13 15 32 19 21 32 25 10 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 1 10 6 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 39 2000\n48 43 26 24 46 37 15 30 39 34 4 14 29 34 8 18 40 8 17 40 15 29 2 23 41 7 12 13 36 11 24 22 26 46 11 31 10 46 11 35 6 41 16 50 11 1 46 20 46 28\nBGBBBBBBRGGBBBRRRRBBGRGGRBBRBBBRBBBBBRRGBGGRRRBBRB\n", "48 2 259\n25 31 22 30 30 17 31 50 28 30 46 43 4 6 10 22 50 14 5 46 12 6 46 4 17 12 4 28 25 14 5 5 6 14 22 12 17 43 43 10 4 3 31 3 25 28 50 10\nBBBBGGRRBRRBBRGGGBGGRGBRBGRGRGRBBRRBRRGBGBGGGRBR\n", "1 1 20\n10\nR\n", "2 1 16\n5 5\nRG\n", "2 1 10\n1 6\nRR\n", "50 50 2000\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 7 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "48 33 357\n18 37 22 21 4 17 39 32 40 43 29 29 50 21 39 43 11 11 4 50 36 40 32 50 18 32 11 36 29 36 22 21 29 43 49 18 17 29 37 40 17 43 49 4 39 49 22 29\nGRGGGGBRBRRGGRGBRGBBGRBRRGBBRRBBBGRBBBBGRGGRRBRG\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 31 17 23 48 20 44 46 44 13 4 29 49 33 41 14 25 46 43 7 47 28 25 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "6 1 21\n4 2 3 2 1 6\nRGBGRB\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 1 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG\n", "2 1 15\n5 1\nRG\n", "50 39 2000\n48 43 26 24 46 37 15 30 36 34 4 14 29 34 8 18 40 8 17 37 15 29 2 23 41 7 12 13 36 11 24 22 26 46 11 31 1 46 11 35 6 41 16 50 11 1 46 20 46 28\nBGBBBBBBRGGBBBRRRRBBGRGGRBBRBBBRBBBBBRRGBGGRRRBBRB\n", "2 1 15\n5 6\nRG\n", "5 3 10\n1 2 3 4 5\nRGBRR\n"], "outputs": ["4\n", "-1\n", "15\n", "10\n", "0\n", "-1\n", "-1\n", "2\n", "7\n", "-1\n", "-1\n", "185\n", "86\n", "23\n", "992\n", "20\n", "24\n", "39\n", "64\n", "63\n", "-1\n", "20\n", "-1\n", "185\n", "39\n", "0\n", "7\n", "2\n", "10\n", "-1\n", "-1\n", "-1\n", "63\n", "64\n", "24\n", "-1\n", "23\n", "15\n", "86\n", "992", "-1\n", "39\n", "3\n", "2\n", "63\n", "24\n", "86\n", "31\n", "20\n", "5\n", "4\n", "28\n", "23\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "39\n", "3\n", "-1\n", "-1\n", "-1\n", "-1\n", "3\n", "-1\n", "31\n", "-1\n", "-1\n", "-1\n", "3\n", "-1\n", "-1\n", "-1\n", "3\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "39\n", "-1\n", "-1\n", "-1\n", "-1\n", "63\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "4\n"]}
[ "INF = 10000000000.0\nmax_n = 50\nmax_k = 2000\n\ndef main():\n\t(n, s, k) = map(int, input().split())\n\ts -= 1\n\tbuf = [''] * (max_n + 1)\n\tdp = [[0 for i in range(max_n + 1)] for j in range(max_k + 1)]\n\tr = list(map(int, input().split()))\n\tc = input()\n\tanswer = INF\n\tfor i in range(len(c)):\n\t\tbuf[i] = c[i]\n\tfor i in range(k, -1, -1):\n\t\tfor j in range(n):\n\t\t\tdp[i][j] = INF\n\tfor j in range(n):\n\t\tvalue = abs(j - s)\n\t\tif k - r[j] <= 0:\n\t\t\tanswer = min(answer, value)\n\t\telse:\n\t\t\tdp[k - r[j]][j] = value\n\tfor i in range(k, 0, -1):\n\t\tfor j in range(n):\n\t\t\tif dp[i][j] < INF:\n\t\t\t\tfor l in range(n):\n\t\t\t\t\tif buf[j] != buf[l] and r[j] < r[l]:\n\t\t\t\t\t\tvalue = dp[i][j] + abs(j - l)\n\t\t\t\t\t\tif i - r[l] <= 0:\n\t\t\t\t\t\t\tanswer = min(answer, value)\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tdp[i - r[l]][l] = min(dp[i - r[l]][l], value)\n\tif answer == INF:\n\t\tprint(-1)\n\t\treturn\n\tprint(answer)\n\ndef __starting_point():\n\tmain()\n__starting_point()\n", "(n, s, k) = map(int, input().split())\ns -= 1\nr = list(map(int, input().split()))\nINF = float('inf')\nc = input()\ndp = [[] for i in range(n)]\n\ndef calc(u):\n\tif dp[u]:\n\t\treturn\n\tdp[u] = [0] * (r[u] + 1) + [INF] * (k - r[u])\n\tfor i in range(n):\n\t\tif c[u] != c[i] and r[i] > r[u]:\n\t\t\tcalc(i)\n\t\t\td = abs(u - i)\n\t\t\tfor j in range(r[u] + 1, k + 1):\n\t\t\t\tdp[u][j] = min(dp[u][j], dp[i][j - r[u]] + d)\nans = INF\nfor i in range(n):\n\tcalc(i)\n\tans = min(ans, abs(i - s) + dp[i][k])\nif ans == INF:\n\tprint(-1)\nelse:\n\tprint(ans)\n", "import math\n\ndef solve():\n\t(n, s, k) = map(int, input().split())\n\ts -= 1\n\tr = list(map(int, input().split()))\n\tc = input()\n\tinf = int(1000000000.0)\n\tdp = [[inf for j in range(n)] for i in range(k + 1)]\n\tfor i in range(0, k + 1):\n\t\tfor j in range(0, n):\n\t\t\tif i == 0 or i <= r[j]:\n\t\t\t\tdp[i][j] = 0\n\t\t\t\tcontinue\n\t\t\tfor K in range(0, n):\n\t\t\t\tif c[K] != c[j] and r[K] > r[j]:\n\t\t\t\t\tdp[i][j] = min(dp[i][j], dp[i - r[j]][K] + int(abs(K - j)))\n\tans = min((dp[k][i] + abs(i - s) for i in range(0, n)))\n\tif ans >= inf:\n\t\tprint(-1)\n\t\treturn\n\tprint(ans)\n\treturn\nt = 1\nwhile t > 0:\n\tt -= 1\n\tsolve()\n", "INF = 100000\n(n, s, k) = list(map(int, input().split()))\nr = list(map(int, input().split()))\nc = input().rstrip()\ndp = [[INF for j in range(k + 1)] for i in range(n)]\ns -= 1\nfor i in range(n):\n\tdp[i][k - r[i]] = abs(s - i)\nfor j in range(k, -1, -1):\n\tfor i in range(n):\n\t\tif dp[i][j] >= INF:\n\t\t\tcontinue\n\t\tfor f in range(n):\n\t\t\tif r[f] <= r[i]:\n\t\t\t\tcontinue\n\t\t\tif c[f] == c[i]:\n\t\t\t\tcontinue\n\t\t\tnew_val = max(0, j - r[f])\n\t\t\tdp[f][new_val] = min(dp[f][new_val], dp[i][j] + abs(i - f))\nans = INF\nfor i in range(n):\n\tans = min(ans, dp[i][0])\nif ans >= INF:\n\tans = -1\nprint(ans)\n", "(n, s, k) = map(int, input().split())\nr = list(map(int, input().split()))\ns -= 1\nc = input()\nbest = [[0 for i in range(n)] for j in range(k + 1)]\nfor i in range(1, k + 1):\n\tfor j in range(n):\n\t\tif i <= r[j]:\n\t\t\tbest[i][j] = abs(j - s)\n\t\telse:\n\t\t\tgood = float('inf')\n\t\t\tfor l in range(n):\n\t\t\t\tif c[j] != c[l] and r[j] > r[l]:\n\t\t\t\t\tgood = min(good, best[i - r[j]][l] + abs(j - l))\n\t\t\tbest[i][j] = good\nif min(best[-1]) == float('inf'):\n\tprint(-1)\nelse:\n\tprint(min(best[-1]))\n", "import sys\nsys.setrecursionlimit(1000)\n\ndef rec(r, c, s, K, k, dp):\n\tif (k, s) in dp:\n\t\treturn dp[k, s]\n\tif k <= 0:\n\t\treturn 0\n\tn = len(r)\n\tbesttime = 10 ** 10\n\tfor i in range(n):\n\t\tif r[i] > r[s] and c[i] != c[s] or k == K:\n\t\t\ttimetakenbelow = rec(r, c, i, K, k - r[i], dp)\n\t\t\ttimetaken = timetakenbelow + abs(s - i)\n\t\t\tif timetaken < besttime:\n\t\t\t\tbesttime = timetaken\n\tdp[k, s] = besttime\n\treturn besttime\n\ndef answer(n, s, K, r, c):\n\tdp = dict()\n\tk = K\n\tans = rec(r, c, s, K, k, dp)\n\tif ans == 10 ** 10:\n\t\treturn -1\n\treturn ans\n\ndef main():\n\t(n, s, K) = map(int, sys.stdin.readline().split())\n\tr = tuple(map(int, sys.stdin.readline().split()))\n\tc = sys.stdin.readline().rstrip()\n\tprint(answer(n, s - 1, K, r, c))\n\treturn\nmain()\n", "INF = 10000000000.0\n(n, s, k) = map(int, input().split())\nr = list(map(int, input().split()))\nr.append(0)\ncol = input()\nmat = []\nfor i in range(n + 1):\n\tadj = {}\n\tfor j in range(n):\n\t\tif i == n:\n\t\t\tadj[j] = abs(s - 1 - j)\n\t\telif col[i] != col[j] and r[i] < r[j]:\n\t\t\tadj[j] = abs(i - j)\n\tmat.append(adj)\nmem = [{} for i in range(n + 1)]\n\ndef get(s, k):\n\tif mem[s].get(k):\n\t\treturn mem[s].get(k)\n\tif r[s] >= k:\n\t\tmem[s][k] = 0\n\telse:\n\t\tmi = None\n\t\tfor nei in mat[s]:\n\t\t\tncost = get(nei, k - r[s])\n\t\t\tif ncost is None:\n\t\t\t\tcontinue\n\t\t\tcurr = ncost + mat[s][nei]\n\t\t\tif mi is None or curr < mi:\n\t\t\t\tmi = curr\n\t\tif mi is not None:\n\t\t\tmem[s][k] = mi\n\t\telse:\n\t\t\tmem[s][k] = INF\n\treturn mem[s].get(k)\nans = get(n, k)\nif ans is None or ans >= INF:\n\tprint(-1)\nelse:\n\tprint(ans)\n", "import sys\n\ndef minp():\n\treturn sys.stdin.readline().strip()\ndp = [None] * 50\nfor j in range(50):\n\tdp[j] = [None] * 2001\n(n, s, k) = map(int, minp().split())\na = [None] * n\ni = 0\ns -= 1\nfor j in map(int, minp().split()):\n\ta[i] = (j, i)\n\ti += 1\ni = 0\nfor j in minp():\n\ta[i] += ('RGB'.find(j),)\n\ti += 1\na.sort()\nr = 10 ** 18\nzzz = 0\nfor i in range(n):\n\tii = dp[i]\n\tc = a[i][0]\n\tii[c] = abs(s - a[i][1])\n\tfor j in range(i):\n\t\tif a[j][2] == a[i][2] or a[j][0] == a[i][0]:\n\t\t\tcontinue\n\t\tjj = dp[j]\n\t\tfor z in range(2001 - c):\n\t\t\tzz = jj[z]\n\t\t\tif zz != None:\n\t\t\t\td = zz + abs(a[i][1] - a[j][1])\n\t\t\t\tcc = z + c\n\t\t\t\tif ii[cc] != None:\n\t\t\t\t\tif ii[cc] > d:\n\t\t\t\t\t\tii[cc] = d\n\t\t\t\telse:\n\t\t\t\t\tii[cc] = d\n\tfor z in range(k, 2001):\n\t\tif ii[z] != None:\n\t\t\tr = min(r, ii[z])\nif r != 10 ** 18:\n\tprint(r)\nelse:\n\tprint(-1)\n", "inf = 10000\n(n, s, k) = map(int, input().split())\na = list(map(int, input().split()))\nb = list(input())\nfor i in range(n):\n\tif b[i] == 'R':\n\t\tb[i] = 0\n\telif b[i] == 'G':\n\t\tb[i] = 1\n\telse:\n\t\tb[i] = 2\nboxes = [[a[i], b[i], i] for i in range(n)]\nboxes.sort()\nl = boxes[-1][0] * n + 1\ns -= 1\ndp = [[[inf, s, -1] for j in range(l)] for i in range(3)]\nif l < k:\n\tprint(-1)\n\treturn\ndp[0][0][0] = 0\ndp[1][0][0] = 0\ndp[2][0][0] = 0\nfor i in range(n):\n\tpos = boxes[i][2]\n\tclr = boxes[i][1]\n\tcnt = boxes[i][0]\n\tfor j in range(l - cnt):\n\t\tfor c in range(3):\n\t\t\tif c == clr:\n\t\t\t\tcontinue\n\t\t\tif dp[clr][j + cnt][0] > dp[c][j][0] + abs(dp[c][j][1] - pos) and cnt > dp[c][j][2]:\n\t\t\t\tdp[clr][j + cnt][0] = dp[c][j][0] + abs(dp[c][j][1] - pos)\n\t\t\t\tdp[clr][j + cnt][1] = pos\n\t\t\t\tdp[clr][j + cnt][2] = cnt\nans = min(dp[0][k][0], min(dp[1][k][0], dp[2][k][0]))\nfor i in range(k, l):\n\tans = min(min(ans, dp[0][i][0]), min(dp[1][i][0], dp[2][i][0]))\nif ans < inf:\n\tprint(ans)\nelse:\n\tprint(-1)\n", "(n, s, k) = list(map(int, input().split()))\namounts = list(map(int, input().split()))\ncolors = list(input())\ndp = [[-1 for j in range(k + 1)] for i in range(n)]\n\ndef getAns(nth, left):\n\tif left <= 0:\n\t\treturn 0\n\tif dp[nth][left] >= 0:\n\t\treturn dp[nth][left]\n\tret = 999999999\n\tfor i in range(n):\n\t\tif amounts[i] <= amounts[nth] or colors[i] == colors[nth]:\n\t\t\tcontinue\n\t\tret = min(ret, abs(nth - i) + getAns(i, left - amounts[i]))\n\tdp[nth][left] = ret\n\treturn ret\nans = 999999999\nfor i in range(n):\n\tans = min(ans, getAns(i, k - amounts[i]) + abs(s - 1 - i))\nif ans == 999999999:\n\tans = -1\nprint(ans)\n" ]
If you visit Aizu Akabeko shrine, you will find a unique paper fortune on which a number with more than one digit is written. Each digit ranges from 1 to 9 (zero is avoided because it is considered a bad omen in this shrine). Using this string of numeric values, you can predict how many years it will take before your dream comes true. Cut up the string into more than one segment and compare their values. The difference between the largest and smallest value will give you the number of years before your wish will be fulfilled. Therefore, the result varies depending on the way you cut up the string. For example, if you are given a string 11121314 and divide it into segments, say, as 1,11,21,3,14, then the difference between the largest and smallest is 21 - 1 = 20. Another division 11,12,13,14 produces 3 (i.e. 14 - 11) years. Any random division produces a game of luck. However, you can search the minimum number of years using a program. Given a string of numerical characters, write a program to search the minimum years before your wish will be fulfilled. Input The input is given in the following format. n An integer n is given. Its number of digits is from 2 to 100,000, and each digit ranges from 1 to 9. Output Output the minimum number of years before your wish will be fulfilled. Examples Input 11121314 Output 3 Input 123125129 Output 6 Input 119138 Output 5
{"inputs": ["9714431", "16612328", "23422731", "754526", "955577", "75547", "2112", "799", "88", "32523857", "4787", "1859551", "135661", "3675", "156692", "167918384", "83994", "4837847", "14513597", "15282598", "12659326", "1468417", "6280", "115464", "52376853", "2315", "3641224", "97187", "836", "195884", "36250", "2427817", "17598762", "5744554", "9295", "129848", "3863342", "3743", "133862", "1237", "1625", "1179729", "12651", "3776912", "4829", "73", "2228", "2546", "3136", "138", "3380", "4828", "3652", "5667", "7275", "774", "9329", "279", "15119", "200", "2461", "19", "2258", "31", "1250", "1216", "1595", "271", "236", "187", "166", "123", "231272", "12342923", "16587352", "32887158", "42478456", "353843", "1884868", "148239", "54241537", "213811", "3614", "1003", "177127860", "54250", "1720310", "6415742", "12117", "1293", "5541389", "44936", "550", "43448", "664", "39426", "5003285", "73925", "4379155", "2270", "123125129", "119138", "11121314"], "outputs": ["8\n", "7\n", "6\n", "5\n", "4\n", "3\n", "1\n", "2\n", "0\n", "6\n", "4\n", "8\n", "5\n", "4\n", "8\n", "8\n", "6\n", "5\n", "8\n", "8\n", "8\n", "7\n", "8\n", "5\n", "6\n", "4\n", "5\n", "8\n", "5\n", "8\n", "6\n", "7\n", "8\n", "3\n", "3\n", "8\n", "6\n", "4\n", "7\n", "6\n", "5\n", "8\n", "5\n", "8\n", "7\n", "4\n", "6\n", "4\n", "5\n", "5\n", "8\n", "6\n", "4\n", "2\n", "3\n", "3\n", "7\n", "7\n", "6\n", "2\n", "5\n", "8\n", "6\n", "2\n", "5\n", "4\n", "8\n", "6\n", "4\n", "7\n", "5\n", "2\n", "6\n", "8\n", "7\n", "7\n", "6\n", "5\n", "7\n", "8\n", "6\n", "7\n", "5\n", "3\n", "8\n", "5\n", "7\n", "6\n", "5\n", "8\n", "8\n", "6\n", "5\n", "5\n", "2\n", "7\n", "8\n", "7\n", "8\n", "7\n", "6", "5", "3"]}
[ "def sub(maxs, mins):\n\tfor i in range(len(maxs)):\n\t\tif maxs[i] != mins[i]:\n\t\t\tif i == len(maxs) - 1:\n\t\t\t\treturn int(maxs[i]) - int(mins[i])\n\t\t\tif i == len(maxs) - 2:\n\t\t\t\treturn int(maxs[i:i + 2]) - int(mins[i:i + 2])\n\t\t\treturn 10\n\treturn 0\n\ndef checkEqual(S):\n\tans = 8\n\tfor k in range(1, len(S)):\n\t\tif len(S) % k != 0:\n\t\t\tcontinue\n\t\tmins = maxs = S[0:k]\n\t\tfor s in range(0, len(S), k):\n\t\t\tmaxs = max(maxs, S[s:s + k])\n\t\t\tmins = min(mins, S[s:s + k])\n\t\tans = min(ans, sub(maxs, mins))\n\treturn ans\n\ndef check12(S):\n\tmaxv = 0\n\tminv = 10\n\tp = 0\n\twhile p < len(S):\n\t\tv = int(S[p])\n\t\tif S[p] == '1' and p + 1 < len(S):\n\t\t\tv = 10 + int(S[p + 1])\n\t\t\tp += 1\n\t\tmaxv = max(maxv, v)\n\t\tminv = min(minv, v)\n\t\tp += 1\n\treturn maxv - minv\nS = input()\nprint(min(checkEqual(S), check12(S)))\n", "n = input()\nlength = len(n)\nans = 10\nlst = []\nind = 0\nwhile ind < length:\n\tif n[ind] == '1' and ind + 1 <= length - 1:\n\t\tlst.append(int(n[ind:ind + 2]))\n\t\tind += 2\n\telse:\n\t\tlst.append(int(n[ind]))\n\t\tind += 1\nif len(lst) >= 2:\n\tans = min(ans, max(lst) - min(lst))\ndivisors = []\nfor i in range(1, length // 2 + 1):\n\tif length % i == 0:\n\t\tdivisors.append(i)\nfor i in divisors:\n\tlst = []\n\tfor j in range(0, length, i):\n\t\tlst.append(int(n[j:j + i]))\n\tans = min(ans, max(lst) - min(lst))\nprint(ans)\n" ]
An **anagram** is the result of rearranging the letters of a word to produce a new word. **Note:** anagrams are case insensitive Complete the function to return `true` if the two arguments given are anagrams of each other; return `false` otherwise. ## Examples * `"foefet"` is an anagram of `"toffee"` * `"Buckethead"` is an anagram of `"DeathCubeK"`
{"fn_name": "is_anagram", "inputs": [["foefet", "toffee"], ["Buckethead", "DeathCubeK"], ["Twoo", "WooT"], ["dumble", "bumble"], ["ound", "round"], ["apple", "pale"]], "outputs": [[true], [true], [true], [false], [false], [false]]}
[ "def is_anagram(test, original):\n\treturn sorted(original.lower()) == sorted(test.lower())\n", "from collections import Counter\n\ndef is_anagram(test, original):\n\treturn Counter(test.lower()) == Counter(original.lower())\n", "def is_anagram(test, original):\n\treturn sorted(test.upper()) == sorted(original.upper())\n", "def is_anagram(test, original):\n\t(test_dict, original_dict) = ({}, {})\n\tfor i in test.lower():\n\t\ttest_dict[i] = test_dict.get(i, 0) + 1\n\tfor i in original.lower():\n\t\toriginal_dict[i] = original_dict.get(i, 0) + 1\n\treturn test_dict == original_dict\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\tcount = [0] * 26\n\tfor i in range(len(test)):\n\t\tcount[(ord(test[i]) & 31) - 1] += 1\n\t\tcount[(ord(original[i]) & 31) - 1] -= 1\n\treturn not any(count)\n", "def is_anagram(test, original):\n\ta = sorted(test.lower())\n\tb = sorted(original.lower())\n\tc = ''.join(a)\n\td = ''.join(b)\n\tif c == d:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tgo = len(test) == len(original)\n\tarr = []\n\tif go:\n\t\tfor i in test:\n\t\t\tarr.append(i.lower() in original.lower())\n\t\treturn False not in arr\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\tfor l in test.lower():\n\t\tif l not in original.lower():\n\t\t\treturn False\n\treturn True\n", "from operator import eq\nfrom collections import Counter\n\ndef is_anagram(test, original):\n\treturn eq(*map(Counter, map(str.lower, (test, original))))\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\treturn sorted(test.lower()) == sorted(original.lower())\n", "def is_anagram(test, original):\n\tif sorted(test.lower()) == sorted(original.lower()):\n\t\treturn True\n\telse:\n\t\treturn False\n", "is_anagram = lambda t, o: sorted(t.lower()) == sorted(o.lower())\n", "aprime = {'a': 2, 'c': 5, 'b': 3, 'e': 11, 'd': 7, 'g': 17, 'f': 13, 'i': 23, 'h': 19, 'k': 31, 'j': 29, 'm': 41, 'l': 37, 'o': 47, 'n': 43, 'q': 59, 'p': 53, 's': 67, 'r': 61, 'u': 73, 't': 71, 'w': 83, 'v': 79, 'y': 97, 'x': 89, 'z': 101}\n\ndef aprime_sum(str):\n\tstrChList = list(str.lower())\n\treturn sum([aprime[x] for x in strChList])\n\ndef is_anagram(test, original):\n\tif aprime_sum(test) == aprime_sum(original):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\treturn set(original.lower()) == set(test.lower()) if len(test) == len(original) else False\n", "def is_anagram(test, original):\n\ta = list(test.lower())\n\ts = list(original.lower())\n\tif len(a) != len(s):\n\t\treturn False\n\telse:\n\t\tfor i in a:\n\t\t\tcond = False\n\t\t\tk = 0\n\t\t\twhile k != len(s) and cond == False:\n\t\t\t\tif i == s[k]:\n\t\t\t\t\ta.remove(i)\n\t\t\t\t\ts.remove(i)\n\t\t\t\t\tcond = True\n\t\t\t\tk += 1\n\t\t\tif cond == False:\n\t\t\t\treturn False\n\t\tif len(a) != len(s):\n\t\t\treturn False\n\t\telse:\n\t\t\treturn True\n", "def is_anagram(test, original):\n\tflag = 0\n\tif len(test) != len(original):\n\t\treturn False\n\telse:\n\t\tfor i in test.lower():\n\t\t\tif i not in original.lower():\n\t\t\t\tflag = 1\n\t\t\telse:\n\t\t\t\tcontinue\n\t\tif flag == 1:\n\t\t\treturn False\n\t\telse:\n\t\t\treturn True\n", "def is_anagram(test, original):\n\n\tdef to_dict(word):\n\t\tdictionary = {}\n\t\tfor w in word.lower():\n\t\t\tif w not in dictionary:\n\t\t\t\tdictionary[w] = 0\n\t\t\telse:\n\t\t\t\tdictionary[w] += 1\n\t\treturn dictionary\n\treturn to_dict(test) == to_dict(original)\n", "is_anagram = lambda a, b, s=sorted: s(a.lower()) == s(b.lower())\n", "def is_anagram(s, l):\n\tn = len(s)\n\tif len(l) != n:\n\t\treturn False\n\ts = s.lower()\n\tl = l.lower()\n\th = [0 for x in range(26)]\n\tfor i in range(n):\n\t\th[ord(s[i]) - 97] += 1\n\t\th[ord(l[i]) - 97] -= 1\n\treturn h.count(0) == 26\n", "def is_anagram(test: str, original: str) -> bool:\n\treturn all([all([_ in original.lower() for _ in test.lower()]), len(test) == len(original)])\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\ttestcount = 0\n\tfor i in test:\n\t\tif i in original:\n\t\t\ttestcount += 1\n\toriginalcount = 0\n\tfor i in original:\n\t\tif i in test:\n\t\t\toriginalcount += 1\n\tif testcount == originalcount and testcount == len(test) and (originalcount == len(original)):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) == len(original):\n\t\ttest = test.lower()\n\t\toriginal = original.lower()\n\t\tcount = 0\n\t\tfor char in test:\n\t\t\tif char in original:\n\t\t\t\tcount += 1\n\t\tif count == len(test):\n\t\t\treturn True\n\t\telse:\n\t\t\treturn False\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest_list = []\n\toriginal_list = []\n\tfor i in test.lower():\n\t\ttest_list.append(i)\n\tfor i in original.lower():\n\t\toriginal_list.append(i)\n\ttest_list.sort()\n\toriginal_list.sort()\n\tprint(test_list)\n\tprint(original_list)\n\tif test_list == original_list:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\tletters = {}\n\tfor i in test.lower():\n\t\tif i in letters:\n\t\t\tletters[i] += 1\n\t\telse:\n\t\t\tletters[i] = 1\n\tfor i in original.lower():\n\t\tif i not in letters:\n\t\t\treturn False\n\t\tif original.lower().count(i) != letters[i]:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(t, o):\n\treturn sorted([*t.lower()]) == sorted([*o.lower()])\n", "def is_anagram(test, original):\n\tx = list(test.lower())\n\ty = list(original.lower())\n\tx = sorted(x)\n\ty = sorted(y)\n\tif x == y:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\ta = sorted(test.lower())\n\tb = sorted(original.lower())\n\tif a == b:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tsorted_test = sorted(list(test.lower()))\n\tsorted_original = sorted(list(original.lower()))\n\treturn sorted_test == sorted_original\n", "def is_anagram(test, original):\n\tletters = [c for c in test.lower()]\n\tfor char in original.lower():\n\t\tif char in letters:\n\t\t\tdel letters[letters.index(char)]\n\t\telse:\n\t\t\treturn False\n\treturn not bool(len(letters))\n", "import collections\n\ndef is_anagram(test, original):\n\treturn collections.Counter([i.lower() for i in sorted(test)]) == collections.Counter([i.lower() for i in sorted(original)])\n", "def is_anagram(test, original):\n\ttest_set = sorted(test.lower())\n\toriginal_set = sorted(original.lower())\n\tif test_set == original_set:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tt = sorted(test.lower())\n\to = sorted(original.lower())\n\tif t == o:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tnew_test = test.lower()\n\tnew_original = original.lower()\n\tsortedTest = sorted(new_test)\n\tsortedOriginal = sorted(new_original)\n\tfor letters in new_test:\n\t\tif letters in new_original and len(new_test) == len(new_original) and (sortedOriginal == sortedTest):\n\t\t\treturn True\n\t\telse:\n\t\t\treturn False\n", "def is_anagram(test, original):\n\ttest_word_freq = {}\n\toriginal_word_freq = {}\n\ttest = test.lower()\n\toriginal = original.lower()\n\tif len(test) == len(original):\n\t\tfor (idx, letter) in enumerate(test):\n\t\t\tif letter not in test_word_freq:\n\t\t\t\ttest_word_freq[letter] = 1\n\t\t\telse:\n\t\t\t\ttest_word_freq[letter] += 1\n\t\tfor (idx, lett) in enumerate(original):\n\t\t\tif lett not in original_word_freq:\n\t\t\t\toriginal_word_freq[lett] = 1\n\t\t\telse:\n\t\t\t\toriginal_word_freq[lett] += 1\n\t\tprint(original_word_freq)\n\t\tprint(test_word_freq)\n\t\tfor (k, v) in list(test_word_freq.items()):\n\t\t\tif k not in original_word_freq:\n\t\t\t\treturn False\n\t\t\tif v != original_word_freq[k]:\n\t\t\t\treturn False\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tfirst = [i.lower() for i in test]\n\tsecond = [i.lower() for i in original]\n\treturn sorted(first) == sorted(second)\n", "def is_anagram(test, original):\n\tlist_test = []\n\tlist_original = []\n\tfor i in test.lower():\n\t\tlist_test += i\n\tfor i in original.lower():\n\t\tlist_original += i\n\tif len(list_test) == len(list_original):\n\t\tlist_test.sort()\n\t\tlist_original.sort()\n\t\tif list_test == list_original:\n\t\t\treturn True\n\t\telse:\n\t\t\treturn False\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\treturn True if sorted([letter for letter in test.lower()]) == sorted([letter for letter in original.lower()]) else False\n", "def is_anagram(test, original):\n\tt = list(test.lower())\n\tto = ''.join(sorted(t))\n\to = list(original.lower())\n\too = ''.join(sorted(o))\n\tif to == oo:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tletterCount = dict.fromkeys('abcdefghijklmnopqrstuvwxyz', 0)\n\tfor c in test.lower():\n\t\tletterCount[c] += 1\n\tfor c in original.lower():\n\t\tletterCount[c] -= 1\n\tfor value in list(letterCount.values()):\n\t\tif value != 0:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(a_str, b_str):\n\tif len(a_str) == len(b_str):\n\t\ta_list = list(a_str.lower())\n\t\tb_list = list(b_str.lower())\n\t\tfor char in a_list:\n\t\t\tif char in b_list:\n\t\t\t\tb_list.remove(char)\n\t\tif not b_list:\n\t\t\treturn True\n\t\telse:\n\t\t\treturn False\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\telse:\n\t\ttest = test.lower()\n\t\toriginal = original.lower()\n\t\tcounter_original = [0] * 26\n\t\tcounter_test = [0] * 26\n\t\tfor i in test:\n\t\t\tcounter_test[ord(i) - 97] += 1\n\t\tfor i in original:\n\t\t\tcounter_original[ord(i) - 97] += 1\n\treturn counter_test == counter_original\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tnewList = [ord(c) for c in test]\n\tnewList.sort()\n\tnewList2 = [ord(b) for b in original]\n\tnewList2.sort()\n\tif newList == newList2:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tcounterTest = [0] * 255\n\tcounterOri = [0] * 255\n\tfor i in range(len(test)):\n\t\tcounterTest[ord(test[i].lower())] += 1\n\tfor i in range(len(original)):\n\t\tcounterOri[ord(original[i].lower())] += 1\n\tif counterOri == counterTest:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest = test.upper()\n\toriginal = original.upper()\n\tif sorted(test) == sorted(original):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) == len(original):\n\t\ttest = test.lower()\n\t\toriginal = original.lower()\n\t\tfor i in test:\n\t\t\tif original.find(i) == -1:\n\t\t\t\treturn False\n\t\t\telse:\n\t\t\t\ttest.replace(i, '')\n\t\t\t\toriginal.replace(i, '')\n\telse:\n\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tcounter1 = [0] * 255\n\tcounter2 = [0] * 255\n\tfor i in range(len(test)):\n\t\tcounter1[ord(test[i].lower())] += 1\n\tfor i in range(len(original)):\n\t\tcounter2[ord(original[i].lower())] += 1\n\treturn counter1 == counter2\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tfor x in range(len(test)):\n\t\tif test.count(test[x]) != original.count(test[x]):\n\t\t\treturn False\n\tfor x in range(len(original)):\n\t\tif test.count(original[x]) != original.count(original[x]):\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tnT = len(test)\n\tnO = len(original)\n\tif nO == nT:\n\t\tcounterT = [0] * (255 + 1)\n\t\tcounterO = [0] * (255 + 1)\n\t\tfor x in range(nT):\n\t\t\tcounterT[ord(test[x])] += 1\n\t\t\tcounterO[ord(original[x])] += 1\n\t\tif counterT == counterO:\n\t\t\treturn True\n\t\telse:\n\t\t\treturn False\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tn = len(original)\n\tif n != len(test):\n\t\treturn False\n\tcounterTest = [0] * 255\n\tcounterOrig = [0] * 255\n\tfor i in range(n):\n\t\tcounterTest[ord(test[i].lower())] += 1\n\t\tcounterOrig[ord(original[i].lower())] += 1\n\treturn True if ''.join(map(str, counterTest)) == ''.join(map(str, counterOrig)) else False\n", "def is_anagram(test, original):\n\treturn sorted([n.lower() for n in test]) == sorted([n.lower() for n in original])\n", "def is_anagram(word_o, test_o):\n\tis_anagram = True\n\tword = word_o.lower()\n\ttest = test_o.lower()\n\tif len(word) != len(test):\n\t\tis_anagram = False\n\talist = list(test.lower())\n\tpos1 = 0\n\twhile pos1 < len(word) and is_anagram:\n\t\tpos2 = 0\n\t\tfound = False\n\t\twhile pos2 < len(alist) and (not found):\n\t\t\tif word[pos1] == alist[pos2]:\n\t\t\t\tfound = True\n\t\t\telse:\n\t\t\t\tpos2 = pos2 + 1\n\t\tif found:\n\t\t\talist[pos2] = None\n\t\telse:\n\t\t\tis_anagram = False\n\t\tpos1 = pos1 + 1\n\treturn is_anagram\n", "def is_anagram(test, original):\n\tl1 = list(test.lower())\n\tl2 = list(original.lower())\n\tif len(l1) == len(l2):\n\t\tfor i in l1:\n\t\t\tif i in l2:\n\t\t\t\tl2.remove(i)\n\t\t\telse:\n\t\t\t\treturn False\n\telse:\n\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tfor i in test.lower():\n\t\tif i in original.lower() and len(test) == len(original):\n\t\t\tcontinue\n\t\telse:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\ttest_list = [letter1 for letter1 in test.lower()]\n\torig_list = [letter2 for letter2 in original.lower()]\n\tif sorted(test_list) == sorted(orig_list):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tt = sorted(test.lower())\n\to = sorted(original.lower())\n\tif t == o:\n\t\tprint('true')\n\t\treturn True\n\telse:\n\t\tprint('false')\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest = [i.lower() for i in test]\n\toriginal = [j.lower() for j in original]\n\ttest.sort()\n\toriginal.sort()\n\treturn test == original\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tif len(test) != len(original):\n\t\treturn False\n\tfor x in test:\n\t\tif test.count(x) == original.count(x):\n\t\t\tcontinue\n\t\telse:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tnew_test = list(test)\n\tnew_original = list(original)\n\tnew_test.sort()\n\tnew_original.sort()\n\tif new_test == new_original:\n\t\treturn True\n\treturn False\n\tpass\n", "def is_anagram(test, original):\n\treturn set(test.upper()) == set(original.upper()) and len(test) == len(original)\n", "is_anagram = lambda test, original: True if sorted(original.lower()) == sorted(test.lower()) else False\n", "def is_anagram(test, original):\n\toriginalLower = [val for val in original.lower()]\n\tarr = test.lower()\n\tif len(arr) != len(originalLower):\n\t\treturn False\n\tfor element in arr:\n\t\tif element not in originalLower:\n\t\t\treturn False\n\t\telse:\n\t\t\toriginalLower.remove(element)\n\treturn True\n", "def is_anagram(test, original):\n\tn1 = len(test)\n\tn2 = len(original)\n\tif n1 != n2:\n\t\treturn False\n\tstr1 = sorted(test.lower())\n\tstr2 = sorted(original.lower())\n\tfor i in range(0, n1):\n\t\tif str1[i] != str2[i]:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\ttest_l = list(test.lower())\n\toriginal_l = list(original.lower())\n\ttest_l.sort()\n\toriginal_l.sort()\n\tif test_l == original_l:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest = list(test.lower())\n\toriginal = list(original.lower())\n\tif len(test) != len(original):\n\t\treturn False\n\tfor word in test:\n\t\tfor word2 in original:\n\t\t\tif word == word2:\n\t\t\t\toriginal.remove(word2)\n\t\t\t\tbreak\n\tif len(original) == 0:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\ta = sorted(list(test.lower()))\n\tb = sorted(list(original.lower()))\n\tif a == b:\n\t\tprint(f'The word {test} is an anagram of {original}')\n\t\treturn True\n\telse:\n\t\tprint(f'Characters do not match for test case {test}, {original}')\n\t\treturn False\n", "def is_anagram(test, original):\n\n\tdef to_list(string):\n\t\tlisted = []\n\t\tfor i in range(len(string)):\n\t\t\tlisted.append(string[i])\n\t\treturn listed\n\treturn str(sorted(to_list(test.lower()))) == str(sorted(to_list(original.lower())))\n", "def is_anagram(test, original):\n\ttest = list(test.lower())\n\ttest.sort()\n\toriginal = list(original.lower())\n\toriginal.sort()\n\tif original != test or len(test) != len(original):\n\t\treturn False\n\telse:\n\t\treturn True\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\ttest = sorted(test.lower())\n\toriginal = sorted(original.lower())\n\tfor i in range(len(test)):\n\t\tif test[i] != original[i]:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tresult = True if len(test) == len(original) else False\n\tfor letter in test.upper():\n\t\tresult = False if letter not in original.upper() else result\n\treturn result\n", "def is_anagram(test, original):\n\tif len(original) != len(test):\n\t\treturn False\n\ttest = test.lower()\n\toriginal = original.lower()\n\tfor letter in original:\n\t\tif original.count(letter) != test.count(letter):\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tif sorted(test.lower()) == sorted(original.lower()):\n\t\treturn True\n\telif test != original:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest_list = sorted(list(test.lower()))\n\toriginal_list = sorted(list(original.lower()))\n\tif test_list == original_list:\n\t\treturn True\n\tif test_list != original_list:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tt = list(test)\n\to = list(original)\n\tt.sort()\n\to.sort()\n\treturn t == o\n", "def is_anagram(test, original):\n\tt = test.lower()\n\to = [*original.lower()]\n\tif len(t) != len(o):\n\t\treturn False\n\tfor c in t:\n\t\tif c in o:\n\t\t\to.remove(c)\n\t\telse:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tif len(test) > len(original) or len(test) < len(original):\n\t\treturn False\n\tres = ''\n\tcounter = 0\n\tsortedTest = sorted(test.lower())\n\tsortedOriginal = sorted(original.lower())\n\tfor i in range(0, len(sortedTest)):\n\t\tif sortedTest[i] != sortedOriginal[i]:\n\t\t\tres = False\n\t\t\tbreak\n\t\telse:\n\t\t\tres = True\n\treturn res\n", "from collections import Counter as C\n\ndef is_anagram(test, original):\n\treturn C(test.lower()) == C(original.lower())\n", "def is_anagram(test, original):\n\tsort1 = sorted(test.lower())\n\tsort2 = sorted(original.lower())\n\tif ''.join(sort2) == ''.join(sort1):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttheTest = test.lower()\n\ttheOriginal = original.lower()\n\tif len(theTest) != len(theOriginal):\n\t\treturn False\n\telse:\n\t\tindex = 0\n\t\tlengthCheck = 0\n\t\tarray = [None] * len(theTest)\n\t\tfor i in theOriginal:\n\t\t\tarray[index] = i\n\t\t\tindex += 1\n\t\tfor j in theTest:\n\t\t\ttestLength = len(theTest)\n\t\t\tif j in array:\n\t\t\t\tlengthCheck += 1\n\t\t\telse:\n\t\t\t\treturn False\n\t\tif lengthCheck == testLength:\n\t\t\treturn True\n", "def is_anagram(tst, org):\n\ttst = tst.lower()\n\torg = org.lower()\n\tif len(tst) != len(org):\n\t\treturn False\n\tfor i in org:\n\t\tif tst.count(i) != org.count(i):\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\telif sorted(test.casefold()) == sorted(original.casefold()):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tletters_original = sorted(list(original.upper()))\n\tletters_test = sorted(list(test.upper()))\n\treturn letters_original == letters_test\n", "def is_anagram(test, original):\n\treturn len(test) == len(original) and all([i in original.lower() for i in test.lower()])\n", "def is_anagram(test, original):\n\torg1 = [x.lower() for x in original]\n\torg2 = [y.lower() for y in test]\n\torg1.sort()\n\torg2.sort()\n\tif org1 == org2:\n\t\treturn True\n\treturn False\n", "def is_anagram(test, original):\n\toriginal_list = list(original.lower())\n\ttest_list = list(test.lower())\n\toriginal_list.sort()\n\ttest_list.sort()\n\ta = ''.join(test_list)\n\tb = ''.join(original_list)\n\treturn a == b\n", "def is_anagram(test, original):\n\ttest = test.lower().replace(' ', '')\n\toriginal = original.lower().replace(' ', '')\n\tif len(test) != len(original):\n\t\treturn False\n\tfor letter in test:\n\t\tif letter not in original:\n\t\t\treturn False\n\tfor letter in original:\n\t\tif letter not in test:\n\t\t\treturn False\n\treturn True\n" ]
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi. Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day. Input The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days. The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day. Output Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. Examples Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 Note In the first example, the following figure shows an optimal case. <image> Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. <image>
{"inputs": ["3\n0 1 1\n", "4\n0 0 1 2\n", "2\n0 0\n", "4\n0 1 1 0\n", "3\n0 1 0\n", "2\n0 1\n", "8\n0 0 2 0 3 0 3 2\n", "3\n0 1 2\n", "10\n0 0 2 2 3 2 3 3 1 3\n", "6\n0 0 0 2 0 1\n", "10\n0 1 2 0 4 5 3 6 0 5\n", "4\n0 0 1 1\n", "3\n0 0 0\n", "9\n0 1 0 1 1 4 0 4 8\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 23 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "3\n0 0 1\n", "5\n0 1 0 3 1\n", "4\n0 1 0 3\n", "7\n0 1 1 3 0 0 6\n", "1\n0\n", "3\n0 0 2\n", "4\n0 1 1 1\n", "10\n0 0 1 2 3 2 3 3 1 3\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "5\n0 1 0 3 0\n", "7\n0 1 2 3 0 0 6\n", "5\n0 1 2 0 2\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "7\n0 0 1 3 0 0 6\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 30 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "10\n0 0 2 2 1 2 3 3 1 3\n", "10\n0 1 2 0 4 0 3 6 0 5\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 23 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 11 43 4\n", "10\n0 0 1 2 3 2 3 0 1 3\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 16 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 0 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 21 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 30 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 69 45 43 4\n", "10\n0 1 2 0 4 0 3 6 1 5\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 71 37 37 38 39 28 0 2 23 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 11 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 16 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 0 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 0 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 30 31 31 31 0 3 15 31 13 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 8 28 0 2 26 41 9 9 0 6 25 41 41 12 42 20 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 21 14 8 15 15 15 19 25 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 30 0 5 8 28 29 48 31 31 31 0 3 3 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 69 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 71 37 37 38 39 28 0 2 23 41 9 5 0 6 25 41 41 12 42 43 43 36 44 51 11 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 9 12 16 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 0 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 10 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 8 28 0 2 26 41 9 9 0 6 25 41 41 12 42 20 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 21 14 8 15 15 15 19 25 7 17 17 18 19 9 10 5 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 30 0 5 8 28 29 48 31 31 31 0 3 3 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 0 25 41 41 12 42 43 43 36 44 69 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 3 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 71 37 37 38 39 28 0 2 23 41 9 5 0 6 25 41 41 12 42 43 43 36 44 51 11 43 4\n", "4\n0 1 0 2\n", "4\n0 1 0 0\n", "6\n0 0 0 1 0 1\n", "4\n0 0 1 0\n", "4\n0 1 1 3\n", "5\n0 1 1 1 2\n", "5\n0 1 2 1 4\n", "6\n0 1 1 3 0 2\n", "4\n0 1 2 1\n", "5\n0 1 0 1 0\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 8 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "6\n0 0 0 1 1 1\n", "5\n0 1 2 0 4\n", "6\n0 0 1 3 0 2\n", "4\n0 0 2 1\n", "5\n0 1 1 1 0\n", "10\n0 1 1 0 4 0 3 6 1 5\n", "6\n0 0 1 3 0 3\n", "4\n0 0 2 0\n", "5\n0 1 1 2 0\n", "10\n0 1 1 1 4 0 3 6 1 5\n", "6\n0 0 0 3 0 3\n", "5\n0 1 1 2 2\n", "5\n0 1 2 1 2\n", "6\n0 1 0 3 0 2\n"], "outputs": ["0\n", "0\n", "0\n", "1\n", "1\n", "0\n", "7\n", "0\n", "4\n", "4\n", "12\n", "0\n", "0\n", "17\n", "761\n", "0\n", "4\n", "2\n", "10\n", "0\n", "1\n", "0\n", "3\n", "758\n", "5\n", "9\n", "2\n", "772\n", "11\n", "1479\n", "1515\n", "1510\n", "4\n", "16\n", "795\n", "6\n", "774\n", "773\n", "1549\n", "1771\n", "15\n", "2658\n", "776\n", "768\n", "1533\n", "1543\n", "1783\n", "2662\n", "779\n", "1528\n", "1559\n", "1789\n", "2667\n", "1\n", "2\n", "1\n", "1\n", "1\n", "0\n", "2\n", "5\n", "1\n", "2\n", "1510\n", "0\n", "3\n", "6\n", "2\n", "1\n", "16\n", "5\n", "3\n", "2\n", "15\n", "6\n", "0\n", "1\n", "6\n"]}
[ "n = int(input())\nabove = list(map(int, input().split()))\ntotal = [x + 1 for x in above]\nfor i in range(0, n - 1)[::-1]:\n\ttotal[i] = max(total[i], total[i + 1] - 1)\nfor i in range(1, n):\n\ttotal[i] = max(total[i], total[i - 1])\nbelow = [t - a - 1 for (t, a) in zip(total, above)]\nprint(sum(below))\n", "from sys import stdin, stdout\n\ndef rint():\n\treturn map(int, stdin.readline().split())\nn = int(input())\nu = list(rint())\nu = [0] + u\nmark = 0\nb = [0]\nfor i in range(1, n + 1):\n\tuu = u[i]\n\tb.append(i)\n\tif uu >= mark:\n\t\tinc = uu - mark + 1\n\t\tl = len(b)\n\t\tfor i in range(inc):\n\t\t\tb.pop()\n\t\tmark += inc\ntot = [1 for i in range(n + 1)]\nfor bb in b:\n\ttot[bb] = 0\nfor i in range(1, n + 1):\n\ttot[i] = tot[i - 1] + tot[i]\nans = 0\nfor i in range(1, n + 1):\n\tans += tot[i] - u[i] - 1\nprint(ans)\n", "n = int(input())\nm = list(map(int, input().split()))\na = [0] * n\nk = 0\nfor i in range(n):\n\tk = max(k, m[i] + 1)\n\ta[i] = k\nfor i in range(n - 1, 0, -1):\n\ta[i - 1] = max(a[i] - 1, a[i - 1])\nans = 0\nfor i in range(n):\n\tans += a[i] - m[i] - 1\nprint(ans)\n" ]
You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $a_i = a_{i + 1}$ (if there is at least one such pair). Replace them by one element with value $a_i + 1$. After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $a$ you can get? -----Input----- The first line contains the single integer $n$ ($1 \le n \le 500$) — the initial length of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) — the initial array $a$. -----Output----- Print the only integer — the minimum possible length you can get after performing the operation described above any number of times. -----Examples----- Input 5 4 3 2 2 3 Output 2 Input 7 3 3 4 4 4 3 3 Output 2 Input 3 1 3 5 Output 3 Input 1 1000 Output 1 -----Note----- In the first test, this is one of the optimal sequences of operations: $4$ $3$ $2$ $2$ $3$ $\rightarrow$ $4$ $3$ $3$ $3$ $\rightarrow$ $4$ $4$ $3$ $\rightarrow$ $5$ $3$. In the second test, this is one of the optimal sequences of operations: $3$ $3$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $3$ $3$ $\rightarrow$ $4$ $4$ $4$ $4$ $4$ $\rightarrow$ $5$ $4$ $4$ $4$ $\rightarrow$ $5$ $5$ $4$ $\rightarrow$ $6$ $4$. In the third and fourth tests, you can't perform the operation at all.
{"inputs": ["5\n4 3 2 2 3\n", "7\n3 3 4 4 4 3 3\n", "3\n1 3 5\n", "1\n1000\n", "15\n67 67 65 65 66 66 66 68 67 67 67 65 65 66 70\n", "15\n17 17 18 17 16 16 17 17 15 15 13 12 12 14 15\n", "20\n10 6 25 38 8 4 22 40 28 45 23 33 18 39 28 26 40 4 14 47\n", "20\n6 5 5 7 12 12 12 12 7 41 35 28 28 28 28 13 12 12 7 6\n", "20\n6 9 29 29 1 1 2 8 9 2 13 37 37 37 37 24 14 17 37 37\n", "20\n1 13 12 7 25 25 46 39 39 18 25 18 8 8 18 42 29 34 34 3\n", "20\n39 38 36 36 37 39 39 19 18 18 20 31 31 15 15 16 16 16 48 48\n", "20\n34 34 35 15 14 14 12 12 18 24 24 25 4 4 14 14 14 14 37 36\n", "20\n34 20 41 21 45 30 4 42 42 21 21 41 43 36 25 49 25 25 44 28\n", "20\n24 20 23 33 22 18 42 47 47 50 38 37 27 12 25 24 24 43 17 24\n", "20\n27 20 19 19 20 19 19 22 48 48 49 35 33 33 33 33 47 46 45 45\n", "20\n34 42 41 41 20 29 37 46 7 37 31 20 20 26 26 34 34 2 35 35\n", "20\n40 34 34 35 35 35 37 34 34 34 32 32 33 36 35 33 33 34 35 35\n", "20\n33 31 31 32 42 42 29 35 33 32 32 34 28 28 5 37 2 44 44 4\n", "20\n25 4 11 48 5 4 4 34 20 20 7 28 43 43 12 10 5 33 33 34\n", "20\n18 18 50 31 47 25 14 13 17 14 37 5 50 41 41 8 9 41 49 13\n", "20\n7 6 6 7 4 4 3 2 2 4 4 4 5 1 1 1 1 1 1 2\n", "20\n15 15 3 35 35 11 11 22 22 22 22 26 39 39 38 38 23 23 24 24\n", "20\n10 7 13 16 39 9 25 25 25 24 24 44 44 11 14 40 28 28 50 19\n", "20\n1 3 45 28 20 35 15 1 39 4 34 17 17 18 29 34 20 23 28 47\n", "20\n42 42 42 42 40 40 40 40 39 39 39 39 40 40 42 41 41 4 4 5\n", "20\n21 21 21 21 11 11 37 36 36 38 10 27 27 26 26 27 29 9 1 28\n", "7\n3 2 2 2 2 2 3\n", "4\n1000 1000 1000 1000\n", "50\n1 1 1 1 1 2 1 1 2 1 2 1 1 1 2 1 1 1 2 2 2 1 1 2 2 1 2 1 1 2 2 1 1 2 2 1 1 1 1 1 2 2 2 2 2 1 2 2 2 1\n", "11\n4 3 3 3 3 3 5 6 7 8 9\n", "50\n1 1 1 1 1 2 1 1 2 1 2 1 1 1 2 1 1 1 2 2 2 1 1 2 2 1 2 1 1 2 2 1 1 2 2 1 1 1 1 1 2 2 2 2 2 1 2 2 2 1\n", "4\n1000 1000 1000 1000\n", "20\n1 13 12 7 25 25 46 39 39 18 25 18 8 8 18 42 29 34 34 3\n", "20\n34 20 41 21 45 30 4 42 42 21 21 41 43 36 25 49 25 25 44 28\n", "20\n21 21 21 21 11 11 37 36 36 38 10 27 27 26 26 27 29 9 1 28\n", "20\n42 42 42 42 40 40 40 40 39 39 39 39 40 40 42 41 41 4 4 5\n", "7\n3 2 2 2 2 2 3\n", "20\n6 9 29 29 1 1 2 8 9 2 13 37 37 37 37 24 14 17 37 37\n", "20\n1 3 45 28 20 35 15 1 39 4 34 17 17 18 29 34 20 23 28 47\n", "20\n10 7 13 16 39 9 25 25 25 24 24 44 44 11 14 40 28 28 50 19\n", "20\n40 34 34 35 35 35 37 34 34 34 32 32 33 36 35 33 33 34 35 35\n", "15\n17 17 18 17 16 16 17 17 15 15 13 12 12 14 15\n", "20\n24 20 23 33 22 18 42 47 47 50 38 37 27 12 25 24 24 43 17 24\n", "15\n67 67 65 65 66 66 66 68 67 67 67 65 65 66 70\n", "20\n34 34 35 15 14 14 12 12 18 24 24 25 4 4 14 14 14 14 37 36\n", "20\n18 18 50 31 47 25 14 13 17 14 37 5 50 41 41 8 9 41 49 13\n", "20\n25 4 11 48 5 4 4 34 20 20 7 28 43 43 12 10 5 33 33 34\n", "20\n10 6 25 38 8 4 22 40 28 45 23 33 18 39 28 26 40 4 14 47\n", "20\n39 38 36 36 37 39 39 19 18 18 20 31 31 15 15 16 16 16 48 48\n", "20\n15 15 3 35 35 11 11 22 22 22 22 26 39 39 38 38 23 23 24 24\n", "20\n7 6 6 7 4 4 3 2 2 4 4 4 5 1 1 1 1 1 1 2\n", "20\n27 20 19 19 20 19 19 22 48 48 49 35 33 33 33 33 47 46 45 45\n", "20\n34 42 41 41 20 29 37 46 7 37 31 20 20 26 26 34 34 2 35 35\n", "20\n6 5 5 7 12 12 12 12 7 41 35 28 28 28 28 13 12 12 7 6\n", "20\n33 31 31 32 42 42 29 35 33 32 32 34 28 28 5 37 2 44 44 4\n", "11\n4 3 3 3 3 3 5 6 7 8 9\n", "50\n1 1 1 1 1 2 1 1 2 1 4 1 1 1 2 1 1 1 2 2 2 1 1 2 2 1 2 1 1 2 2 1 1 2 2 1 1 1 1 1 2 2 2 2 2 1 2 2 2 1\n", "20\n1 13 12 7 25 25 46 39 39 18 25 18 8 8 18 42 37 34 34 3\n", "20\n34 20 41 21 45 30 4 80 42 21 21 41 43 36 25 49 25 25 44 28\n", "20\n21 21 21 21 11 11 37 36 36 38 15 27 27 26 26 27 29 9 1 28\n", "20\n6 9 29 29 1 1 2 8 9 2 13 37 13 37 37 24 14 17 37 37\n", "20\n1 7 13 16 39 9 25 25 25 24 24 44 44 11 14 40 28 28 50 19\n", "20\n40 34 34 35 35 35 37 34 34 34 32 32 33 36 19 33 33 34 35 35\n", "15\n17 17 18 17 16 16 17 17 15 4 13 12 12 14 15\n", "20\n24 20 23 33 22 18 59 47 47 50 38 37 27 12 25 24 24 43 17 24\n", "15\n67 67 65 65 66 66 66 68 67 67 67 65 65 66 20\n", "20\n34 34 35 15 14 14 12 12 18 24 24 25 4 4 14 14 13 14 37 36\n", "20\n33 31 31 32 42 42 29 35 33 32 32 11 28 28 5 37 2 44 44 4\n", "11\n4 3 3 3 6 3 5 6 7 8 9\n", "20\n34 20 41 21 45 30 4 80 42 21 21 41 43 36 25 49 25 7 44 28\n", "20\n42 42 42 42 40 40 40 40 52 39 39 39 40 40 42 18 41 4 4 5\n", "15\n67 67 65 65 66 35 66 68 67 67 67 65 65 66 20\n", "20\n33 31 31 32 65 42 29 35 33 32 32 11 28 28 5 37 2 44 44 4\n", "7\n5 3 4 7 4 3 3\n", "20\n42 42 42 42 40 40 40 40 52 39 39 39 40 40 42 41 41 4 4 5\n", "20\n1 3 45 28 20 35 15 1 39 4 57 17 17 18 29 34 20 23 28 47\n", "20\n18 18 50 31 47 25 14 13 17 14 37 5 94 41 41 8 9 41 49 13\n", "20\n25 4 11 2 5 4 4 34 20 20 7 28 43 43 12 10 5 33 33 34\n", "20\n10 6 13 38 8 4 22 40 28 45 23 33 18 39 28 26 40 4 14 47\n", "20\n39 74 36 36 37 39 39 19 18 18 20 31 31 15 15 16 16 16 48 48\n", "20\n27 20 19 19 20 19 19 22 48 48 49 35 33 33 33 33 47 46 45 73\n", "20\n34 42 41 41 20 29 66 46 7 37 31 20 20 26 26 34 34 2 35 35\n", "20\n6 5 5 7 12 12 12 12 7 41 35 28 28 28 28 13 12 22 7 6\n", "7\n3 3 4 7 4 3 3\n", "5\n4 3 2 3 3\n", "3\n2 3 5\n", "20\n1 13 12 7 25 25 46 39 39 18 25 18 8 8 18 36 37 34 34 3\n", "20\n6 9 29 29 1 1 2 8 9 2 13 37 13 37 37 24 14 29 37 37\n", "20\n1 3 45 28 20 35 10 1 39 4 57 17 17 18 29 34 20 23 28 47\n", "20\n1 7 13 16 39 9 25 25 25 24 24 44 44 11 14 40 28 34 50 19\n", "20\n40 34 34 35 35 35 37 34 34 34 32 32 33 36 19 63 33 34 35 35\n", "15\n17 17 18 17 16 15 17 17 15 4 13 12 12 14 15\n", "20\n24 20 23 33 22 18 59 47 47 50 38 37 27 12 25 24 9 43 17 24\n", "20\n34 34 35 15 14 14 12 12 18 24 24 16 4 4 14 14 13 14 37 36\n", "20\n18 18 50 31 47 25 14 13 17 14 37 5 94 41 41 8 9 41 23 13\n", "20\n25 4 11 2 5 4 4 34 20 3 7 28 43 43 12 10 5 33 33 34\n", "20\n10 6 13 38 8 4 22 40 28 45 23 33 18 39 20 26 40 4 14 47\n", "20\n29 74 36 36 37 39 39 19 18 18 20 31 31 15 15 16 16 16 48 48\n", "20\n27 19 19 19 20 19 19 22 48 48 49 35 33 33 33 33 47 46 45 73\n", "20\n34 42 41 41 20 29 66 46 7 37 31 20 20 26 26 34 13 2 35 35\n", "20\n6 5 5 7 12 12 12 12 7 41 46 28 28 28 28 13 12 22 7 6\n", "5\n8 3 2 3 3\n", "3\n2 3 1\n", "20\n34 20 41 21 45 30 4 80 42 21 21 41 43 36 6 49 25 7 44 28\n", "20\n42 42 42 42 40 9 40 40 52 39 39 39 40 40 42 18 41 4 4 5\n", "20\n6 9 29 29 1 1 2 8 9 2 13 37 10 37 37 24 14 29 37 37\n", "20\n1 3 45 28 20 35 10 1 64 4 57 17 17 18 29 34 20 23 28 47\n", "20\n1 7 13 16 39 9 25 25 25 24 24 46 44 11 14 40 28 34 50 19\n", "20\n3 34 34 35 35 35 37 34 34 34 32 32 33 36 19 63 33 34 35 35\n", "15\n8 17 18 17 16 15 17 17 15 4 13 12 12 14 15\n", "20\n24 20 23 33 22 18 59 47 3 50 38 37 27 12 25 24 9 43 17 24\n", "15\n67 67 65 65 66 35 66 68 67 67 67 65 65 66 40\n", "20\n34 34 35 15 11 14 12 12 18 24 24 16 4 4 14 14 13 14 37 36\n", "20\n18 18 50 31 47 25 14 13 17 14 34 5 94 41 41 8 9 41 23 13\n", "20\n25 4 11 2 5 4 4 34 20 3 7 28 43 43 12 10 5 7 33 34\n", "20\n10 6 13 38 8 6 22 40 28 45 23 33 18 39 20 26 40 4 14 47\n", "20\n29 74 36 36 37 39 62 19 18 18 20 31 31 15 15 16 16 16 48 48\n", "20\n27 19 19 19 20 19 19 22 48 48 49 35 33 33 3 33 47 46 45 73\n", "20\n34 42 41 41 20 29 66 46 7 37 31 20 20 26 6 34 13 2 35 35\n", "20\n6 5 5 7 12 12 12 9 7 41 46 28 28 28 28 13 12 22 7 6\n", "20\n33 31 31 32 65 42 29 35 33 32 32 18 28 28 5 37 2 44 44 4\n", "7\n8 3 4 7 4 3 3\n", "20\n34 20 41 21 45 30 4 80 42 21 21 41 43 36 6 49 25 7 44 29\n", "20\n42 42 42 42 40 9 40 40 52 39 39 39 40 40 42 18 41 4 1 5\n", "20\n6 9 29 29 1 1 2 8 9 2 17 37 10 37 37 24 14 29 37 37\n", "20\n1 3 45 28 20 35 10 1 64 4 57 17 30 18 29 34 20 23 28 47\n", "20\n1 7 13 16 39 9 25 10 25 24 24 46 44 11 14 40 28 34 50 19\n", "20\n3 34 64 35 35 35 37 34 34 34 32 32 33 36 19 63 33 34 35 35\n", "15\n8 17 18 17 16 15 17 17 15 4 7 12 12 14 15\n", "20\n24 20 23 33 22 18 59 47 3 50 38 37 27 12 25 24 15 43 17 24\n", "15\n67 67 49 65 66 35 66 68 67 67 67 65 65 66 40\n", "20\n34 34 35 15 11 14 12 12 18 24 24 16 4 4 14 14 13 14 5 36\n", "20\n14 18 50 31 47 25 14 13 17 14 34 5 94 41 41 8 9 41 23 13\n", "20\n25 4 5 2 5 4 4 34 20 3 7 28 43 43 12 10 5 7 33 34\n", "20\n10 6 18 38 8 6 22 40 28 45 23 33 18 39 20 26 40 4 14 47\n", "20\n29 74 36 36 37 39 62 19 18 18 20 31 31 15 15 16 6 16 48 48\n", "20\n27 19 19 19 20 19 19 22 48 48 49 35 33 33 3 33 47 46 45 9\n", "20\n6 5 5 7 12 4 12 9 7 41 46 28 28 28 28 13 12 22 7 6\n", "20\n33 31 33 32 65 42 29 35 33 32 32 18 28 28 5 37 2 44 44 4\n", "7\n8 6 4 7 4 3 3\n", "20\n34 20 51 21 45 30 4 80 42 21 21 41 43 36 6 49 25 7 44 29\n", "20\n42 42 42 84 40 9 40 40 52 39 39 39 40 40 42 18 41 4 1 5\n", "20\n6 9 29 29 1 1 2 8 4 2 17 37 10 37 37 24 14 29 37 37\n", "20\n1 3 45 28 20 35 10 1 89 4 57 17 30 18 29 34 20 23 28 47\n", "20\n1 7 13 16 39 9 25 10 25 24 24 46 44 11 14 40 28 34 29 19\n", "20\n3 34 64 35 35 35 37 34 34 34 32 32 33 36 19 107 33 34 35 35\n", "15\n8 17 18 17 16 15 17 17 15 4 7 11 12 14 15\n", "20\n24 20 23 33 22 18 59 47 3 50 38 37 27 12 25 24 17 43 17 24\n", "15\n67 67 49 65 66 35 66 68 34 67 67 65 65 66 40\n", "1\n1000\n", "7\n3 3 4 4 4 3 3\n", "5\n4 3 2 2 3\n", "3\n1 3 5\n"], "outputs": ["2\n", "2\n", "3\n", "1\n", "2\n", "2\n", "20\n", "9\n", "13\n", "16\n", "5\n", "9\n", "17\n", "17\n", "5\n", "14\n", "2\n", "10\n", "14\n", "18\n", "2\n", "10\n", "14\n", "18\n", "2\n", "8\n", "3\n", "1\n", "19\n", "3\n", "19\n", "1\n", "16\n", "17\n", "8\n", "2\n", "3\n", "13\n", "18\n", "14\n", "2\n", "2\n", "17\n", "2\n", "9\n", "18\n", "14\n", "20\n", "5\n", "10\n", "2\n", "5\n", "14\n", "9\n", "10\n", "3\n", "20\n", "16\n", "18\n", "8\n", "15\n", "14\n", "6\n", "4\n", "17\n", "3\n", "11\n", "12\n", "9\n", "19\n", "10\n", "7\n", "13\n", "5\n", "8\n", "18\n", "18\n", "14\n", "20\n", "8\n", "8\n", "14\n", "11\n", "3\n", "4\n", "3\n", "16\n", "15\n", "18\n", "15\n", "8\n", "8\n", "19\n", "12\n", "18\n", "15\n", "20\n", "8\n", "11\n", "15\n", "11\n", "4\n", "3\n", "19\n", "12\n", "15\n", "18\n", "16\n", "8\n", "10\n", "20\n", "7\n", "14\n", "18\n", "17\n", "20\n", "9\n", "14\n", "16\n", "13\n", "13\n", "5\n", "19\n", "14\n", "15\n", "20\n", "18\n", "11\n", "13\n", "20\n", "9\n", "14\n", "19\n", "17\n", "20\n", "11\n", "14\n", "14\n", "16\n", "5\n", "19\n", "16\n", "15\n", "20\n", "18\n", "11\n", "14\n", "20\n", "11\n", "1\n", "2\n", "2\n", "3\n"]}
[ "from collections import defaultdict, deque\nfrom heapq import heappush, heappop\nfrom bisect import bisect_left, bisect_right\nimport sys, itertools, math\nsys.setrecursionlimit(10 ** 5)\ninput = sys.stdin.readline\nsqrt = math.sqrt\n\ndef LI():\n\treturn list(map(int, input().split()))\n\ndef LF():\n\treturn list(map(float, input().split()))\n\ndef LI_():\n\treturn list(map(lambda x: int(x) - 1, input().split()))\n\ndef II():\n\treturn int(input())\n\ndef IF():\n\treturn float(input())\n\ndef S():\n\treturn input().rstrip()\n\ndef LS():\n\treturn S().split()\n\ndef IR(n):\n\tres = [None] * n\n\tfor i in range(n):\n\t\tres[i] = II()\n\treturn res\n\ndef LIR(n):\n\tres = [None] * n\n\tfor i in range(n):\n\t\tres[i] = LI()\n\treturn res\n\ndef FR(n):\n\tres = [None] * n\n\tfor i in range(n):\n\t\tres[i] = IF()\n\treturn res\n\ndef LIR(n):\n\tres = [None] * n\n\tfor i in range(n):\n\t\tres[i] = IF()\n\treturn res\n\ndef LIR_(n):\n\tres = [None] * n\n\tfor i in range(n):\n\t\tres[i] = LI_()\n\treturn res\n\ndef SR(n):\n\tres = [None] * n\n\tfor i in range(n):\n\t\tres[i] = S()\n\treturn res\n\ndef LSR(n):\n\tres = [None] * n\n\tfor i in range(n):\n\t\tres[i] = LS()\n\treturn res\nmod = 1000000007\ninf = float('INF')\n\ndef solve():\n\tn = II()\n\ta = LI()\n\tdp = [[None for i in range(n + 1)] for i in range(n + 1)]\n\tfor i in range(n):\n\t\tdp[i][i + 1] = [a[i], a[i], 1]\n\t\tdp[i + 1][i] = [a[i], a[i], 1]\n\tfor i in range(2, n + 1):\n\t\tfor l in range(n - i + 1):\n\t\t\ttmp = [-inf, inf, inf]\n\t\t\tr = l + i\n\t\t\tdpl = dp[l]\n\t\t\tdpr = dp[r]\n\t\t\tfor m in range(l + 1, r):\n\t\t\t\tlm = dpl[m]\n\t\t\t\tmr = dpr[m]\n\t\t\t\tlr = lm[2] + mr[2] - (lm[1] == mr[0])\n\t\t\t\tif lr < tmp[2]:\n\t\t\t\t\ttmp[2] = lr\n\t\t\t\t\tif lm[1] == mr[0]:\n\t\t\t\t\t\tif lm[2] == 1:\n\t\t\t\t\t\t\ttmp[0] = lm[0] + 1\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\ttmp[0] = lm[0]\n\t\t\t\t\t\tif mr[2] == 1:\n\t\t\t\t\t\t\ttmp[1] = mr[1] + 1\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\ttmp[1] = mr[1]\n\t\t\t\t\telse:\n\t\t\t\t\t\ttmp[0] = lm[0]\n\t\t\t\t\t\ttmp[1] = mr[1]\n\t\t\tdp[l][r] = tmp\n\t\t\tdp[r][l] = tmp\n\tprint(dp[0][n][2])\n\treturn\nsolve()\n", "import sys\ninput = sys.stdin.readline\nn = int(input())\nA = list(map(int, input().split()))\nDP = [[-1] * (n + 1) for i in range(n + 1)]\nfor i in range(n):\n\tDP[i][i] = A[i]\nfor mid in range(1, n):\n\tfor i in range(n):\n\t\tj = i + mid\n\t\tif j == n:\n\t\t\tbreak\n\t\tfor k in range(i, j + 1):\n\t\t\tif DP[i][k] == DP[k + 1][j] and DP[i][k] != -1:\n\t\t\t\tDP[i][j] = DP[i][k] + 1\nANS = [2000] * (n + 1)\nANS.append(0)\nfor i in range(n):\n\tANS[i] = min(ANS[i], ANS[i - 1] + 1)\n\tfor j in range(i, n):\n\t\tif DP[i][j] != -1:\n\t\t\tANS[j] = min(ANS[j], ANS[i - 1] + 1)\nprint(ANS[n - 1])\n", "n = int(input())\nb = [int(_) for _ in input().split()]\ne = [[-1] * (n + 1) for _ in range(2024)]\nd = [[] for _ in range(n)]\nfor (j, v) in enumerate(b):\n\te[v][j] = j\n\td[j].append(j)\nfor v in range(1, 2024):\n\tfor i in range(n):\n\t\tj = e[v][i]\n\t\th = e[v][j + 1] if j != -1 else -1\n\t\tif j != -1 and h != -1:\n\t\t\te[v + 1][i] = h\n\t\t\td[i].append(h)\na = [_ for _ in range(1, n + 1)]\nfor s in range(n):\n\tfor e in d[s]:\n\t\ta[e] = min(a[e], a[s - 1] + 1 if s > 0 else 1)\nprint(a[n - 1])\n", "n = int(input())\na = list(map(int, input().split()))\ndp = [[505] * n for _ in range(n)]\nMax = [[0] * n for _ in range(n)]\nfor i in range(n):\n\tdp[i][i] = 1\n\tMax[i][i] = a[i]\nfor len in range(1, n + 1):\n\tfor i in range(n - len + 1):\n\t\tj = i + len - 1\n\t\tfor k in range(i, j):\n\t\t\tdp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j])\n\t\t\tif dp[i][k] == 1 and dp[k + 1][j] == 1 and (Max[i][k] == Max[k + 1][j]):\n\t\t\t\tdp[i][j] = 1\n\t\t\t\tMax[i][j] = Max[i][k] + 1\nprint(dp[0][n - 1])\n", "import sys, math\nimport io, os\nfrom bisect import bisect_left as bl, bisect_right as br, insort\nfrom collections import defaultdict as dd, deque, Counter\n\ndef data():\n\treturn sys.stdin.readline().strip()\n\ndef mdata():\n\treturn list(map(int, data().split()))\n\ndef outl(var):\n\tsys.stdout.write(' '.join(map(str, var)) + '\\n')\n\ndef out(var):\n\tsys.stdout.write(str(var) + '\\n')\nfrom decimal import Decimal\nINF = float('inf')\nmod = int(1000000000.0) + 7\n\ndef cal(l, r):\n\tif l == r:\n\t\tdp1[l][r] = 1\n\t\tdp2[l][r] = a[l]\n\tif dp1[l][r]:\n\t\treturn dp1[l][r]\n\tfor i in range(l, r):\n\t\tif cal(l, i) == 1 and cal(i + 1, r) == 1 and (dp2[l][i] == dp2[i + 1][r]):\n\t\t\tdp1[l][r] = 1\n\t\t\tdp2[l][r] = dp2[l][i] + 1\n\tif not dp2[l][r]:\n\t\tdp1[l][r] = 2\n\treturn dp1[l][r]\n\ndef cal2(l, r):\n\tif dp1[l][r] == 1:\n\t\tdp3[l][r] = 1\n\t\treturn 1\n\telif dp3[l][r]:\n\t\treturn dp3[l][r]\n\tans = INF\n\tfor i in range(l, r):\n\t\tans = min(cal2(l, i) + cal2(i + 1, r), ans)\n\tdp3[l][r] = ans\n\treturn ans\nn = int(data())\na = mdata()\nans = [n]\ndp1 = [[0] * n for i in range(n)]\ndp2 = [[0] * n for i in range(n)]\ndp3 = [[0] * n for i in range(n)]\ncal(0, n - 1)\ncal2(0, n - 1)\nout(dp3[0][n - 1])\n", "n = int(input())\nb = list(map(int, input().split(' ')))\ne = [[-1] * (n + 1) for _ in range(2048)]\nd = [[] for _ in range(n)]\nfor (i, v) in enumerate(b):\n\te[v][i] = i\n\td[i].append(i)\nfor v in range(1, 2048):\n\tfor i in range(n):\n\t\tj = e[v][i]\n\t\tif j != -1:\n\t\t\th = e[v][j + 1]\n\t\telse:\n\t\t\th = -1\n\t\tif j != -1 and h != -1:\n\t\t\te[v + 1][i] = h\n\t\t\td[i].append(h)\na = [_ for _ in range(1, n + 1)]\nfor s in range(n):\n\tfor e in d[s]:\n\t\tif s > 0:\n\t\t\ttemp = a[s - 1] + 1\n\t\telse:\n\t\t\ttemp = 1\n\t\ta[e] = min(a[e], temp)\nprint(a[n - 1])\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nfrom math import gcd, ceil\n\ndef prod(a, mod=10 ** 9 + 7):\n\tans = 1\n\tfor each in a:\n\t\tans = ans * each % mod\n\treturn ans\n\ndef lcm(a, b):\n\treturn a * b // gcd(a, b)\n\ndef binary(x, length=16):\n\ty = bin(x)[2:]\n\treturn y if len(y) >= length else '0' * (length - len(y)) + y\nfor _ in range(int(input()) if not True else 1):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tdp = [[False] * (n + 2) for i in range(n + 2)]\n\tdp2 = [[600] * (n + 2) for i in range(n + 2)]\n\tfor i in range(n):\n\t\tdp[i][i] = a[i]\n\t\tdp2[i][i] = 1\n\tfor diff in range(1, n):\n\t\tfor i in range(n - diff):\n\t\t\tfor j in range(i, i + diff):\n\t\t\t\tif dp[i][j] == dp[j + 1][i + diff] and dp[i][j]:\n\t\t\t\t\tdp[i][i + diff] = dp[i][j] + 1\n\t\t\t\t\tdp2[i][i + diff] = 1\n\t\t\t\tdp2[i][i + diff] = min(dp2[i][i + diff], dp2[i][j] + dp2[j + 1][i + diff])\n\t\t\tif not dp2[i][i + diff]:\n\t\t\t\tdp2[i][i + diff] = min(dp2[i + 1][i + diff] + 1, dp2[i][i + diff - 1] + 1)\n\tprint(dp2[0][n - 1])\n", "N = int(input())\nX = list(map(int, input().split()))\nfrom collections import defaultdict\ndp1 = defaultdict(lambda : -1)\nM = 1001\n\ndef ec(i, j):\n\treturn i * M + j\nfor i in range(N):\n\tdp1[ec(i, i + 1)] = X[i]\nfor i in range(2, N + 1):\n\tfor j in range(N - i + 1):\n\t\tfor k in range(1, i):\n\t\t\t(u, v) = (dp1[ec(j, j + k)], dp1[ec(j + k, j + i)])\n\t\t\tif u != -1 and v != -1 and (u == v):\n\t\t\t\tdp1[ec(j, j + i)] = u + 1\n\t\t\t\tbreak\ndp2 = [0] * (N + 1)\nfor i in range(N):\n\tdp2[i + 1] = dp2[i] + 1\n\tfor j in range(i + 1):\n\t\tif dp1[ec(j, i + 1)] == -1:\n\t\t\tcontinue\n\t\tdp2[i + 1] = min(dp2[i + 1], dp2[j] + 1)\nprint(dp2[-1])\n", "import sys\ninput = lambda : sys.stdin.readline().rstrip()\nimport copy\nn = int(input())\nA = [int(i) for i in input().split()]\ninf = float('inf')\nDP = [[inf] * (n + 1) for _ in range(n + 1)]\nfor j in range(1, n + 1):\n\tfor i in range(n):\n\t\tif i + j > n:\n\t\t\tcontinue\n\t\telif j == 1:\n\t\t\tDP[i][i + 1] = A[i]\n\t\telse:\n\t\t\tfor k in range(i + 1, i + j):\n\t\t\t\tif DP[i][k] < 10000 and DP[k][i + j] < 10000:\n\t\t\t\t\tif DP[i][k] == DP[k][i + j]:\n\t\t\t\t\t\tDP[i][i + j] = DP[i][k] + 1\n\t\t\t\t\telse:\n\t\t\t\t\t\tDP[i][i + j] = 20000\n\t\t\t\telif DP[i][k] < 10000:\n\t\t\t\t\tDP[i][i + j] = min(DP[i][i + j], 10000 + DP[k][i + j])\n\t\t\t\telif DP[k][i + j] < 10000:\n\t\t\t\t\tDP[i][i + j] = min(DP[i][i + j], DP[i][k] + 10000)\n\t\t\t\telse:\n\t\t\t\t\tDP[i][i + j] = min(DP[i][i + j], DP[i][k] + DP[k][i + j])\nprint(DP[0][n] // 10000 if DP[0][n] >= 10000 else 1)\n", "N = int(input())\nX = list(map(int, input().split()))\nfrom collections import defaultdict\ndp = defaultdict(lambda : -1)\nM = 1000001\nfor i in range(N):\n\tdp[i + M] = X[i]\nfor i in range(2, N + 1):\n\tfor j in range(N - i + 1):\n\t\tfor k in range(1, i):\n\t\t\t(u, v) = (dp[j + M * k], dp[j + k + M * (i - k)])\n\t\t\tif u == -1 or v == -1 or u != v:\n\t\t\t\tcontinue\n\t\t\tdp[j + M * i] = u + 1\n\t\t\tbreak\ndp2 = [0] * (N + 1)\nfor i in range(N):\n\tdp2[i + 1] = dp2[i] + 1\n\tfor j in range(i + 1):\n\t\tif dp[j + (i + 1 - j) * M] == -1:\n\t\t\tcontinue\n\t\tdp2[i + 1] = min(dp2[i + 1], dp2[j] + 1)\nprint(dp2[-1])\n", "N = int(input())\nL = list(map(int, input().split()))\nDP = [[-1] * N for i in range(N)]\nfor d in range(N):\n\tfor s in range(N - d):\n\t\te = s + d\n\t\tif s == e:\n\t\t\tDP[s][e] = L[s]\n\t\t\tcontinue\n\t\tfor m in range(s, e):\n\t\t\tl = DP[s][m]\n\t\t\tr = DP[m + 1][e]\n\t\t\tif l == r and l != -1:\n\t\t\t\tDP[s][e] = max(DP[s][e], l + 1)\nDP2 = [i + 1 for i in range(N)]\nfor i in range(N):\n\tif DP[0][i] != -1:\n\t\tDP2[i] = 1\n\t\tcontinue\n\tfor j in range(i):\n\t\tif DP[j + 1][i] != -1:\n\t\t\tDP2[i] = min(DP2[i], DP2[j] + 1)\nprint(DP2[N - 1])\n", "printn = lambda x: print(x, end='')\ninn = lambda : int(input())\ninl = lambda : list(map(int, input().split()))\ninm = lambda : map(int, input().split())\nins = lambda : input().strip()\nDBG = True and False\nBIG = 10 ** 18\nR = 10 ** 9 + 7\n\ndef ddprint(x):\n\tif DBG:\n\t\tprint(x)\n\ndef dp(l, r):\n\tif dpa[l][r] >= 0:\n\t\treturn dpa[l][r]\n\telif l == r:\n\t\tdpa[l][r] = a[l]\n\telse:\n\t\tdpa[l][r] = 0\n\t\tfor j in range(l, r):\n\t\t\tx = dp(l, j)\n\t\t\ty = dp(j + 1, r)\n\t\t\tif 0 < x == y:\n\t\t\t\tdpa[l][r] = x + 1\n\treturn dpa[l][r]\nn = inn()\na = inl()\ndpa = [[-1] * (n + 1) for i in range(n + 1)]\ndp2 = [BIG] * (n + 1)\ndp2[0] = 1\nfor i in range(n):\n\tif dp(0, i) > 0:\n\t\tdp2[i] = 1\n\telse:\n\t\tmn = i + 1\n\t\tfor j in range(i):\n\t\t\tx = dp(j + 1, i)\n\t\t\tif 0 < x:\n\t\t\t\tmn = min(mn, dp2[j] + 1)\n\t\tdp2[i] = mn\nfor i in range(n):\n\tddprint(dpa[i])\nddprint(dp2)\nprint(dp2[n - 1])\n", "import sys\ninput = sys.stdin.readline\nn = int(input())\na = list(map(int, input().split()))\ndp = [[1000] * (n + 1) for i in range(n + 1)]\nval = [[0] * (n + 1) for i in range(n + 1)]\nfor i in range(n):\n\tdp[i][i + 1] = 1\n\tval[i][i + 1] = a[i]\nfor p in range(2, n + 1):\n\tfor i in range(n - p + 1):\n\t\tj = i + p\n\t\tfor k in range(i + 1, j):\n\t\t\tif dp[i][k] == dp[k][j] == 1 and val[i][k] == val[k][j]:\n\t\t\t\tdp[i][j] = 1\n\t\t\t\tval[i][j] = val[i][k] + 1\n\t\t\telse:\n\t\t\t\tdp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])\nprint(dp[0][n])\n", "rr = lambda : input().rstrip()\nrri = lambda : int(rr())\nrrm = lambda : list(map(int, rr().split()))\nfrom functools import lru_cache\nmemo = lru_cache(None)\nfrom sys import setrecursionlimit as srl\nsrl(10 ** 5)\n\ndef solve(N, A):\n\n\t@memo\n\tdef dp(i, j, left=0):\n\t\tif i == j:\n\t\t\tif left == 0:\n\t\t\t\treturn 1\n\t\t\tif A[i] == left:\n\t\t\t\treturn 1\n\t\t\treturn 2\n\t\tif i > j:\n\t\t\treturn 0 if left == 0 else 1\n\t\tans = 1 + dp(i + 1, j, A[i])\n\t\tif left >= 1:\n\t\t\tstack = []\n\t\t\tfor k in range(i, j + 1):\n\t\t\t\tstack.append(A[k])\n\t\t\t\twhile len(stack) >= 2 and stack[-1] == stack[-2]:\n\t\t\t\t\tstack.pop()\n\t\t\t\t\tstack[-1] += 1\n\t\t\t\tif len(stack) == 1 and left == stack[-1]:\n\t\t\t\t\tcand = dp(k + 1, j, left + 1)\n\t\t\t\t\tif cand < ans:\n\t\t\t\t\t\tans = cand\n\t\treturn ans\n\treturn dp(1, N - 1, A[0])\nprint(solve(rri(), rrm()))\n", "n = int(input())\na = list(map(int, input().split()))\ngrip = [[-1] * (n - i) for i in range(n)]\ngrip[0] = a.copy()\nfor level in range(1, n):\n\tfor left in range(n - level):\n\t\tfor split in range(level):\n\t\t\tpl = grip[level - split - 1][left]\n\t\t\tpr = grip[split][left + level - split]\n\t\t\tif pl == pr != -1:\n\t\t\t\tgrip[level][left] = pl + 1\npref = [0] * (n + 1)\nfor p in range(1, n + 1):\n\tx = n\n\tfor j in range(p):\n\t\tl = pref[j]\n\t\tr = grip[p - j - 1][j]\n\t\tif r == -1:\n\t\t\tr = p - j\n\t\telse:\n\t\t\tr = 1\n\t\tx = min(x, l + r)\n\tpref[p] = x\nprint(pref[-1])\n", "import os, sys\nfrom io import BytesIO, IOBase\nfrom math import inf\n\ndef main():\n\tn = int(input())\n\ta = [0] + list(map(int, input().split()))\n\tdp = [[[-1, 0] for _ in range(n + 1)] for _ in range(n + 1)]\n\tfor i in range(1, n + 1):\n\t\t(dp[i][i][0], dp[i][i][1]) = (a[i], 1)\n\tfor i in range(n - 1, 0, -1):\n\t\tfor j in range(i + 1, n + 1):\n\t\t\tfor k in range(j - i):\n\t\t\t\t(a, b) = (dp[i][i + k], dp[i + k + 1][j])\n\t\t\t\tif a[1] and b[1] and (a[0] == b[0]):\n\t\t\t\t\t(dp[i][j][0], dp[i][j][1]) = (a[0] + 1, 1)\n\t\t\t\t\tbreak\n\tval = [0, 0] + [inf] * n\n\tfor i in range(1, n + 1):\n\t\tfor j in range(1, n + 1):\n\t\t\tif dp[i][j][1]:\n\t\t\t\tval[j + 1] = min(val[j + 1], val[i] + 1)\n\tprint(val[-1])\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nmain()\n", "import sys\ndp = []\na = []\n\ndef calcdp(l, r):\n\tglobal dp, a\n\tif l + 1 == r:\n\t\tdp[l][r] = a[l]\n\t\treturn dp[l][r]\n\tif dp[l][r] != 0:\n\t\treturn dp[l][r]\n\tdp[l][r] = -1\n\tfor k in range(l + 1, r):\n\t\tla = calcdp(l, k)\n\t\tra = calcdp(k, r)\n\t\tif la > 0 and la == ra:\n\t\t\tdp[l][r] = la + 1\n\treturn dp[l][r]\n\ndef solve(n):\n\tdp2 = [float('inf')] * (n + 1)\n\tdp2[0] = 0\n\tfor i in range(n):\n\t\tfor j in range(i + 1, n + 1):\n\t\t\tif calcdp(i, j) > 0:\n\t\t\t\tdp2[j] = min(dp2[j], dp2[i] + 1)\n\treturn dp2[n]\n\ndef ip():\n\tglobal dp, a\n\tn = int(sys.stdin.readline())\n\ta = list(map(int, sys.stdin.readline().split()))\n\ta.append(0)\n\tdp = []\n\tll = [0] * (n + 1)\n\tfor _ in range(n + 1):\n\t\tdp.append(list(ll))\n\tprint(solve(n))\nip()\n", "N = int(input())\narr = list(map(int, input().split()))\ndp = [[-1 for x in range(N)] for y in range(N)]\nfor size in range(1, N + 1):\n\tfor i in range(N - size + 1):\n\t\tj = i + size - 1\n\t\tif i == j:\n\t\t\tdp[i][j] = arr[i]\n\t\telse:\n\t\t\tfor k in range(i, j):\n\t\t\t\tif dp[i][k] != -1 and dp[i][k] == dp[k + 1][j]:\n\t\t\t\t\tdp[i][j] = dp[i][k] + 1\ndp2 = [x + 1 for x in range(N)]\nfor i in range(N):\n\tfor k in range(i + 1):\n\t\tif dp[k][i] != -1:\n\t\t\tif k == 0:\n\t\t\t\tdp2[i] = 1\n\t\t\telse:\n\t\t\t\tdp2[i] = min(dp2[i], dp2[k - 1] + 1)\nprint(dp2[N - 1])\n", "n = int(input())\na = list(map(int, input().split()))\ndp = [[False] * (n + 1) for i in range(n + 1)]\n\ndef solve(l, r):\n\tif dp[l][r]:\n\t\treturn dp[l][r]\n\tif r - l == 1:\n\t\tdp[l][r] = (a[l], 1)\n\t\treturn dp[l][r]\n\ttmp = 10 ** 9\n\tfor i in range(l + 1, r):\n\t\tif solve(l, i)[0] == -1 or solve(i, r)[0] == -1:\n\t\t\ttmp = min(tmp, dp[l][i][1] + dp[i][r][1])\n\t\telif solve(l, i) == solve(i, r):\n\t\t\ttmp = solve(l, i)[0] + 1\n\t\t\tdp[l][r] = (tmp, 1)\n\t\t\treturn dp[l][r]\n\t\telse:\n\t\t\ttmp = min(tmp, 2)\n\tdp[l][r] = (-1, tmp)\n\treturn dp[l][r]\nsolve(0, n)\nprint(dp[0][n][1])\n", "import sys\ninput = sys.stdin.readline\nN = int(input())\nA = list(map(int, input().split()))\ndp = [[-1] * (N + 1) for _ in range(N + 1)]\nfor l in range(N):\n\tdp[l][l + 1] = A[l]\nfor d in range(2, N + 1):\n\tfor l in range(N - d + 1):\n\t\tfor t in range(1, d):\n\t\t\tif dp[l][l + t] == dp[l + t][l + d] and dp[l][l + t] != -1:\n\t\t\t\tdp[l][l + d] = dp[l][l + t] + 1\n\t\t\t\tbreak\ndp2 = [i for i in range(N + 1)]\nfor r in range(1, N + 1):\n\tif dp[0][r] != -1:\n\t\tdp2[r] = 1\nfor l in range(N):\n\tfor r in range(l + 2, N + 1):\n\t\tif dp[l + 1][r] != -1:\n\t\t\tdp2[r] = min(dp2[l + 1] + 1, dp2[r])\n\t\telse:\n\t\t\tdp2[r] = min(dp2[l + 1] + (r - l - 1), dp2[r])\nprint(dp2[N])\n", "from collections import Counter\nfrom collections import defaultdict\nimport math\nimport random\nimport heapq as hq\nfrom math import sqrt\nimport sys\nfrom functools import reduce\n\ndef input():\n\treturn sys.stdin.readline().strip()\n\ndef iinput():\n\treturn int(input())\n\ndef tinput():\n\treturn input().split()\n\ndef rinput():\n\treturn map(int, tinput())\n\ndef rlinput():\n\treturn list(rinput())\nmod = int(1000000000.0) + 7\n\ndef factors(n):\n\treturn set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))\nn = iinput()\na = rlinput()\ndp = [[0 for i in range(n)] for j in range(n)]\nfor i in range(n):\n\tdp[i][i] = a[i]\nfor l in range(n - 2, -1, -1):\n\tfor r in range(l + 1, n):\n\t\tfor k in range(l, r):\n\t\t\tif dp[l][k] == dp[k + 1][r] and dp[l][k] != 0:\n\t\t\t\tdp[l][r] = dp[l][k] + 1\nsqueeze = [float('inf')] * (n + 1)\nsqueeze[0] = 0\nfor i in range(1, n + 1):\n\tfor j in range(i):\n\t\tif dp[j][i - 1] != 0:\n\t\t\tsqueeze[i] = min(squeeze[i], squeeze[j] + 1)\nprint(squeeze[n])\n", "def divisors(M):\n\td = []\n\ti = 1\n\twhile M >= i ** 2:\n\t\tif M % i == 0:\n\t\t\td.append(i)\n\t\t\tif i ** 2 != M:\n\t\t\t\td.append(M // i)\n\t\ti = i + 1\n\treturn d\n\ndef popcount(x):\n\tx = x - (x >> 1 & 1431655765)\n\tx = (x & 858993459) + (x >> 2 & 858993459)\n\tx = x + (x >> 4) & 252645135\n\tx = x + (x >> 8)\n\tx = x + (x >> 16)\n\treturn x & 127\n\ndef eratosthenes(n):\n\tres = [0 for i in range(n + 1)]\n\tprime = set([])\n\tfor i in range(2, n + 1):\n\t\tif not res[i]:\n\t\t\tprime.add(i)\n\t\t\tfor j in range(1, n // i + 1):\n\t\t\t\tres[i * j] = 1\n\treturn prime\n\ndef factorization(n):\n\tres = []\n\tfor p in prime:\n\t\tif n % p == 0:\n\t\t\twhile n % p == 0:\n\t\t\t\tn //= p\n\t\t\tres.append(p)\n\tif n != 1:\n\t\tres.append(n)\n\treturn res\n\ndef euler_phi(n):\n\tres = n\n\tfor x in range(2, n + 1):\n\t\tif x ** 2 > n:\n\t\t\tbreak\n\t\tif n % x == 0:\n\t\t\tres = res // x * (x - 1)\n\t\t\twhile n % x == 0:\n\t\t\t\tn //= x\n\tif n != 1:\n\t\tres = res // n * (n - 1)\n\treturn res\n\ndef ind(b, n):\n\tres = 0\n\twhile n % b == 0:\n\t\tres += 1\n\t\tn //= b\n\treturn res\n\ndef isPrimeMR(n):\n\tif n == 1:\n\t\treturn 0\n\td = n - 1\n\td = d // (d & -d)\n\tL = [2, 3, 5, 7, 11, 13, 17]\n\tfor a in L:\n\t\tt = d\n\t\ty = pow(a, t, n)\n\t\tif y == 1:\n\t\t\tcontinue\n\t\twhile y != n - 1:\n\t\t\ty = y * y % n\n\t\t\tif y == 1 or t == n - 1:\n\t\t\t\treturn 0\n\t\t\tt <<= 1\n\treturn 1\n\ndef findFactorRho(n):\n\tfrom math import gcd\n\tm = 1 << n.bit_length() // 8\n\tfor c in range(1, 99):\n\t\tf = lambda x: (x * x + c) % n\n\t\t(y, r, q, g) = (2, 1, 1, 1)\n\t\twhile g == 1:\n\t\t\tx = y\n\t\t\tfor i in range(r):\n\t\t\t\ty = f(y)\n\t\t\tk = 0\n\t\t\twhile k < r and g == 1:\n\t\t\t\tys = y\n\t\t\t\tfor i in range(min(m, r - k)):\n\t\t\t\t\ty = f(y)\n\t\t\t\t\tq = q * abs(x - y) % n\n\t\t\t\tg = gcd(q, n)\n\t\t\t\tk += m\n\t\t\tr <<= 1\n\t\tif g == n:\n\t\t\tg = 1\n\t\t\twhile g == 1:\n\t\t\t\tys = f(ys)\n\t\t\t\tg = gcd(abs(x - ys), n)\n\t\tif g < n:\n\t\t\tif isPrimeMR(g):\n\t\t\t\treturn g\n\t\t\telif isPrimeMR(n // g):\n\t\t\t\treturn n // g\n\t\t\treturn findFactorRho(g)\n\ndef primeFactor(n):\n\ti = 2\n\tret = {}\n\trhoFlg = 0\n\twhile i * i <= n:\n\t\tk = 0\n\t\twhile n % i == 0:\n\t\t\tn //= i\n\t\t\tk += 1\n\t\tif k:\n\t\t\tret[i] = k\n\t\ti += 1 + i % 2\n\t\tif i == 101 and n >= 2 ** 20:\n\t\t\twhile n > 1:\n\t\t\t\tif isPrimeMR(n):\n\t\t\t\t\t(ret[n], n) = (1, 1)\n\t\t\t\telse:\n\t\t\t\t\trhoFlg = 1\n\t\t\t\t\tj = findFactorRho(n)\n\t\t\t\t\tk = 0\n\t\t\t\t\twhile n % j == 0:\n\t\t\t\t\t\tn //= j\n\t\t\t\t\t\tk += 1\n\t\t\t\t\tret[j] = k\n\tif n > 1:\n\t\tret[n] = 1\n\tif rhoFlg:\n\t\tret = {x: ret[x] for x in sorted(ret)}\n\treturn ret\n\ndef divisors(n):\n\tres = [1]\n\tprime = primeFactor(n)\n\tfor p in prime:\n\t\tnewres = []\n\t\tfor d in res:\n\t\t\tfor j in range(prime[p] + 1):\n\t\t\t\tnewres.append(d * p ** j)\n\t\tres = newres\n\tres.sort()\n\treturn res\n\ndef xorfactorial(num):\n\tif num == 0:\n\t\treturn 0\n\telif num == 1:\n\t\treturn 1\n\telif num == 2:\n\t\treturn 3\n\telif num == 3:\n\t\treturn 0\n\telse:\n\t\tx = baseorder(num)\n\t\treturn 2 ** x * ((num - 2 ** x + 1) % 2) + function(num - 2 ** x)\n\ndef xorconv(n, X, Y):\n\tif n == 0:\n\t\tres = [X[0] * Y[0] % mod]\n\t\treturn res\n\tx = [digit[i] + X[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]\n\ty = [Y[i] + Y[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]\n\tz = [digit[i] - X[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]\n\tw = [Y[i] - Y[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]\n\tres1 = xorconv(n - 1, x, y)\n\tres2 = xorconv(n - 1, z, w)\n\tformer = [(res1[i] + res2[i]) * inv for i in range(2 ** (n - 1))]\n\tlatter = [(res1[i] - res2[i]) * inv for i in range(2 ** (n - 1))]\n\tformer = list(map(lambda x: x % mod, former))\n\tlatter = list(map(lambda x: x % mod, latter))\n\treturn former + latter\n\ndef merge_sort(A, B):\n\t(pos_A, pos_B) = (0, 0)\n\t(n, m) = (len(A), len(B))\n\tres = []\n\twhile pos_A < n and pos_B < m:\n\t\t(a, b) = (A[pos_A], B[pos_B])\n\t\tif a < b:\n\t\t\tres.append(a)\n\t\t\tpos_A += 1\n\t\telse:\n\t\t\tres.append(b)\n\t\t\tpos_B += 1\n\tres += A[pos_A:]\n\tres += B[pos_B:]\n\treturn res\n\nclass UnionFindVerSize:\n\n\tdef __init__(self, N):\n\t\tself._parent = [n for n in range(0, N)]\n\t\tself._size = [1] * N\n\t\tself.group = N\n\n\tdef find_root(self, x):\n\t\tif self._parent[x] == x:\n\t\t\treturn x\n\t\tself._parent[x] = self.find_root(self._parent[x])\n\t\tstack = [x]\n\t\twhile self._parent[stack[-1]] != stack[-1]:\n\t\t\tstack.append(self._parent[stack[-1]])\n\t\tfor v in stack:\n\t\t\tself._parent[v] = stack[-1]\n\t\treturn self._parent[x]\n\n\tdef unite(self, x, y):\n\t\tgx = self.find_root(x)\n\t\tgy = self.find_root(y)\n\t\tif gx == gy:\n\t\t\treturn\n\t\tself.group -= 1\n\t\tif self._size[gx] < self._size[gy]:\n\t\t\tself._parent[gx] = gy\n\t\t\tself._size[gy] += self._size[gx]\n\t\telse:\n\t\t\tself._parent[gy] = gx\n\t\t\tself._size[gx] += self._size[gy]\n\n\tdef get_size(self, x):\n\t\treturn self._size[self.find_root(x)]\n\n\tdef is_same_group(self, x, y):\n\t\treturn self.find_root(x) == self.find_root(y)\n\nclass WeightedUnionFind:\n\n\tdef __init__(self, N):\n\t\tself.parent = [i for i in range(N)]\n\t\tself.size = [1 for i in range(N)]\n\t\tself.val = [0 for i in range(N)]\n\t\tself.flag = True\n\t\tself.edge = [[] for i in range(N)]\n\n\tdef dfs(self, v, pv):\n\t\tstack = [(v, pv)]\n\t\tnew_parent = self.parent[pv]\n\t\twhile stack:\n\t\t\t(v, pv) = stack.pop()\n\t\t\tself.parent[v] = new_parent\n\t\t\tfor (nv, w) in self.edge[v]:\n\t\t\t\tif nv != pv:\n\t\t\t\t\tself.val[nv] = self.val[v] + w\n\t\t\t\t\tstack.append((nv, v))\n\n\tdef unite(self, x, y, w):\n\t\tif not self.flag:\n\t\t\treturn\n\t\tif self.parent[x] == self.parent[y]:\n\t\t\tself.flag = self.val[x] - self.val[y] == w\n\t\t\treturn\n\t\tif self.size[self.parent[x]] > self.size[self.parent[y]]:\n\t\t\tself.edge[x].append((y, -w))\n\t\t\tself.edge[y].append((x, w))\n\t\t\tself.size[x] += self.size[y]\n\t\t\tself.val[y] = self.val[x] - w\n\t\t\tself.dfs(y, x)\n\t\telse:\n\t\t\tself.edge[x].append((y, -w))\n\t\t\tself.edge[y].append((x, w))\n\t\t\tself.size[y] += self.size[x]\n\t\t\tself.val[x] = self.val[y] + w\n\t\t\tself.dfs(x, y)\n\nclass Dijkstra:\n\n\tclass Edge:\n\n\t\tdef __init__(self, _to, _cost):\n\t\t\tself.to = _to\n\t\t\tself.cost = _cost\n\n\tdef __init__(self, V):\n\t\tself.G = [[] for i in range(V)]\n\t\tself._E = 0\n\t\tself._V = V\n\n\t@property\n\tdef E(self):\n\t\treturn self._E\n\n\t@property\n\tdef V(self):\n\t\treturn self._V\n\n\tdef add_edge(self, _from, _to, _cost):\n\t\tself.G[_from].append(self.Edge(_to, _cost))\n\t\tself._E += 1\n\n\tdef shortest_path(self, s):\n\t\timport heapq\n\t\tque = []\n\t\td = [10 ** 15] * self.V\n\t\td[s] = 0\n\t\theapq.heappush(que, (0, s))\n\t\twhile len(que) != 0:\n\t\t\t(cost, v) = heapq.heappop(que)\n\t\t\tif d[v] < cost:\n\t\t\t\tcontinue\n\t\t\tfor i in range(len(self.G[v])):\n\t\t\t\te = self.G[v][i]\n\t\t\t\tif d[e.to] > d[v] + e.cost:\n\t\t\t\t\td[e.to] = d[v] + e.cost\n\t\t\t\t\theapq.heappush(que, (d[e.to], e.to))\n\t\treturn d\n\ndef Z_algorithm(s):\n\tN = len(s)\n\tZ_alg = [0] * N\n\tZ_alg[0] = N\n\ti = 1\n\tj = 0\n\twhile i < N:\n\t\twhile i + j < N and s[j] == s[i + j]:\n\t\t\tj += 1\n\t\tZ_alg[i] = j\n\t\tif j == 0:\n\t\t\ti += 1\n\t\t\tcontinue\n\t\tk = 1\n\t\twhile i + k < N and k + Z_alg[k] < j:\n\t\t\tZ_alg[i + k] = Z_alg[k]\n\t\t\tk += 1\n\t\ti += k\n\t\tj -= k\n\treturn Z_alg\n\nclass BIT:\n\n\tdef __init__(self, n, mod=0):\n\t\tself.BIT = [0] * (n + 1)\n\t\tself.num = n\n\t\tself.mod = mod\n\n\tdef query(self, idx):\n\t\tres_sum = 0\n\t\tmod = self.mod\n\t\twhile idx > 0:\n\t\t\tres_sum += self.BIT[idx]\n\t\t\tif mod:\n\t\t\t\tres_sum %= mod\n\t\t\tidx -= idx & -idx\n\t\treturn res_sum\n\n\tdef update(self, idx, x):\n\t\tmod = self.mod\n\t\twhile idx <= self.num:\n\t\t\tself.BIT[idx] += x\n\t\t\tif mod:\n\t\t\t\tself.BIT[idx] %= mod\n\t\t\tidx += idx & -idx\n\t\treturn\n\nclass dancinglink:\n\n\tdef __init__(self, n, debug=False):\n\t\tself.n = n\n\t\tself.debug = debug\n\t\tself._left = [i - 1 for i in range(n)]\n\t\tself._right = [i + 1 for i in range(n)]\n\t\tself.exist = [True for i in range(n)]\n\n\tdef pop(self, k):\n\t\tif self.debug:\n\t\t\tassert self.exist[k]\n\t\tL = self._left[k]\n\t\tR = self._right[k]\n\t\tif L != -1:\n\t\t\tif R != self.n:\n\t\t\t\t(self._right[L], self._left[R]) = (R, L)\n\t\t\telse:\n\t\t\t\tself._right[L] = self.n\n\t\telif R != self.n:\n\t\t\tself._left[R] = -1\n\t\tself.exist[k] = False\n\n\tdef left(self, idx, k=1):\n\t\tif self.debug:\n\t\t\tassert self.exist[idx]\n\t\tres = idx\n\t\twhile k:\n\t\t\tres = self._left[res]\n\t\t\tif res == -1:\n\t\t\t\tbreak\n\t\t\tk -= 1\n\t\treturn res\n\n\tdef right(self, idx, k=1):\n\t\tif self.debug:\n\t\t\tassert self.exist[idx]\n\t\tres = idx\n\t\twhile k:\n\t\t\tres = self._right[res]\n\t\t\tif res == self.n:\n\t\t\t\tbreak\n\t\t\tk -= 1\n\t\treturn res\n\nclass SparseTable:\n\n\tdef __init__(self, A, merge_func, ide_ele):\n\t\tN = len(A)\n\t\tn = N.bit_length()\n\t\tself.table = [[ide_ele for i in range(n)] for i in range(N)]\n\t\tself.merge_func = merge_func\n\t\tfor i in range(N):\n\t\t\tself.table[i][0] = A[i]\n\t\tfor j in range(1, n):\n\t\t\tfor i in range(0, N - 2 ** j + 1):\n\t\t\t\tf = self.table[i][j - 1]\n\t\t\t\ts = self.table[i + 2 ** (j - 1)][j - 1]\n\t\t\t\tself.table[i][j] = self.merge_func(f, s)\n\n\tdef query(self, s, t):\n\t\tb = t - s + 1\n\t\tm = b.bit_length() - 1\n\t\treturn self.merge_func(self.table[s][m], self.table[t - 2 ** m + 1][m])\n\nclass BinaryTrie:\n\n\tclass node:\n\n\t\tdef __init__(self, val):\n\t\t\tself.left = None\n\t\t\tself.right = None\n\t\t\tself.max = val\n\n\tdef __init__(self):\n\t\tself.root = self.node(-10 ** 15)\n\n\tdef append(self, key, val):\n\t\tpos = self.root\n\t\tfor i in range(29, -1, -1):\n\t\t\tpos.max = max(pos.max, val)\n\t\t\tif key >> i & 1:\n\t\t\t\tif pos.right is None:\n\t\t\t\t\tpos.right = self.node(val)\n\t\t\t\t\tpos = pos.right\n\t\t\t\telse:\n\t\t\t\t\tpos = pos.right\n\t\t\telif pos.left is None:\n\t\t\t\tpos.left = self.node(val)\n\t\t\t\tpos = pos.left\n\t\t\telse:\n\t\t\t\tpos = pos.left\n\t\tpos.max = max(pos.max, val)\n\n\tdef search(self, M, xor):\n\t\tres = -10 ** 15\n\t\tpos = self.root\n\t\tfor i in range(29, -1, -1):\n\t\t\tif pos is None:\n\t\t\t\tbreak\n\t\t\tif M >> i & 1:\n\t\t\t\tif xor >> i & 1:\n\t\t\t\t\tif pos.right:\n\t\t\t\t\t\tres = max(res, pos.right.max)\n\t\t\t\t\tpos = pos.left\n\t\t\t\telse:\n\t\t\t\t\tif pos.left:\n\t\t\t\t\t\tres = max(res, pos.left.max)\n\t\t\t\t\tpos = pos.right\n\t\t\telif xor >> i & 1:\n\t\t\t\tpos = pos.right\n\t\t\telse:\n\t\t\t\tpos = pos.left\n\t\tif pos:\n\t\t\tres = max(res, pos.max)\n\t\treturn res\n\ndef solveequation(edge, ans, n, m):\n\tx = [0] * m\n\tused = [False] * n\n\tfor v in range(n):\n\t\tif used[v]:\n\t\t\tcontinue\n\t\ty = dfs(v)\n\t\tif y != 0:\n\t\t\treturn False\n\treturn x\n\n\tdef dfs(v):\n\t\tused[v] = True\n\t\tr = ans[v]\n\t\tfor (to, dire, id) in edge[v]:\n\t\t\tif used[to]:\n\t\t\t\tcontinue\n\t\t\ty = dfs(to)\n\t\t\tif dire == -1:\n\t\t\t\tx[id] = y\n\t\t\telse:\n\t\t\t\tx[id] = -y\n\t\t\tr += y\n\t\treturn r\n\nclass Matrix:\n\tmod = 10 ** 9 + 7\n\n\tdef set_mod(m):\n\t\tMatrix.mod = m\n\n\tdef __init__(self, L):\n\t\tself.row = len(L)\n\t\tself.column = len(L[0])\n\t\tself._matrix = L\n\t\tfor i in range(self.row):\n\t\t\tfor j in range(self.column):\n\t\t\t\tself._matridigit[i][j] %= Matrix.mod\n\n\tdef __getitem__(self, item):\n\t\tif type(item) == int:\n\t\t\traise IndexError('you must specific row and column')\n\t\telif len(item) != 2:\n\t\t\traise IndexError('you must specific row and column')\n\t\t(i, j) = item\n\t\treturn self._matridigit[i][j]\n\n\tdef __setitem__(self, item, val):\n\t\tif type(item) == int:\n\t\t\traise IndexError('you must specific row and column')\n\t\telif len(item) != 2:\n\t\t\traise IndexError('you must specific row and column')\n\t\t(i, j) = item\n\t\tself._matridigit[i][j] = val\n\n\tdef __add__(self, other):\n\t\tif (self.row, self.column) != (other.row, other.column):\n\t\t\traise SizeError('sizes of matrixes are different')\n\t\tres = [[0 for j in range(self.column)] for i in range(self.row)]\n\t\tfor i in range(self.row):\n\t\t\tfor j in range(self.column):\n\t\t\t\tres[i][j] = self._matridigit[i][j] + other._matridigit[i][j]\n\t\t\t\tres[i][j] %= Matrix.mod\n\t\treturn Matrix(res)\n\n\tdef __sub__(self, other):\n\t\tif (self.row, self.column) != (other.row, other.column):\n\t\t\traise SizeError('sizes of matrixes are different')\n\t\tres = [[0 for j in range(self.column)] for i in range(self.row)]\n\t\tfor i in range(self.row):\n\t\t\tfor j in range(self.column):\n\t\t\t\tres[i][j] = self._matridigit[i][j] - other._matridigit[i][j]\n\t\t\t\tres[i][j] %= Matrix.mod\n\t\treturn Matrix(res)\n\n\tdef __mul__(self, other):\n\t\tif type(other) != int:\n\t\t\tif self.column != other.row:\n\t\t\t\traise SizeError('sizes of matrixes are different')\n\t\t\tres = [[0 for j in range(other.column)] for i in range(self.row)]\n\t\t\tfor i in range(self.row):\n\t\t\t\tfor j in range(other.column):\n\t\t\t\t\ttemp = 0\n\t\t\t\t\tfor k in range(self.column):\n\t\t\t\t\t\ttemp += self._matridigit[i][k] * other._matrix[k][j]\n\t\t\t\t\tres[i][j] = temp % Matrix.mod\n\t\t\treturn Matrix(res)\n\t\telse:\n\t\t\tn = other\n\t\t\tres = [[n * self._matridigit[i][j] % Matrix.mod for j in range(self.column)] for i in range(self.row)]\n\t\t\treturn Matrix(res)\n\n\tdef __pow__(self, m):\n\t\tif self.column != self.row:\n\t\t\traise MatrixPowError('the size of row must be the same as that of column')\n\t\tn = self.row\n\t\tres = Matrix([[int(i == j) for i in range(n)] for j in range(n)])\n\t\twhile m:\n\t\t\tif m % 2 == 1:\n\t\t\t\tres = res * self\n\t\t\tself = self * self\n\t\t\tm //= 2\n\t\treturn res\n\n\tdef __str__(self):\n\t\tres = []\n\t\tfor i in range(self.row):\n\t\t\tfor j in range(self.column):\n\t\t\t\tres.append(str(self._matridigit[i][j]))\n\t\t\t\tres.append(' ')\n\t\t\tres.append('\\n')\n\t\tres = res[:len(res) - 1]\n\t\treturn ''.join(res)\nfrom collections import deque\n\nclass Dinic:\n\n\tdef __init__(self, N):\n\t\tself.N = N\n\t\tself.G = [[] for i in range(N)]\n\n\tdef add_edge(self, fr, to, cap):\n\t\tforward = [to, cap, None]\n\t\tforward[2] = backward = [fr, 0, forward]\n\t\tself.G[fr].append(forward)\n\t\tself.G[to].append(backward)\n\n\tdef add_multi_edge(self, v1, v2, cap1, cap2):\n\t\tedge1 = [v2, cap1, None]\n\t\tedge1[2] = edge2 = [v1, cap2, edge1]\n\t\tself.G[v1].append(edge1)\n\t\tself.G[v2].append(edge2)\n\n\tdef bfs(self, s, t):\n\t\tself.level = level = [None] * self.N\n\t\tdeq = deque([s])\n\t\tlevel[s] = 0\n\t\tG = self.G\n\t\twhile deq:\n\t\t\tv = deq.popleft()\n\t\t\tlv = level[v] + 1\n\t\t\tfor (w, cap, _) in G[v]:\n\t\t\t\tif cap and level[w] is None:\n\t\t\t\t\tlevel[w] = lv\n\t\t\t\t\tdeq.append(w)\n\t\treturn level[t] is not None\n\n\tdef dfs(self, v, t, f):\n\t\tif v == t:\n\t\t\treturn f\n\t\tlevel = self.level\n\t\tfor e in self.it[v]:\n\t\t\t(w, cap, rev) = e\n\t\t\tif cap and level[v] < level[w]:\n\t\t\t\td = self.dfs(w, t, min(f, cap))\n\t\t\t\tif d:\n\t\t\t\t\te[1] -= d\n\t\t\t\t\trev[1] += d\n\t\t\t\t\treturn d\n\t\treturn 0\n\n\tdef flow(self, s, t):\n\t\tflow = 0\n\t\tINF = 10 ** 9 + 7\n\t\tG = self.G\n\t\twhile self.bfs(s, t):\n\t\t\t(*self.it,) = map(iter, self.G)\n\t\t\tf = INF\n\t\t\twhile f:\n\t\t\t\tf = self.dfs(s, t, INF)\n\t\t\t\tflow += f\n\t\treturn flow\nimport sys, random, bisect\nfrom collections import deque, defaultdict\nfrom heapq import heapify, heappop, heappush\nfrom itertools import permutations\nfrom math import gcd, log\ninput = lambda : sys.stdin.readline().rstrip()\nmi = lambda : map(int, input().split())\nli = lambda : list(mi())\nN = int(input())\nA = li()\ndp = [[False for r in range(N)] for l in range(N)]\nfor l in range(N):\n\ttmp = [A[l]]\n\tdp[l][l] = True\n\tfor r in range(l + 1, N):\n\t\tval = A[r]\n\t\twhile tmp and tmp[-1] == val:\n\t\t\tval = tmp[-1] + 1\n\t\t\ttmp.pop()\n\t\ttmp.append(val)\n\t\tif len(tmp) == 1:\n\t\t\tdp[l][r] = True\nres = [i for i in range(N + 1)]\nfor r in range(1, N + 1):\n\tfor l in range(1, r + 1):\n\t\tif dp[l - 1][r - 1]:\n\t\t\tres[r] = min(res[r], 1 + res[l - 1])\nprint(res[N])\n", "from sys import stdin, gettrace\nif not gettrace():\n\n\tdef input():\n\t\treturn next(stdin)[:-1]\nINF = 10000\n\ndef main():\n\tn = int(input())\n\taa = [int(a) for a in input().split()]\n\tdp = [[0] * (n + 1) for _ in range(n)]\n\n\tdef calc_dp(i, j):\n\t\tif i + 1 == j:\n\t\t\tdp[i][j] = aa[i]\n\t\tif dp[i][j] != 0:\n\t\t\treturn dp[i][j]\n\t\tdp[i][j] = -1\n\t\tfor k in range(i + 1, j):\n\t\t\tlf = calc_dp(i, k)\n\t\t\trg = calc_dp(k, j)\n\t\t\tif lf > 0 and lf == rg:\n\t\t\t\tdp[i][j] = lf + 1\n\t\t\t\tbreak\n\t\treturn dp[i][j]\n\tdp2 = list(range(0, n + 1))\n\tfor i in range(n):\n\t\tfor j in range(i + 1, n + 1):\n\t\t\tif calc_dp(i, j) > 0:\n\t\t\t\tdp2[j] = min(dp2[j], dp2[i] + 1)\n\tprint(dp2[n])\nmain()\n", "def f():\n\tn = int(input())\n\tA = [int(s) for s in input().split()]\n\tmemo = [[None for j in range(n + 1)] for i in range(n + 1)]\n\tfor i in range(n):\n\t\tmemo[i][i] = [A[i], A[i], 1]\n\tfor l in range(2, n + 1):\n\t\tfor left in range(0, n - l + 1):\n\t\t\tright = left + l - 1\n\t\t\tminLen = l\n\t\t\tshortestMid = right\n\t\t\tfor mid in range(left + 1, right + 1):\n\t\t\t\tpre = memo[left][mid - 1]\n\t\t\t\tpost = memo[mid][right]\n\t\t\t\tcombLen = pre[2] + post[2]\n\t\t\t\tif pre[1] == post[0]:\n\t\t\t\t\tcombLen -= 1\n\t\t\t\tif combLen < minLen:\n\t\t\t\t\tminLen = combLen\n\t\t\t\t\tshortestMid = mid\n\t\t\tpre = memo[left][shortestMid - 1]\n\t\t\tpost = memo[shortestMid][right]\n\t\t\tstartEle = pre[0]\n\t\t\tendEle = post[1]\n\t\t\tif pre[2] == 1:\n\t\t\t\tif pre[0] == post[0]:\n\t\t\t\t\tstartEle = pre[0] + 1\n\t\t\tif post[2] == 1:\n\t\t\t\tif pre[1] == post[0]:\n\t\t\t\t\tendEle = post[0] + 1\n\t\t\tmemo[left][right] = [startEle, endEle, minLen]\n\tprint(memo[0][n - 1][2])\nf()\n", "import sys\nimport bisect\nimport heapq\nfrom collections import defaultdict as dd\nfrom collections import deque\nfrom collections import Counter as c\nfrom itertools import combinations as comb\nfrom bisect import bisect_left as bl, bisect_right as br, bisect\nmod = pow(10, 9) + 7\nmod2 = 998244353\n\ndef data():\n\treturn sys.stdin.readline().strip()\n\ndef out(var):\n\tsys.stdout.write(var)\n\ndef l():\n\treturn list(map(int, data().split()))\n\ndef sl():\n\treturn list(map(str, data().split()))\n\ndef sp():\n\treturn map(int, data().split())\n\ndef ssp():\n\treturn map(str, data().split())\n\ndef l1d(n, val=0):\n\treturn [val for i in range(n)]\n\ndef l2d(n, m, val=0):\n\treturn [[val for i in range(n)] for j in range(m)]\nn = int(data())\narr = l()\ndp = [[0 for j in range(500)] for i in range(500)]\ndp2 = [0 for i in range(501)]\nfor i in range(n):\n\tdp[i][i] = arr[i]\ni = n - 2\nwhile ~i:\n\tj = i + 1\n\twhile j < n:\n\t\tdp[i][j] = -1\n\t\tfor k in range(i, j):\n\t\t\tif (~dp[i][k] and dp[i][k]) == dp[k + 1][j]:\n\t\t\t\tdp[i][j] = dp[i][k] + 1\n\t\tj += 1\n\ti -= 1\nfor i in range(1, n + 1):\n\tdp2[i] = pow(10, 9)\n\tfor j in range(i):\n\t\tif ~dp[j][i - 1]:\n\t\t\tdp2[i] = min(dp2[i], dp2[j] + 1)\nout(str(dp2[n]))\n", "from heapq import *\nimport sys\nint1 = lambda x: int(x) - 1\np2D = lambda x: print(*x, sep='\\n')\n\ndef II():\n\treturn int(sys.stdin.readline())\n\ndef MI():\n\treturn map(int, sys.stdin.readline().split())\n\ndef LI():\n\treturn list(map(int, sys.stdin.readline().split()))\n\ndef LLI(rows_number):\n\treturn [LI() for _ in range(rows_number)]\n\ndef SI():\n\treturn sys.stdin.readline()[:-1]\n\ndef main():\n\tinf = 10 ** 9\n\tn = II()\n\taa = LI()\n\tdp1 = [[-1] * (n + 1) for _ in range(n)]\n\tto = [[i + 1] for i in range(n)]\n\tfor i in range(n):\n\t\tdp1[i][i + 1] = aa[i]\n\tfor w in range(2, n + 1):\n\t\tfor l in range(n - w + 1):\n\t\t\tr = l + w\n\t\t\tfor m in range(l + 1, r):\n\t\t\t\tif dp1[l][m] != -1 and dp1[l][m] == dp1[m][r]:\n\t\t\t\t\tdp1[l][r] = dp1[l][m] + 1\n\t\t\t\t\tto[l].append(r)\n\thp = []\n\theappush(hp, (0, 0))\n\tdist = [-1] * (n + 1)\n\twhile hp:\n\t\t(d, i) = heappop(hp)\n\t\tif i == n:\n\t\t\tprint(d)\n\t\t\tbreak\n\t\tif dist[i] != -1:\n\t\t\tcontinue\n\t\tdist[i] = d\n\t\tfor j in to[i]:\n\t\t\tif dist[j] != -1:\n\t\t\t\tcontinue\n\t\t\theappush(hp, (d + 1, j))\nmain()\n", "import sys\nfrom array import array\nfrom typing import List, Tuple, TypeVar, Generic, Sequence, Union\n\ndef input():\n\treturn sys.stdin.buffer.readline().decode('utf-8')\n\ndef main():\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tdp = [array('h', [10000]) * (n + 1) for _ in range(n + 1)]\n\tnum = [array('h', [-1]) * (n + 1) for _ in range(n + 1)]\n\tfor i in range(n):\n\t\tdp[i][i + 1] = 1\n\t\tnum[i][i + 1] = a[i]\n\tfor sublen in range(2, n + 1):\n\t\tfor (l, r) in zip(range(n), range(sublen, n + 1)):\n\t\t\tfor mid in range(l + 1, r):\n\t\t\t\tif num[l][mid] == num[mid][r] != -1:\n\t\t\t\t\tdp[l][r] = 1\n\t\t\t\t\tnum[l][r] = num[l][mid] + 1\n\t\t\t\t\tbreak\n\t\t\t\tdp[l][r] = min(dp[l][r], dp[l][mid] + dp[mid][r])\n\tprint(dp[0][-1])\nmain()\n", "n = int(input())\na = [int(x) for x in input().split()]\ndp = [[-1] * (n + 1) for _ in range(n + 1)]\nfor i in range(n):\n\tdp[i][i + 1] = a[i]\nfor leng in range(2, n + 1):\n\tfor l in range(n + 1):\n\t\tif l + leng > n:\n\t\t\tcontinue\n\t\tr = l + leng\n\t\tfor mid in range(l + 1, n + 1):\n\t\t\tif dp[l][mid] != -1 and dp[l][mid] == dp[mid][r]:\n\t\t\t\tdp[l][r] = dp[l][mid] + 1\ndp2 = [float('inf') for _ in range(n + 1)]\nfor i in range(n + 1):\n\tdp2[i] = i\n\tfor j in range(i):\n\t\tif dp[j][i] != -1:\n\t\t\tdp2[i] = min(dp2[i], dp2[j] + 1)\nprint(dp2[n])\n", "import sys\ninput = sys.stdin.readline\nn = int(input().strip())\na = [int(x) for x in input().strip().split()]\ndp = [[0] * n for i in range(n)]\nfor i in range(n):\n\tdp[i][i] = [a[i], 1]\nfor i in range(1, n):\n\tfor j in range(n - i):\n\t\t(v, c) = (-1, i + 1)\n\t\tfor k in range(i):\n\t\t\tif dp[j][j + k][0] != -1 and dp[j][j + k][0] == dp[j + k + 1][j + i][0]:\n\t\t\t\t(v, c) = (dp[j][j + k][0] + 1, 1)\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\t(v, c) = (-1, min(c, dp[j][j + k][1] + dp[j + k + 1][j + i][1]))\n\t\tdp[j][j + i] = [v, c]\nprint(dp[0][-1][1])\n", "import sys\npl = 1\nif pl:\n\tinput = sys.stdin.readline\nelse:\n\tsys.stdin = open('input.txt', 'r')\n\tsys.stdout = open('outpt.txt', 'w')\n\ndef li():\n\treturn [int(xxx) for xxx in input().split()]\n\ndef fi():\n\treturn int(input())\n\ndef si():\n\treturn list(input().rstrip())\n\ndef mi():\n\treturn map(int, input().split())\nt = 1\nwhile t > 0:\n\tt -= 1\n\tn = fi()\n\ta = li()\n\tdp = [[0] * (n + 1) for i in range(n + 1)]\n\tfor i in range(n - 1, -1, -1):\n\t\tfor j in range(i, n):\n\t\t\tif i == j:\n\t\t\t\tdp[i][j] = a[i]\n\t\t\telif i == j - 1:\n\t\t\t\tif a[i] == a[j]:\n\t\t\t\t\tdp[i][j] = a[i] + 1\n\t\t\telse:\n\t\t\t\tfor k in range(i, j):\n\t\t\t\t\tif dp[i][k] and dp[k + 1][j] and (dp[i][k] == dp[k + 1][j]):\n\t\t\t\t\t\tdp[i][j] = dp[i][k] + 1\n\t\t\t\t\t\tbreak\n\tans = [10 ** 18] * (n + 1)\n\tans[-1] = 0\n\tfor i in range(n - 1, -1, -1):\n\t\tfor j in range(i, n):\n\t\t\tif dp[i][j]:\n\t\t\t\tans[i] = min(ans[i], 1 + ans[j + 1])\n\t\t\telse:\n\t\t\t\tans[i] = min(ans[i], j - i + 1 + ans[j + 1])\n\tprint(ans[0])\n", "from sys import stdin, stdout\nimport sys\nn = int(stdin.readline().strip())\narr = list(map(int, stdin.readline().strip().split(' ')))\ndp_arr = [[None for i in range(n)] for i in range(n)]\nfor i in range(n):\n\tdp_arr[i][i] = (arr[i], 1, arr[i])\n\ndef merge_small(c1, c2):\n\tif c1[1] == 1 and c2[1] == 1:\n\t\tif c1[0] == c2[0]:\n\t\t\treturn (c1[0] + 1, 1, c1[0] + 1)\n\t\telse:\n\t\t\treturn (c1[0], 2, c2[0])\n\telif c1[1] == 2 and c2[1] == 1:\n\t\tif c1[2] == c2[0]:\n\t\t\tif c1[0] == c1[2] + 1:\n\t\t\t\treturn (c1[0] + 1, 1, c1[0] + 1)\n\t\t\telse:\n\t\t\t\treturn (c1[0], 2, c2[2] + 1)\n\t\telse:\n\t\t\treturn (c1[0], 3, c2[2])\n\telif c1[1] == 1 and c2[1] == 2:\n\t\tif c1[2] == c2[0]:\n\t\t\tif c2[2] == c2[0] + 1:\n\t\t\t\treturn (c2[2] + 1, 1, c2[2] + 1)\n\t\t\telse:\n\t\t\t\treturn (c2[0] + 1, 2, c2[2])\n\t\telse:\n\t\t\treturn (c1[0], 3, c2[2])\n\telif c1[1] == 2 and c2[1] == 2:\n\t\tif c1[2] == c2[0]:\n\t\t\tc1 = (c1[0], 2, c1[2] + 1)\n\t\t\tc2 = (c2[2], 1, c2[2])\n\t\t\tif c1[1] == 2 and c2[1] == 1:\n\t\t\t\tif c1[2] == c2[0]:\n\t\t\t\t\tif c1[0] == c1[2] + 1:\n\t\t\t\t\t\treturn (c1[0] + 1, 1, c1[0] + 1)\n\t\t\t\t\telse:\n\t\t\t\t\t\treturn (c1[0], 2, c2[2] + 1)\n\t\t\t\telse:\n\t\t\t\t\treturn (c1[0], 3, c2[2])\n\t\telse:\n\t\t\treturn (c1[0], 4, c2[2])\n\ndef merge_main(c1, c2):\n\tif c1[1] > 2:\n\t\tif c2[1] > 2:\n\t\t\tif c1[2] == c2[0]:\n\t\t\t\treturn (c1[0], c1[1] + c2[1] - 1, c2[2])\n\t\t\telse:\n\t\t\t\treturn (c1[0], c1[1] + c2[1], c2[2])\n\t\telse:\n\t\t\tif c2[1] == 1:\n\t\t\t\tif c1[2] == c2[0]:\n\t\t\t\t\treturn (c1[0], c1[1], c2[2] + 1)\n\t\t\t\telse:\n\t\t\t\t\treturn (c1[0], c1[1] + 1, c2[2])\n\t\t\tif c2[1] == 2:\n\t\t\t\tif c1[2] == c2[0]:\n\t\t\t\t\tif c1[2] + 1 == c2[2]:\n\t\t\t\t\t\treturn (c1[0], c1[1], c2[2] + 1)\n\t\t\t\t\telse:\n\t\t\t\t\t\treturn (c1[0], c1[1] + 1, c2[2])\n\t\t\t\telse:\n\t\t\t\t\treturn (c1[0], c1[1] + 2, c2[2])\n\telif c2[1] > 2:\n\t\tif c1[1] == 1:\n\t\t\tif c1[2] == c2[0]:\n\t\t\t\treturn (c1[2] + 1, c2[1], c2[2])\n\t\t\telse:\n\t\t\t\treturn (c1[2], c2[1] + 1, c2[2])\n\t\tif c1[1] == 2:\n\t\t\tif c1[2] == c2[0]:\n\t\t\t\tif c1[0] == c1[2] + 1:\n\t\t\t\t\treturn (c1[0] + 1, c2[1], c2[2])\n\t\t\t\telse:\n\t\t\t\t\treturn (c1[0], c2[1] + 1, c2[2])\n\t\t\telse:\n\t\t\t\treturn (c1[0], c2[1] + 2, c2[2])\n\telse:\n\t\treturn merge_small(c1, c2)\nfor i1 in range(1, n):\n\tfor j1 in range(n - i1):\n\t\tcurr_pos = (j1, j1 + i1)\n\t\tfor k1 in range(j1, j1 + i1):\n\t\t\tres = merge_main(dp_arr[j1][k1], dp_arr[k1 + 1][j1 + i1])\n\t\t\tif dp_arr[j1][j1 + i1] == None or dp_arr[j1][j1 + i1][1] > res[1]:\n\t\t\t\tdp_arr[j1][j1 + i1] = res\nstdout.write(str(dp_arr[0][n - 1][1]) + '\\n')\n", "import sys\nint1 = lambda x: int(x) - 1\np2D = lambda x: print(*x, sep='\\n')\n\ndef II():\n\treturn int(sys.stdin.readline())\n\ndef MI():\n\treturn map(int, sys.stdin.readline().split())\n\ndef LI():\n\treturn list(map(int, sys.stdin.readline().split()))\n\ndef LLI(rows_number):\n\treturn [LI() for _ in range(rows_number)]\n\ndef SI():\n\treturn sys.stdin.readline()[:-1]\n\ndef main():\n\tinf = 10 ** 9\n\tn = II()\n\taa = LI()\n\tdp1 = [[-1] * n for _ in range(n)]\n\tdp2 = [[inf] * n for _ in range(n)]\n\tfor i in range(n):\n\t\tdp1[i][i] = aa[i]\n\t\tdp2[i][i] = 1\n\tfor w in range(2, n + 1):\n\t\tfor l in range(n - w + 1):\n\t\t\tr = l + w - 1\n\t\t\tfor m in range(l, r):\n\t\t\t\tif dp1[l][m] != -1 and dp1[l][m] == dp1[m + 1][r]:\n\t\t\t\t\tdp1[l][r] = dp1[l][m] + 1\n\t\t\t\t\tdp2[l][r] = 1\n\tfor m in range(n):\n\t\tfor l in range(m + 1):\n\t\t\tfor r in range(m + 1, n):\n\t\t\t\tdp2[l][r] = min(dp2[l][r], dp2[l][m] + dp2[m + 1][r])\n\tprint(dp2[0][n - 1])\nmain()\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nfrom math import factorial\nfrom collections import Counter, defaultdict, deque\nfrom heapq import heapify, heappop, heappush\n\ndef RL():\n\treturn map(int, sys.stdin.readline().rstrip().split())\n\ndef RLL():\n\treturn list(map(int, sys.stdin.readline().rstrip().split()))\n\ndef N():\n\treturn int(input())\n\ndef comb(n, m):\n\treturn factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0\n\ndef perm(n, m):\n\treturn factorial(n) // factorial(n - m) if n >= m else 0\n\ndef mdis(x1, y1, x2, y2):\n\treturn abs(x1 - x2) + abs(y1 - y2)\nmod = 998244353\nINF = float('inf')\n\ndef main():\n\tn = N()\n\tarr = RLL()\n\tdp = [[n] * n for i in range(n)]\n\trec = [[0] * n for i in range(n)]\n\tfor i in range(n):\n\t\trec[i][i] = arr[i]\n\t\tdp[i][i] = 1\n\tfor le in range(2, n + 1):\n\t\tfor l in range(n):\n\t\t\tr = l + le - 1\n\t\t\tif r > n - 1:\n\t\t\t\tbreak\n\t\t\tfor m in range(l, r):\n\t\t\t\tdp[l][r] = min(dp[l][r], dp[l][m] + dp[m + 1][r])\n\t\t\t\tif rec[l][m] == rec[m + 1][r] and dp[l][m] == dp[m + 1][r] == 1:\n\t\t\t\t\tdp[l][r] = 1\n\t\t\t\t\trec[l][r] = rec[l][m] + 1\n\tprint(dp[0][-1])\nmain()\n", "from sys import stdin, stdout\n\ndef dfs(l, r, dp, a_a):\n\tif l == r:\n\t\treturn a_a[l]\n\tif l + 1 == r:\n\t\tif a_a[l] == a_a[r]:\n\t\t\treturn a_a[l] + 1\n\t\telse:\n\t\t\treturn -1\n\tif dp[l][r] != 10 ** 6:\n\t\treturn dp[l][r]\n\tdp[l][r] = -1\n\tfor m in range(l, r):\n\t\tr1 = dfs(l, m, dp, a_a)\n\t\tr2 = dfs(m + 1, r, dp, a_a)\n\t\tif r1 > 0 and r1 == r2:\n\t\t\tdp[l][r] = r1 + 1\n\t\t\treturn dp[l][r]\n\treturn dp[l][r]\n\ndef array_shrinking(n, a_a):\n\tdp = [[10 ** 6 for _ in range(n)] for _ in range(n)]\n\tdp2 = [10 ** 6 for _ in range(n)]\n\tfor i in range(n):\n\t\tdp2[i] = min(i + 1, dp2[i])\n\t\tfor j in range(i, n):\n\t\t\tr = dfs(i, j, dp, a_a)\n\t\t\tif r != -1:\n\t\t\t\tif i > 0:\n\t\t\t\t\tdp2[j] = min(dp2[i - 1] + 1, dp2[j])\n\t\t\t\telse:\n\t\t\t\t\tdp2[j] = min(1, dp2[j])\n\treturn dp2[n - 1]\nn = int(stdin.readline())\na_a = list(map(int, stdin.readline().split()))\nres = array_shrinking(n, a_a)\nstdout.write(str(res))\n", "import sys, math\nimport io, os\nfrom bisect import bisect_left as bl, bisect_right as br, insort\nfrom collections import defaultdict as dd, deque, Counter\n\ndef data():\n\treturn sys.stdin.readline().strip()\n\ndef mdata():\n\treturn list(map(int, data().split()))\n\ndef outl(var):\n\tsys.stdout.write(' '.join(map(str, var)) + '\\n')\n\ndef out(var):\n\tsys.stdout.write(str(var) + '\\n')\nfrom decimal import Decimal\nINF = 10001\nmod = int(1000000000.0) + 7\nn = int(data())\na = mdata()\nans = [n]\ndp1 = [[0] * n for i in range(n)]\ndp2 = [[n] * n for i in range(n)]\nfor i in range(n - 1, -1, -1):\n\tdp1[i][i] = a[i]\n\tdp2[i][i] = 1\n\tfor j in range(i + 1, n):\n\t\tfor k in range(i, j):\n\t\t\tif dp1[i][k] == dp1[k + 1][j] != 0:\n\t\t\t\tdp1[i][j] = dp1[i][k] + 1\n\t\t\t\tdp2[i][j] = 1\n\t\t\tdp2[i][j] = min(dp2[i][j], dp2[i][k] + dp2[k + 1][j])\nout(dp2[0][n - 1])\n", "import io\nimport os\nimport sys\nfrom functools import lru_cache\nfrom collections import defaultdict\nsys.setrecursionlimit(10 ** 5)\n\ndef solve(N, A):\n\tvalToLeftRight = defaultdict(lambda : defaultdict(set))\n\tvalToRightLeft = defaultdict(lambda : defaultdict(set))\n\tfor (i, x) in enumerate(A):\n\t\tvalToLeftRight[x][i].add(i)\n\t\tvalToRightLeft[x][i].add(i)\n\tmaxVal = 1000 + 10\n\tfor val in range(maxVal):\n\t\tfor (l, rights) in valToLeftRight[val - 1].items():\n\t\t\tfor r in rights:\n\t\t\t\tl2 = r + 1\n\t\t\t\tif l2 in valToLeftRight[val - 1]:\n\t\t\t\t\tfor r2 in valToLeftRight[val - 1][l2]:\n\t\t\t\t\t\tassert l <= r\n\t\t\t\t\t\tassert r + 1 == l2\n\t\t\t\t\t\tassert l2 <= r2\n\t\t\t\t\t\tvalToLeftRight[val][l].add(r2)\n\t\t\t\t\t\tvalToRightLeft[val][r2].add(l)\n\t\t\t\tr2 = l - 1\n\t\t\t\tif r2 in valToRightLeft[val - 1]:\n\t\t\t\t\tfor l2 in valToRightLeft[val - 1][r2]:\n\t\t\t\t\t\tassert l2 <= r2\n\t\t\t\t\t\tassert r2 == l - 1\n\t\t\t\t\t\tassert l <= r\n\t\t\t\t\t\tvalToLeftRight[val][l2].add(r)\n\t\t\t\t\t\tvalToRightLeft[val][r].add(l2)\n\tintervals = defaultdict(list)\n\tfor val in range(maxVal):\n\t\tfor (l, rights) in valToLeftRight[val].items():\n\t\t\tfor r in rights:\n\t\t\t\tintervals[l].append(r)\n\n\t@lru_cache(maxsize=None)\n\tdef getBest(left):\n\t\tif left == N:\n\t\t\treturn 0\n\t\tbest = float('inf')\n\t\tfor right in intervals[left]:\n\t\t\tbest = min(best, 1 + getBest(right + 1))\n\t\treturn best\n\treturn getBest(0)\n\ndef tup(l, r):\n\treturn l * 16384 + r\n\ndef untup(t):\n\treturn divmod(t, 16384)\n\ndef solve(N, A):\n\tcache = {}\n\n\tdef f(lr):\n\t\tif lr not in cache:\n\t\t\t(l, r) = untup(lr)\n\t\t\tif r - l == 1:\n\t\t\t\treturn tup(1, A[l])\n\t\t\tbest = tup(float('inf'), float('inf'))\n\t\t\tfor i in range(l + 1, r):\n\t\t\t\tlSplit = f(tup(l, i))\n\t\t\t\trSplit = f(tup(i, r))\n\t\t\t\t(lLen, lVal) = untup(lSplit)\n\t\t\t\t(rLen, rVal) = untup(rSplit)\n\t\t\t\tif lLen != 1 or rLen != 1:\n\t\t\t\t\tbest = min(best, tup(lLen + rLen, 9999))\n\t\t\t\telif lVal == rVal:\n\t\t\t\t\tbest = min(best, tup(1, lVal + 1))\n\t\t\t\telse:\n\t\t\t\t\tbest = min(best, tup(2, 9999))\n\t\t\tcache[lr] = best\n\t\treturn cache[lr]\n\tans = untup(f(tup(0, N)))[0]\n\treturn ans\ninput = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline\n(N,) = list(map(int, input().split()))\nA = list(map(int, input().split()))\nans = solve(N, A)\nprint(ans)\n", "def rr():\n\treturn input().rstrip()\n\ndef rri():\n\treturn int(rr())\n\ndef rrm():\n\treturn list(map(int, rr().split()))\nfrom collections import defaultdict\n\ndef mus(d=0):\n\treturn defaultdict(defaultdict(d))\n\ndef ms(x, y, d=0):\n\treturn [[d] * y for i in range(x)]\n\ndef ar(x, d=0):\n\treturn [d] * x\n\ndef ppm(m, n=0, x=0, y=0):\n\tprint('\\n'.join(('\\t'.join((str(m[j][i]) for j in range(y or n))) for i in range(x or n))))\n\ndef ppa(a, n):\n\tprint('\\t'.join(map(str, a[0:n])))\n\ndef ppl():\n\tprint('\\n+' + '- -' * 20 + '+\\n')\nINF = float('inf')\n\ndef fake_input():\n\treturn ...\ndp = ms(501, 501)\ndp2 = ar(501, INF)\n\ndef read():\n\tn = rri()\n\tglobal arr\n\tarr = rrm()\n\treturn (arr, n)\n\ndef calc_dp(l, r):\n\tassert l < r\n\tif l + 1 == r:\n\t\tdp[l][r] = arr[l]\n\t\treturn dp[l][r]\n\tif dp[l][r] != 0:\n\t\treturn dp[l][r]\n\tdp[l][r] = -1\n\tfor i in range(l + 1, r):\n\t\tlf = calc_dp(l, i)\n\t\trg = calc_dp(i, r)\n\t\tif lf > 0 and lf == rg:\n\t\t\tdp[l][r] = lf + 1\n\t\t\treturn dp[l][r]\n\treturn dp[l][r]\n\ndef solve(arr, n):\n\tdp2[0] = 0\n\tfor i in range(n):\n\t\tfor j in range(i + 1, n + 1):\n\t\t\tv = calc_dp(i, j)\n\t\t\tif v > 0:\n\t\t\t\tdp2[j] = min(dp2[j], dp2[i] + 1)\n\tans = dp2[n]\n\treturn ans\ninput_data = read()\nresult = solve(*input_data)\nprint(result)\n", "n = int(input())\nli = list(map(int, input().split(' ')))\ndp1 = []\nfor i in range(n):\n\tlis = [-1] * n\n\tdp1.append(lis)\ndp2 = [0] * n\nfor i in range(n):\n\tdp1[i][i] = li[i]\nfor i in range(n):\n\tdp2[i] = i + 1\nsize = 2\nwhile size <= n:\n\ti = 0\n\twhile i < n - size + 1:\n\t\tj = i + size - 1\n\t\tk = i\n\t\twhile k < j:\n\t\t\tif dp1[i][k] != -1:\n\t\t\t\tif dp1[i][k] == dp1[k + 1][j]:\n\t\t\t\t\tdp1[i][j] = dp1[i][k] + 1\n\t\t\tk += 1\n\t\ti += 1\n\tsize += 1\ni = 0\nwhile i < n:\n\tk = 0\n\twhile k <= i:\n\t\tif dp1[k][i] != -1:\n\t\t\tif k == 0:\n\t\t\t\tdp2[i] = 1\n\t\t\telse:\n\t\t\t\tdp2[i] = min(dp2[i], dp2[k - 1] + 1)\n\t\tk += 1\n\ti += 1\nprint(dp2[n - 1])\n", "import math\ninput_list = lambda : list(map(int, input().split()))\nn = int(input())\na = input_list()\n(rows, cols) = (n + 1, n + 1)\ndp = [[-1 for i in range(rows)] for j in range(cols)]\nfor i in range(n):\n\tdp[i][i] = a[i]\nfor last in range(1, n):\n\tfor first in range(last - 1, -1, -1):\n\t\tfor mid in range(last, first, -1):\n\t\t\tif dp[first][mid - 1] != -1 and dp[mid][last] != -1 and (dp[first][mid - 1] == dp[mid][last]):\n\t\t\t\tdp[first][last] = dp[first][mid - 1] + 1\nans = [0 for i in range(n)]\nfor i in range(n):\n\tans[i] = i + 1\nfor i in range(n):\n\tfor j in range(i, -1, -1):\n\t\tif j - 1 >= 0:\n\t\t\tif dp[j][i] != -1:\n\t\t\t\tans[i] = min(ans[i], ans[j - 1] + 1)\n\t\telif dp[0][i] != -1:\n\t\t\tans[i] = 1\nprint(ans[n - 1])\n", "n = int(input())\nb = [int(_) for _ in input().split()]\nd = [[b[i] if i == j else -1 for i in range(n)] for j in range(n)]\n\ndef f(i, j):\n\tif d[i][j] != -1:\n\t\treturn d[i][j]\n\td[i][j] = 0\n\tfor m in range(i, j):\n\t\tl = f(i, m)\n\t\tif f(m + 1, j) == l and l:\n\t\t\td[i][j] = l + 1\n\t\t\tbreak\n\treturn d[i][j]\na = [_ for _ in range(1, n + 1)]\nfor e in range(1, n):\n\tfor s in range(e + 1):\n\t\tif f(s, e):\n\t\t\ta[e] = min(a[e], a[s - 1] + 1 if s > 0 else a[s])\nprint(a[-1])\n", "def examA():\n\tT = I()\n\tans = []\n\tfor _ in range(T):\n\t\t(N, M) = LI()\n\t\tif N % M != 0:\n\t\t\tans.append('NO')\n\t\telse:\n\t\t\tans.append('YES')\n\tfor v in ans:\n\t\tprint(v)\n\treturn\n\ndef examB():\n\tT = I()\n\tans = []\n\tfor _ in range(T):\n\t\tN = I()\n\t\tA = LI()\n\t\tA.sort()\n\t\tans.append(A[::-1])\n\tfor v in ans:\n\t\tprint(' '.join(map(str, v)))\n\treturn\n\ndef examC():\n\tT = I()\n\tans = []\n\tfor _ in range(T):\n\t\t(N, K) = LI()\n\t\tA = LI()\n\t\tsumA = sum(A)\n\t\tif sumA == 0:\n\t\t\tans.append('YES')\n\t\t\tcontinue\n\t\tcur = 0\n\t\tL = []\n\t\tfor i in range(100):\n\t\t\tnow = K ** i\n\t\t\tL.append(now)\n\t\t\tcur += now\n\t\t\tif cur >= sumA:\n\t\t\t\tbreak\n\t\tfor i in range(N):\n\t\t\tA[i] *= -1\n\t\theapify(A)\n\t\tfor l in L[::-1]:\n\t\t\tif not A:\n\t\t\t\tbreak\n\t\t\ta = -heappop(A)\n\t\t\tif a < l:\n\t\t\t\theappush(A, -a)\n\t\t\telif a > l:\n\t\t\t\theappush(A, -(a - l))\n\t\tif not A or heappop(A) == 0:\n\t\t\tans.append('YES')\n\t\telse:\n\t\t\tans.append('NO')\n\tfor v in ans:\n\t\tprint(v)\n\treturn\n\ndef examD():\n\n\tclass combination:\n\n\t\tdef __init__(self, n, mod):\n\t\t\tself.n = n\n\t\t\tself.fac = [1] * (n + 1)\n\t\t\tself.inv = [1] * (n + 1)\n\t\t\tfor j in range(1, n + 1):\n\t\t\t\tself.fac[j] = self.fac[j - 1] * j % mod\n\t\t\tself.inv[n] = pow(self.fac[n], mod - 2, mod)\n\t\t\tfor j in range(n - 1, -1, -1):\n\t\t\t\tself.inv[j] = self.inv[j + 1] * (j + 1) % mod\n\n\t\tdef comb(self, n, r, mod):\n\t\t\tif r > n or n < 0 or r < 0:\n\t\t\t\treturn 0\n\t\t\treturn self.fac[n] * self.inv[n - r] * self.inv[r] % mod\n\t(N, M) = LI()\n\tans = 0\n\tif N == 2:\n\t\tprint(ans)\n\t\treturn\n\tC = combination(M, mod2)\n\tfor i in range(N - 1, M + 1):\n\t\tcur = pow(2, N - 3, mod2) * (i - 1) * C.comb(i - 2, N - 3, mod2)\n\t\tans += cur\n\t\tans %= mod2\n\tprint(ans)\n\treturn\n\ndef examE():\n\tN = I()\n\tA = LI()\n\tdp = [[-1] * (N + 1) for _ in range(N + 1)]\n\tfor i in range(N):\n\t\tdp[i][i + 1] = A[i]\n\tfor l in range(2, N + 1):\n\t\tfor i in range(N - l + 1):\n\t\t\tfor k in range(i + 1, i + l):\n\t\t\t\tif dp[i][k] >= 1 and dp[i][k] == dp[k][i + l]:\n\t\t\t\t\tdp[i][i + l] = dp[i][k] + 1\n\tL = [inf] * (N + 1)\n\tfor i in range(1, N + 1):\n\t\tif dp[0][i] >= 1:\n\t\t\tL[i] = 1\n\tfor i in range(N):\n\t\tfor k in range(1, N - i + 1):\n\t\t\tif dp[i][i + k] >= 1:\n\t\t\t\tL[i + k] = min(L[i + k], L[i] + 1)\n\tans = L[N]\n\tprint(ans)\n\treturn\n\ndef examF():\n\tans = 0\n\tprint(ans)\n\treturn\nimport sys, copy, bisect, itertools, heapq, math, random\nfrom heapq import heappop, heappush, heapify\nfrom collections import Counter, defaultdict, deque\n\ndef I():\n\treturn int(sys.stdin.readline())\n\ndef LI():\n\treturn list(map(int, sys.stdin.readline().split()))\n\ndef LSI():\n\treturn list(map(str, sys.stdin.readline().split()))\n\ndef LS():\n\treturn sys.stdin.readline().split()\n\ndef SI():\n\treturn sys.stdin.readline().strip()\nglobal mod, mod2, inf, alphabet, _ep\nmod = 10 ** 9 + 7\nmod2 = 998244353\ninf = 10 ** 18\n_ep = 10 ** (-12)\nalphabet = [chr(ord('a') + i) for i in range(26)]\nexamE()\n", "n = int(input())\nb = [int(_) for _ in input().split()]\nd = [[-1 if i != j else b[i] for i in range(n)] for j in range(n)]\nfor l in range(1, n):\n\tfor s in range(n - l):\n\t\te = s + l\n\t\tfor m in range(s, e):\n\t\t\tif d[s][m] == d[m + 1][e] and d[s][m] != -1:\n\t\t\t\td[s][e] = d[s][m] + 1\na = [1]\nfor e in range(1, n):\n\tt = 4096\n\tfor s in range(e + 1):\n\t\tif d[s][e] != -1:\n\t\t\tt = min(t, a[s - 1] + 1 if s > 0 else a[s])\n\ta.append(t)\nprint(a[-1])\n" ]
There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different colors of coins from a single person. On the other hand, a person will give all of his/her coins of the color specified by Snuke. Snuke would like to maximize the total number of coins of all colors he gets. Find the maximum possible number of coins. Constraints * 1 \leq X * 1 \leq Y * 1 \leq Z * X+Y+Z \leq 10^5 * 1 \leq A_i \leq 10^9 * 1 \leq B_i \leq 10^9 * 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: X Y Z A_1 B_1 C_1 A_2 B_2 C_2 : A_{X+Y+Z} B_{X+Y+Z} C_{X+Y+Z} Output Print the maximum possible total number of coins of all colors he gets. Examples Input 1 2 1 2 4 4 3 2 1 7 6 7 5 2 3 Output 18 Input 3 3 2 16 17 1 2 7 5 2 16 12 17 7 7 13 2 10 12 18 3 16 15 19 5 6 2 Output 110 Input 6 2 4 33189 87907 277349742 71616 46764 575306520 8801 53151 327161251 58589 4337 796697686 66854 17565 289910583 50598 35195 478112689 13919 88414 103962455 7953 69657 699253752 44255 98144 468443709 2332 42580 752437097 39752 19060 845062869 60126 74101 382963164 Output 3093929975
{"inputs": ["1 2 1\n2 4 4\n0 2 1\n7 6 7\n5 2 3", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 88414 103962455\n7953 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 382963164", "3 3 2\n16 17 1\n2 7 5\n2 16 12\n17 7 7\n13 2 13\n12 18 3\n16 15 19\n5 6 2", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 88414 103962455\n7953 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 382963164", "3 3 2\n16 17 1\n2 7 5\n2 16 12\n17 7 7\n13 2 13\n12 18 3\n22 15 19\n5 6 2", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n7953 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 382963164", "3 3 2\n16 17 1\n2 7 5\n2 16 12\n17 7 7\n13 2 3\n12 18 3\n22 15 19\n5 6 2", "1 2 1\n2 4 4\n1 2 1\n7 6 2\n5 3 3", "3 3 2\n16 17 1\n2 7 5\n2 16 12\n17 7 7\n13 2 3\n12 13 3\n22 15 19\n5 6 2", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 7236 141577431\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 12598 141577431\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n61732 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 394223083\n8801 53151 327161251\n58589 12598 141577431\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n61732 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 394223083\n8801 53151 327161251\n58589 12598 141577431\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n74454 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 394223083\n8801 53151 327161251\n84495 18431 141577431\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n74454 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 394223083\n8801 53151 327161251\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n8201 1573 103962455\n9712 69657 699253752\n74454 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 394223083\n8801 53151 327161251\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n8201 1573 103962455\n9712 69657 699253752\n74454 98144 468443709\n2332 42580 87287886\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n25568 46764 394223083\n8801 53151 327161251\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n8201 1573 103962455\n9712 69657 699253752\n74454 98144 468443709\n2332 42580 87287886\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 88414 103962455\n7953 69657 699253752\n44255 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 245001003", "3 3 2\n16 17 1\n2 7 5\n2 0 12\n17 7 7\n13 2 10\n12 18 3\n16 15 19\n5 6 2", "1 2 1\n2 4 6\n0 2 1\n7 6 7\n5 2 3", "1 2 1\n2 4 4\n1 2 1\n7 6 11\n5 3 3", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n7953 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n66232 74101 382963164", "3 3 2\n16 17 1\n2 7 5\n2 16 12\n17 7 7\n13 2 3\n12 18 3\n31 15 19\n5 6 2", "1 2 1\n2 4 4\n1 2 1\n7 0 2\n5 3 3", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n42495 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 382963164", "3 3 2\n16 17 1\n2 3 5\n2 16 12\n17 7 7\n13 2 3\n12 13 3\n22 15 19\n9 6 2", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 7236 141577431\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 1041467256\n42489 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 12598 141577431\n66854 17565 289910583\n59976 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 394223083\n8801 53151 632227322\n58589 12598 141577431\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n61732 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 88414 103962455\n7953 69657 699253752\n44255 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n105264 74101 245001003", "1 2 1\n2 4 6\n0 2 1\n7 6 7\n9 2 3", "3 3 2\n16 17 1\n2 7 5\n2 21 12\n17 7 7\n13 2 3\n12 18 3\n31 15 19\n5 6 2", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n42495 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n69352 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 382963164", "3 3 2\n16 17 1\n2 7 5\n2 16 12\n17 7 7\n13 2 0\n13 13 3\n22 15 19\n5 6 2", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 7236 796697686\n57317 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 37860 845062869\n60126 74101 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 656167279\n8801 53151 327161251\n58589 12598 141577431\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n61732 98144 468443709\n2332 42580 752437097\n7996 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 394223083\n8801 53151 327161251\n58589 18431 250423809\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 882028374\n74454 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 85321 394223083\n8801 53151 218424419\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n8201 1573 103962455\n9712 69657 699253752\n74454 98144 468443709\n2332 42580 87287886\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 159130 277349742\n25568 46764 394223083\n8801 53151 327161251\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n8201 1573 103962455\n9712 69657 699253752\n74454 98144 468443709\n2332 29437 87287886\n18946 19060 845062869\n60126 62114 382963164", "1 2 1\n2 4 7\n3 2 2\n7 6 7\n5 4 3", "3 3 2\n16 17 1\n2 7 2\n2 0 12\n17 7 7\n13 2 10\n12 18 3\n16 15 26\n5 6 2", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 261301742\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 88414 103962455\n7953 69657 699253752\n42489 135203 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 115476935", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 11501 289910583\n50598 35195 478112689\n13919 1573 103962455\n7953 69657 699253752\n42489 36004 468443709\n2332 42580 752437097\n39752 19060 845062869\n66232 74101 382963164", "6 2 4\n33189 87907 277349742\n33905 85321 394223083\n8801 53151 218424419\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n8201 1573 103962455\n9712 69657 699253752\n74454 98144 468443709\n2332 42580 87287886\n39752 19060 845062869\n60126 62114 570675168", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n110501 17565 289910583\n50598 35195 478112689\n13919 88414 103962455\n7953 69657 699253752\n44255 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n105264 91167 245001003", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 261301742\n58589 4337 796697686\n66854 17565 289910583\n5650 35195 478112689\n13919 88414 103962455\n7953 69657 699253752\n42489 135203 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 115476935", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n44701 35195 478112689\n13919 1573 111724159\n9712 69657 699253752\n69352 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n104581 7236 796697686\n57317 17565 289910583\n69438 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 37860 845062869\n60126 74101 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 500599301\n8801 53151 632227322\n58589 12598 141577431\n66854 17565 130159932\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n61732 98144 468443709\n2332 42580 1492635034\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 394223083\n8801 53151 327161251\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n74454 37174 468443709\n2332 7005 752437097\n39752 19060 845062869\n40358 15102 382963164", "6 2 4\n33189 159130 277349742\n25568 46764 394223083\n8801 53151 327161251\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n8201 1573 103962455\n9712 69657 699253752\n74454 98144 468443709\n2332 29437 87287886\n18946 19060 715215246\n60126 62114 124089072", "3 3 2\n0 17 1\n2 7 5\n2 21 12\n27 2 7\n13 2 3\n12 18 3\n31 15 19\n5 6 2", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n44701 35195 478112689\n13919 1573 111724159\n9712 69657 699253752\n69352 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n72164 74101 382963164", "6 2 4\n33189 87907 179450675\n33905 46764 575306520\n8801 53151 327161251\n58589 10425 199827665\n66854 17565 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 1041467256\n42489 98144 468443709\n2332 42580 997425647\n39752 19060 845062869\n60126 74101 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n58589 7236 78600873\n66854 17565 289910583\n43805 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 36869 752437097\n39752 27851 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 85321 394223083\n8801 53151 3205876\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n8201 1573 103962455\n9712 69657 699253752\n74454 98144 496532349\n2332 42580 87287886\n39752 19060 845062869\n60126 62114 570675168", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n110501 17565 289910583\n50598 35195 478112689\n13919 118109 103962455\n7953 69657 699253752\n44255 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n105264 135743 245001003", "6 2 4\n33189 88609 25397593\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 88414 136264683\n7953 69657 699253752\n71858 98144 468443709\n2332 27840 752437097\n39752 19060 845062869\n60126 80230 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 74934 327161251\n58589 4337 796697686\n66854 17565 289910583\n44701 35195 478112689\n13919 1573 111724159\n9712 69657 699253752\n69352 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n72164 74101 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 327161251\n104581 7236 886275317\n57317 17565 289910583\n69438 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 65491 845062869\n60126 74101 382963164", "3 3 2\n16 17 1\n0 3 5\n2 20 12\n17 7 7\n13 2 3\n12 13 2\n22 15 22\n9 6 1", "6 2 4\n33189 87907 277349742\n33905 46764 656167279\n8801 53151 624602328\n58589 12598 214222412\n66854 17565 289910583\n50598 35195 478112689\n13919 1283 103962455\n9712 69657 699253752\n59705 98144 468443709\n2332 42580 752437097\n7996 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 500599301\n8801 53151 632227322\n58589 12598 141577431\n66854 17565 130159932\n50598 35195 478112689\n16833 1573 103962455\n9712 69657 699253752\n71970 98144 468443709\n2332 42580 1492635034\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 102169 277349742\n33905 46764 394223083\n9873 53151 327161251\n58589 12598 118670620\n66854 17565 173583719\n50598 35195 478112689\n13919 1573 42293659\n17814 69657 699253752\n74454 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 657407748\n6440 53151 213255275\n84495 18431 141577431\n66854 17565 100595605\n50598 56534 478112689\n13919 1573 103962455\n9712 69657 699253752\n74454 107411 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 394223083\n8801 15308 327161251\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n74454 37174 468443709\n2332 7005 752437097\n39752 19060 845062869\n40358 25029 382963164", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n110501 17565 289910583\n50598 35195 478112689\n13919 53191 103962455\n7953 69657 699253752\n44255 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n105264 135743 245001003", "6 2 4\n33189 88609 25397593\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 88414 136264683\n7953 69657 699253752\n71858 132580 468443709\n2332 27840 752437097\n39752 19060 845062869\n60126 80230 382963164", "3 3 2\n16 17 1\n2 7 5\n3 12 12\n17 9 7\n13 2 13\n12 18 0\n22 15 2\n6 6 0", "6 2 4\n33189 60740 277349742\n33905 46764 575306520\n8801 53151 327161251\n104581 7236 886275317\n57317 17565 289910583\n69438 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n39752 65491 845062869\n60126 74101 382963164", "3 3 2\n16 17 1\n0 3 5\n2 20 12\n17 7 7\n13 2 3\n12 17 2\n22 15 22\n9 6 1", "6 2 4\n33189 148451 277349742\n33905 46764 656167279\n8801 53151 624602328\n58589 12598 214222412\n66854 17565 289910583\n50598 35195 478112689\n13919 1283 103962455\n9712 69657 699253752\n59705 98144 468443709\n2332 42580 752437097\n7996 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 500599301\n8801 53151 632227322\n58589 12598 141577431\n66854 17565 130159932\n50598 35195 478112689\n16833 1573 103962455\n9712 69657 699253752\n71970 98144 468443709\n2332 42580 1492635034\n39752 19060 48797902\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 657407748\n6440 53151 213255275\n84495 18431 141577431\n66854 17565 100595605\n50598 56534 478112689\n13919 1573 103962455\n9712 69657 699253752\n74454 168808 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 394223083\n8490 15308 327161251\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n74454 37174 468443709\n2332 7005 752437097\n39752 19060 845062869\n40358 25029 382963164", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 261301742\n58589 4337 796697686\n66854 17565 4018814\n5650 35195 478112689\n13919 88414 103962455\n7953 69657 674703161\n42489 135203 468443709\n2332 42580 752437097\n39752 34092 845062869\n60126 21442 115476935", "6 2 4\n33189 147435 25397593\n33905 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 88414 136264683\n7953 69657 699253752\n71858 132580 468443709\n2332 27840 752437097\n39752 19060 845062869\n60126 80230 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n11508 53151 327161251\n58589 1173 78600873\n66854 17565 289910583\n43805 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 36869 752437097\n39752 26806 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 74934 327161251\n38899 4337 796697686\n66854 17565 289910583\n44701 35195 478112689\n16051 1573 111724159\n9712 69657 699253752\n69352 98144 468443709\n2332 42580 752437097\n21769 19060 845062869\n72164 74101 382963164", "6 2 4\n33189 148451 277349742\n33905 30594 656167279\n8801 53151 624602328\n58589 12598 214222412\n66854 17565 289910583\n50598 35195 478112689\n13919 1283 103962455\n9712 69657 699253752\n59705 98144 468443709\n2332 42580 1311285814\n7996 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 57442 277349742\n33905 46764 657407748\n6440 53151 213255275\n84495 18431 141577431\n66854 17565 100595605\n50598 56534 499531067\n13919 1573 103962455\n9712 69657 699253752\n74454 168808 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n33905 39844 394223083\n9170 53151 327161251\n129926 526 141577431\n66854 32775 289910583\n50598 35195 478112689\n8201 2368 134806813\n9712 69657 699253752\n74454 98144 468443709\n2144 42580 752437097\n39752 19060 845062869\n60126 62114 262490364", "3 3 2\n29 17 2\n2 7 5\n3 12 12\n21 9 7\n13 2 13\n12 18 0\n22 15 2\n6 6 0", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 45986694\n58589 5487 305429394\n66854 11501 289910583\n50598 35195 478112689\n13919 2504 103962455\n7953 69657 699253752\n42489 36004 343885277\n1315 40284 752437097\n39752 19060 845062869\n66232 74101 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 74934 327161251\n38899 4337 796697686\n66854 17565 289910583\n44701 35195 478112689\n25447 1573 111724159\n9712 69657 699253752\n69352 98144 468443709\n2332 42580 752437097\n21769 19060 845062869\n72164 74101 382963164", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n11508 53151 327161251\n58589 1173 78600873\n66854 17565 289910583\n43805 58381 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 36869 752437097\n39752 26806 845062869\n75132 62114 382963164", "6 2 4\n33189 148451 277349742\n33905 30594 656167279\n8801 53151 624602328\n58589 12598 214222412\n66854 17565 289910583\n50598 35195 478112689\n26829 1283 103962455\n9712 69657 699253752\n59705 98144 468443709\n2332 42580 1311285814\n7996 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 102169 344107733\n33905 46764 394223083\n9873 53151 327161251\n58589 12598 20784962\n66854 34525 173583719\n50598 35195 478112689\n13919 1573 42293659\n17814 69657 699253752\n74454 98144 468443709\n2332 42580 752437097\n39752 19060 1438466979\n60126 62114 382963164", "6 2 4\n33189 57442 277349742\n33905 46764 657407748\n6440 53151 213255275\n84495 18431 141577431\n66854 17565 100595605\n50598 56534 499531067\n13919 1573 103962455\n9712 69657 699253752\n74454 266085 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 62114 382963164", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 261301742\n58589 4337 796697686\n66854 17565 4018814\n5650 35195 478112689\n13919 88414 103962455\n6505 69657 721009150\n42275 135203 468443709\n2332 42580 752437097\n39752 34092 845062869\n60126 21442 115476935", "3 3 2\n29 17 2\n2 7 5\n3 12 12\n21 9 7\n13 2 15\n12 18 0\n22 15 2\n6 6 0", "3 3 2\n1 17 1\n2 7 5\n2 21 0\n27 1 7\n13 2 3\n12 26 3\n31 1 19\n5 6 2", "6 2 4\n33189 60740 277349742\n33905 29724 575306520\n8801 53151 327161251\n104581 7236 886275317\n68908 15379 289910583\n69438 35195 478112689\n13919 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 42580 752437097\n63908 65491 845062869\n60126 74101 382963164", "3 3 2\n16 17 1\n0 3 5\n0 20 12\n17 7 7\n5 2 2\n12 17 2\n22 15 22\n14 6 1", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n11508 53151 327161251\n58589 1173 78600873\n66854 17565 289910583\n43805 58381 478112689\n9339 1573 103962455\n9712 69657 699253752\n42489 98144 468443709\n2332 36869 752437097\n39752 26806 845062869\n75132 62114 382963164", "6 2 4\n33189 87907 402986692\n21176 46764 500599301\n8801 53151 1210998588\n58589 12598 141577431\n66854 17565 130159932\n50598 35195 478112689\n16833 1573 103962455\n9712 69657 699253752\n71970 98144 468443709\n1018 42580 1492635034\n39752 19060 48797902\n60126 62114 382963164", "6 2 4\n33189 87907 530443197\n33905 85321 394223083\n8801 27804 4040527\n84495 18431 141577431\n66854 32775 289910583\n50598 35195 478112689\n8201 1573 103962455\n9712 69657 699253752\n119260 98144 496532349\n2332 42580 74611723\n39752 22945 845062869\n60126 62114 570675168", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n12506 53151 261301742\n58589 4337 796697686\n66854 17565 4018814\n5650 35195 478112689\n13919 88414 103962455\n6505 69657 721009150\n42275 135203 468443709\n2332 42580 752437097\n39752 34092 845062869\n60126 21442 115476935", "6 2 4\n33189 87907 277349742\n33905 46764 575306520\n8801 53151 45986694\n58589 5487 305429394\n66854 11501 289910583\n50598 35195 478112689\n13919 2504 103962455\n7953 69657 699253752\n42489 36004 275737389\n1315 40284 143834229\n39752 19060 845062869\n66232 74101 382963164", "3 3 2\n1 17 1\n2 7 5\n2 21 0\n27 1 7\n23 2 3\n12 26 3\n31 1 19\n5 6 2", "1 2 1\n2 4 4\n3 2 1\n7 6 7\n5 2 3", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 88414 103962455\n7953 69657 699253752\n44255 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n60126 74101 382963164", "3 3 2\n16 17 1\n2 7 5\n2 16 12\n17 7 7\n13 2 10\n12 18 3\n16 15 19\n5 6 2"], "outputs": ["18\n", "3093929146\n", "111\n", "3093891435\n", "113\n", "3093871658\n", "110\n", "17\n", "105\n", "2872505176\n", "2872513114\n", "2775302590\n", "2775315312\n", "2775341218\n", "2775335500\n", "2491269990\n", "2491261653\n", "3093929975\n", "108\n", "19\n", "22\n", "3093877764\n", "119\n", "16\n", "3093863555\n", "109\n", "3214718680\n", "2872514554\n", "2929451082\n", "3093975113\n", "23\n", "124\n", "3093879113\n", "106\n", "3093862121\n", "2953373873\n", "2958089934\n", "2491277056\n", "2491332876\n", "20\n", "115\n", "3093966205\n", "3093866459\n", "2593522843\n", "3094018760\n", "3093921257\n", "3093881319\n", "3093880961\n", "3669649019\n", "2775321450\n", "2361485253\n", "134\n", "3093893357\n", "3459707230\n", "2872498383\n", "2611918647\n", "3094048455\n", "3093920569\n", "3093915140\n", "3183458592\n", "116\n", "2953371846\n", "3669651933\n", "2775329574\n", "2954652970\n", "2775289959\n", "3093998154\n", "3093925871\n", "112\n", "3183448224\n", "120\n", "2953432390\n", "3325194212\n", "2954700613\n", "2775289648\n", "3069370666\n", "3093979395\n", "2872501090\n", "3093917272\n", "3512281107\n", "2954692606\n", "2775380931\n", "128\n", "2872499977\n", "3093926668\n", "2872516096\n", "3512294017\n", "3368733684\n", "2954789883\n", "3115676655\n", "130\n", "142\n", "3183459815\n", "117\n", "2872511516\n", "3903965478\n", "2645901096\n", "3115680360\n", "2598126286\n", "152\n", "18", "3093929975", "110"]}
[ "from heapq import heappushpop\nimport sys\n(X, Y, Z) = map(int, sys.stdin.readline().split())\nN = X + Y + Z\nABC = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]\nABC.sort(key=lambda x: x[0] - x[1], reverse=True)\nGB = [None] * N\nQ = [a - c for (a, _, c) in ABC[:X]]\nQ.sort()\ngs = sum((a for (a, _, _) in ABC[:X]))\nGB[X - 1] = gs\nfor (i, (a, b, c)) in enumerate(ABC[X:X + Z], X):\n\tgs += -heappushpop(Q, a - c) + a\n\tGB[i] = gs\nSB = [None] * N\nQ = [b - c for (_, b, c) in ABC[X + Z:]]\nQ.sort()\nss = sum((b for (_, b, _) in ABC[X + Z:]))\nSB[-Y - 1] = ss\nfor (i, (a, b, c)) in enumerate(ABC[X + Z - 1:X - 1:-1], 1):\n\ti = -Y - i\n\tss += -heappushpop(Q, b - c) + b\n\tSB[i - 1] = ss\nprint(max((i + j for (i, j) in zip(GB[X - 1:X + Z], SB[X - 1:X + Z]))))\n", "from heapq import *\n(X, Y, Z) = map(int, input().split())\nN = X + Y + Z\nA = []\nq1 = []\nq2 = []\nL = [0]\nR = [0]\nfor _ in [0] * N:\n\tA.append([int(e) for e in input().split()])\nA.sort(key=lambda a: a[0] - a[1])\nfor i in range(N):\n\tL += [L[i] + A[i][1]]\n\theappush(q1, A[i][1] - A[i][2])\n\tR += [R[i] + A[-1 - i][0]]\n\theappush(q2, A[~i][0] - A[~i][2])\n\tif i >= Y:\n\t\tL[i + 1] -= heappop(q1)\n\tif i >= X:\n\t\tR[i + 1] -= heappop(q2)\nprint(max((L[i] + R[~i] for i in range(Y, N - X + 1))))\n", "import sys\nfrom heapq import heappush, heappushpop\n(X, Y, Z) = map(int, input().split())\nxyz = sorted([list(map(int, l.split())) for l in sys.stdin], key=lambda x: x[0] - x[1])\nuq = []\ncy = 0\nfor (x, y, z) in xyz[:Y]:\n\theappush(uq, y - z)\n\tcy += y\nLy = [cy]\nfor (x, y, z) in xyz[Y:Y + Z]:\n\tcy += y - heappushpop(uq, y - z)\n\tLy += [cy]\nlq = []\ncx = 0\nfor _ in [0] * X:\n\t(x, y, z) = xyz.pop()\n\theappush(lq, x - z)\n\tcx += x\nLx = [cx]\nfor _ in [0] * Z:\n\t(x, y, z) = xyz.pop()\n\tcx += x - heappushpop(lq, x - z)\n\tLx += [cx]\nprint(max(map(sum, zip(Lx, Ly[::-1]))))\n", "import sys\ninput = sys.stdin.readline\nfrom heapq import heappush, heappushpop\n(x, y, z) = map(int, input().split())\nABC = [tuple(map(int, input().split())) for _ in range(x + y + z)]\nABC.sort(key=lambda x: x[0] - x[1])\ncy = 0\nhq = []\nfor (a, b, c) in ABC[:y]:\n\tcy += b\n\theappush(hq, b - c)\nLy = [cy]\nfor (a, b, c) in ABC[y:y + z]:\n\tcy += b - heappushpop(hq, b - c)\n\tLy.append(cy)\ncx = 0\nhq = []\nfor (a, b, c) in ABC[y + z:]:\n\tcx += a\n\theappush(hq, a - c)\nLx = [cx]\nfor (a, b, c) in reversed(ABC[y:y + z]):\n\tcx += a - heappushpop(hq, a - c)\n\tLx.append(cx)\nprint(max((i + j for (i, j) in zip(Lx[::-1], Ly))))\n", "import heapq\n(X, Y, Z) = map(int, input().split())\nABC = []\nfor i in range(X + Y + Z):\n\t(a, b, c) = map(int, input().split())\n\tABC.append((b - a, a, b, c))\nABC.sort()\nq = []\nansX = []\nnow = 0\nfor i in range(X):\n\t(tmp, a, b, c) = ABC[i]\n\theapq.heappush(q, a - c)\n\tnow += a\nansX.append(now)\nfor i in range(X, X + Z):\n\t(tmp, a, b, c) = ABC[i]\n\theapq.heappush(q, a - c)\n\tnow += a\n\tpp = heapq.heappop(q)\n\tnow -= pp\n\tansX.append(now)\nABC.reverse()\nq = []\nansY = []\nnow = 0\nfor i in range(Y):\n\t(tmp, a, b, c) = ABC[i]\n\theapq.heappush(q, b - c)\n\tnow += b\nansY.append(now)\nfor i in range(Y, Y + Z):\n\t(tmp, a, b, c) = ABC[i]\n\theapq.heappush(q, b - c)\n\tnow += b\n\tpp = heapq.heappop(q)\n\tnow -= pp\n\tansY.append(now)\nansY.reverse()\nans = 0\nfor i in range(len(ansX)):\n\tans = max(ans, ansX[i] + ansY[i])\nprint(ans)\n", "import heapq\n(x, y, z) = [int(item) for item in input().split()]\ngsb = []\nfor i in range(x + y + z):\n\tgsb.append([int(item) for item in input().split()])\ngsb.sort(key=lambda x: x[0] - x[1], reverse=True)\ng_sum = sum((item[0] for item in gsb[:x]))\ns_sum = sum((item[1] for item in gsb[x + z:x + y + z]))\nb_sum = sum((item[2] for item in gsb[x:x + z]))\ngb_pq = [a - c for (a, b, c) in gsb[:x]]\nsb_pq = [b - c for (a, b, c) in gsb[x + z:x + y + z]]\nheapq.heapify(gb_pq)\nheapq.heapify(sb_pq)\nans_gb = [0]\ngb_total_delta = 0\nfor (a, b, c) in gsb[x:x + z]:\n\tnew_gb = a - c\n\tsmall_gb = heapq.heappushpop(gb_pq, new_gb)\n\tgb_total_delta += new_gb - small_gb\n\tans_gb.append(gb_total_delta)\nans_sb = [0]\nsb_total_delta = 0\nfor (a, b, c) in gsb[x:x + z][::-1]:\n\tnew_sb = b - c\n\tsmall_sb = heapq.heappushpop(sb_pq, new_sb)\n\tsb_total_delta += new_sb - small_sb\n\tans_sb.append(sb_total_delta)\nans_sb.reverse()\nmax_delta = 0\nfor (gb, sb) in zip(ans_gb, ans_sb):\n\tmax_delta = max(max_delta, gb + sb)\nprint(g_sum + s_sum + b_sum + max_delta)\n", "import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools\nsys.setrecursionlimit(10 ** 7)\ninf = 10 ** 20\neps = 1.0 / 10 ** 10\nmod = 10 ** 9 + 7\ndd = [(-1, 0), (0, 1), (1, 0), (0, -1)]\nddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]\n\ndef LI():\n\treturn [int(x) for x in sys.stdin.readline().split()]\n\ndef LI_():\n\treturn [int(x) - 1 for x in sys.stdin.readline().split()]\n\ndef LF():\n\treturn [float(x) for x in sys.stdin.readline().split()]\n\ndef LS():\n\treturn sys.stdin.readline().split()\n\ndef I():\n\treturn int(sys.stdin.readline())\n\ndef F():\n\treturn float(sys.stdin.readline())\n\ndef S():\n\treturn input()\n\ndef pf(s):\n\treturn print(s, flush=True)\n\ndef main():\n\t(X, Y, Z) = LI()\n\txyz = sorted([LI() for _ in range(X + Y + Z)], key=lambda x: x[0] - x[1])\n\tys = xyz[:Y]\n\tyq = []\n\ttr = 0\n\tfor (x, y, z) in ys:\n\t\theapq.heappush(yq, y - z)\n\t\ttr += y\n\tya = [tr]\n\tfor i in range(Z):\n\t\t(x, y, z) = xyz[Y + i]\n\t\ttr += y\n\t\theapq.heappush(yq, y - z)\n\t\tt = heapq.heappop(yq)\n\t\ttr -= t\n\t\tya.append(tr)\n\txs = xyz[Y + Z:]\n\txq = []\n\ttr = 0\n\tfor (x, y, z) in xs:\n\t\theapq.heappush(xq, x - z)\n\t\ttr += x\n\txa = [tr]\n\tfor i in range(Z):\n\t\t(x, y, z) = xyz[-X - i - 1]\n\t\ttr += x\n\t\theapq.heappush(xq, x - z)\n\t\tt = heapq.heappop(xq)\n\t\ttr -= t\n\t\txa.append(tr)\n\tr = 0\n\tfor (a, b) in zip(ya, xa[::-1]):\n\t\ttr = a + b\n\t\tif r < tr:\n\t\t\tr = tr\n\treturn r\nprint(main())\n", "import heapq\n\ndef main():\n\t(X, Y, Z) = map(int, input().split())\n\tN = X + Y + Z\n\tP = [None] * N\n\tfor i in range(N):\n\t\t(A, B, C) = map(int, input().split())\n\t\tP[i] = (A, B, C)\n\tP.sort(key=lambda x: x[0] - x[1])\n\tlsum = [0] * (Z + 1)\n\tlsum[0] = sum(map(lambda x: x[1], P[:Y]))\n\th = list(map(lambda x: x[1] - x[2], P[:Y]))\n\theapq.heapify(h)\n\tfor i in range(Z):\n\t\theapq.heappush(h, P[Y + i][1] - P[Y + i][2])\n\t\tlsum[i + 1] = lsum[i] - heapq.heappop(h) + P[Y + i][1]\n\trsum = [0] * (Z + 1)\n\trsum[0] = sum(map(lambda x: x[0], P[N - X:]))\n\th = list(map(lambda x: x[0] - x[2], P[N - X:]))\n\theapq.heapify(h)\n\tfor i in range(Z):\n\t\theapq.heappush(h, P[N - X - 1 - i][0] - P[N - X - 1 - i][2])\n\t\trsum[i + 1] = rsum[i] - heapq.heappop(h) + P[N - X - 1 - i][0]\n\tans = 0\n\tfor i in range(Z + 1):\n\t\tans = max(lsum[i] + rsum[Z - i], ans)\n\tprint(ans)\nmain()\n", "import heapq\n(X, Y, Z) = list(map(int, input().split()))\nA = []\nB = []\nC = []\nN = X + Y + Z\nfor i in range(N):\n\t(tmp_a, tmp_b, tmp_c) = list(map(int, input().split()))\n\tA.append(tmp_a)\n\tB.append(tmp_b)\n\tC.append(tmp_c)\ngold_minus_silver = [(a - b, a, b, c) for (a, b, c) in zip(A, B, C)]\ngold_minus_silver.sort()\nleft_side = []\nfor i in range(0, Y):\n\theapq.heappush(left_side, (gold_minus_silver[i][2] - gold_minus_silver[i][3], gold_minus_silver[i][2], gold_minus_silver[i][3]))\nleft_max = [0 for i in range(Z + 1)]\nfor i in range(0, Y):\n\tleft_max[0] += left_side[i][1]\nleft_bronze = []\nfor K in range(1, Z + 1):\n\theapq.heappush(left_side, (gold_minus_silver[K + Y - 1][2] - gold_minus_silver[K + Y - 1][3], gold_minus_silver[K + Y - 1][2], gold_minus_silver[K + Y - 1][3]))\n\tleft_max[K] = left_max[K - 1] + gold_minus_silver[K + Y - 1][2]\n\tbronze = heapq.heappop(left_side)\n\tleft_max[K] += bronze[2] - bronze[1]\nright_side = []\nfor i in range(Y + Z, N):\n\theapq.heappush(right_side, (gold_minus_silver[i][1] - gold_minus_silver[i][3], gold_minus_silver[i][1], gold_minus_silver[i][3]))\nright_max = [0 for i in range(Z + 1)]\nfor i in range(0, X):\n\tright_max[Z] += right_side[i][1]\nright_bronze = []\nfor K in range(Z - 1, -1, -1):\n\theapq.heappush(right_side, (gold_minus_silver[K + Y][1] - gold_minus_silver[K + Y][3], gold_minus_silver[K + Y][1], gold_minus_silver[K + Y][3]))\n\tright_max[K] = right_max[K + 1] + gold_minus_silver[K + Y][1]\n\tbronze = heapq.heappop(right_side)\n\tright_max[K] += bronze[2] - bronze[1]\nans = 0\nfor i in range(0, Z + 1):\n\tif ans < left_max[i] + right_max[i]:\n\t\tans = left_max[i] + right_max[i]\nprint(ans)\n", "from heapq import heapify, heappushpop\nfrom itertools import accumulate\n(X, Y, Z, *ABC) = map(int, open(0).read().split())\nP = sorted(zip(*[iter(ABC)] * 3), key=lambda t: t[0] - t[1])\nG = sum((t[0] for t in P[-X:]))\nS = sum((t[1] for t in P[:Y]))\nC = sum((t[2] for t in P[Y:-X]))\nQg = [a - c for (a, b, c) in P[-X:]]\nheapify(Qg)\nB = [0] + [a - c - heappushpop(Qg, a - c) for (a, b, c) in reversed(P[Y:-X])]\nQs = [b - c for (a, b, c) in P[:Y]]\nheapify(Qs)\nF = [0] + [b - c - heappushpop(Qs, b - c) for (a, b, c) in P[Y:-X]]\nprint(G + S + C + max((a + b for (a, b) in zip(accumulate(F), reversed(list(accumulate(B)))))))\n", "import heapq\n(X, Y, Z) = map(int, input().split())\nN = X + Y + Z\nsrc = [tuple(map(int, input().split())) for i in range(N)]\nsrc.sort(key=lambda x: x[0] - x[1])\nl_opt = [0] * (N + 1)\nr_opt = [0] * (N + 1)\nsilver = bronze = 0\nq_sb = []\nheapq.heapify(q_sb)\nfor (i, (g, s, b)) in enumerate(src):\n\theapq.heappush(q_sb, (s - b, s, b))\n\tsilver += s\n\tif i >= Y:\n\t\t(_, s2, b2) = heapq.heappop(q_sb)\n\t\tsilver -= s2\n\t\tbronze += b2\n\tl_opt[i + 1] = silver + bronze\ngold = bronze = 0\nq_gb = []\nheapq.heapify(q_gb)\nfor (i, (g, s, b)) in enumerate(reversed(src)):\n\theapq.heappush(q_gb, (g - b, g, b))\n\tgold += g\n\tif i >= X:\n\t\t(_, g2, b2) = heapq.heappop(q_gb)\n\t\tgold -= g2\n\t\tbronze += b2\n\tr_opt[N - 1 - i] = gold + bronze\nans = 0\nfor (l, r) in list(zip(l_opt, r_opt))[Y:Y + Z + 1]:\n\tans = max(ans, l + r)\nprint(ans)\n", "import sys\nfrom heapq import heappush, heappop\ninput = sys.stdin.readline\n(X, Y, Z) = map(int, input().split())\nN = X + Y + Z\n(A, B, C) = ([], [], [])\nfor i in range(N):\n\t(a, b, c) = map(int, input().split())\n\tA.append(a)\n\tB.append(b)\n\tC.append(c)\ngs = [(A[i] - B[i], i) for i in range(N)]\ngs = sorted(gs)\nsilver = [0] * (Z + 1)\nsb = []\nfor i in range(Y):\n\tj = gs[i][1]\n\theappush(sb, (B[j] - C[j], j))\n\tsilver[0] += B[j]\nfor i in range(Z):\n\tj = gs[i + Y][1]\n\theappush(sb, (B[j] - C[j], j))\n\tsilver[i + 1] += silver[i] + B[j]\n\tk = heappop(sb)\n\tsilver[i + 1] -= k[0]\ngs = sorted(gs, reverse=True)\ngold = [0] * (Z + 1)\ngb = []\nfor i in range(X):\n\tj = gs[i][1]\n\theappush(gb, (A[j] - C[j], j))\n\tgold[0] += A[j]\nfor i in range(Z):\n\tj = gs[i + X][1]\n\theappush(gb, (A[j] - C[j], j))\n\tgold[i + 1] += gold[i] + A[j]\n\tk = heappop(gb)\n\tgold[i + 1] -= k[0]\nans = 0\nfor i in range(Z + 1):\n\tans = max(ans, silver[i] + gold[Z - i])\nprint(ans)\n", "from heapq import heappush, heappop\n(x, y, z) = map(int, input().split())\nn = x + y + z\nabc = [list(map(int, input().split())) for i in range(n)]\nabc.sort(key=lambda t: t[0] - t[1], reverse=True)\nxls = []\nansx = 0\nfor i in range(x):\n\t(a, b, c) = abc[i]\n\theappush(xls, (a - c, i))\n\tansx += a\nyls = []\nansy = 0\nfor i in range(n - y, n):\n\t(a, b, c) = abc[i]\n\theappush(yls, (b - c, i))\n\tansy += b\nansls = [[0 for i in range(2)] for j in range(z + 1)]\nansls[0][0] = ansx\nansls[-1][1] = ansy\nfor (d, ls, w, ans) in ((0, xls, range(x, x + z), ansx), (1, yls, range(n - y - 1, x - 1, -1), ansy)):\n\tfor i in w:\n\t\t(a, b, c) = abc[i]\n\t\tif d == 0:\n\t\t\theappush(ls, (a - c, i))\n\t\telse:\n\t\t\theappush(ls, (b - c, i))\n\t\t(p, q) = heappop(ls)\n\t\tans += -p\n\t\tif d == 0:\n\t\t\tans += a\n\t\t\tansls[i - x + 1][0] = ans\n\t\telse:\n\t\t\tans += b\n\t\t\tansls[i - n + y + z][1] = ans\nprint(max([sum(ansls[i]) for i in range(z + 1)]))\n", "from heapq import heapify, heappush, heappop\n(X, Y, Z) = map(int, input().split())\nW = X + Y + Z\nABCs = [tuple(map(int, input().split())) for _ in range(W)]\nABCs.sort(key=lambda x: x[0] - x[1])\nsumBCs = [0] * W\nsumB = sumC = 0\nPQ = []\nfor i in range(W):\n\t(A, B, C) = ABCs[i]\n\theappush(PQ, (B - C, i))\n\tsumB += B\n\tif len(PQ) > Y:\n\t\t(_, j) = heappop(PQ)\n\t\t(A, B, C) = ABCs[j]\n\t\tsumB -= B\n\t\tsumC += C\n\tsumBCs[i] = sumB + sumC\nsumACs = [0] * W\nsumA = sumC = 0\nPQ = []\nfor i in reversed(range(W)):\n\t(A, B, C) = ABCs[i]\n\theappush(PQ, (A - C, i))\n\tsumA += A\n\tif len(PQ) > X:\n\t\t(_, j) = heappop(PQ)\n\t\t(A, B, C) = ABCs[j]\n\t\tsumA -= A\n\t\tsumC += C\n\tsumACs[i] = sumA + sumC\nans = 0\nfor i in range(Y - 1, W - X):\n\tsumABC = sumBCs[i] + sumACs[i + 1]\n\tans = max(ans, sumABC)\nprint(ans)\n", "import sys\nread = sys.stdin.buffer.read\nreadline = sys.stdin.buffer.readline\nreadlines = sys.stdin.buffer.readlines\nfrom heapq import heappop, heappush, heappushpop\n(X, Y, Z) = map(int, readline().split())\nm = map(int, read().split())\nABC = sorted(zip(m, m, m), key=lambda x: x[1] - x[0])\nAC = [0] * (X + Y + Z)\nq = []\nS = 0\nfor (i, (a, b, c)) in enumerate(ABC):\n\tS += a\n\td = a - c\n\tif len(q) < X:\n\t\theappush(q, d)\n\telse:\n\t\tS -= heappushpop(q, d)\n\tAC[i] = S\nBC = [0] * (X + Y + Z)\nq = []\nS = 0\nfor (i, (a, b, c)) in enumerate(ABC[::-1]):\n\tS += b\n\td = b - c\n\tif len(q) < Y:\n\t\theappush(q, d)\n\telse:\n\t\tS -= heappushpop(q, d)\n\tBC[i] = S\nBC = BC[::-1]\nanswer = max((x + y for (x, y) in zip(AC[X - 1:X + Z], BC[X:])))\nprint(answer)\n", "from heapq import heapify, heappushpop\nfrom itertools import accumulate\n(x, y, z) = map(int, input().split())\npersons = [list(map(int, input().split())) for _ in range(x + y + z)]\npersons.sort(key=lambda abc: abc[0] - abc[1])\nans_g = sum((x[0] for x in persons[-x:]))\nans_s = sum((x[1] for x in persons[:y]))\nans_c = sum((x[2] for x in persons[y:-x]))\ngold_pq = [a - c for (a, b, c) in persons[-x:]]\nsilver_pq = [b - c for (a, b, c) in persons[:y]]\nheapify(gold_pq)\nheapify(silver_pq)\nans_f = [0]\nfor (a, b, c) in persons[y:-x]:\n\tnp = b - c\n\trp = heappushpop(silver_pq, np)\n\tans_f.append(np - rp)\nans_b = [0]\nfor (a, b, c) in persons[-x - 1:y - 1:-1]:\n\tnp = a - c\n\trp = heappushpop(gold_pq, np)\n\tans_b.append(np - rp)\nans_f = list(accumulate(ans_f))\nans_b = list(accumulate(ans_b))\nprint(ans_g + ans_s + ans_c + max((sum(z) for z in zip(ans_f, reversed(ans_b)))))\n", "import sys\ninput = sys.stdin.readline\nfrom heapq import heappush, heappushpop\n(X, Y, Z) = map(int, input().split())\nABC = [[int(x) for x in input().split()] for _ in range(X + Y + Z)]\nABC.sort(key=lambda x: x[0] - x[1], reverse=True)\nq = []\nsum_a = 0\nsum_c = 0\nfor (a, b, c) in ABC[:X]:\n\theappush(q, (a - c, a))\n\tsum_a += a\nA = [0] * (Z + 1)\nLC = [0] * (Z + 1)\nA[0] = sum_a\nfor (i, (a, b, c)) in enumerate(ABC[X:X + Z], 1):\n\tsum_a += a\n\t(x, del_a) = heappushpop(q, (a - c, a))\n\tsum_a -= del_a\n\tsum_c += del_a - x\n\tA[i] = sum_a\n\tLC[i] = sum_c\nABC_rev = ABC[::-1]\nq = []\nsum_b = 0\nsum_c = 0\nfor (a, b, c) in ABC_rev[:Y]:\n\theappush(q, (b - c, b))\n\tsum_b += b\nB = [0] * (Z + 1)\nRC = [0] * (Z + 1)\nB[0] += sum_b\nfor (i, (a, b, c)) in enumerate(ABC_rev[Y:Y + Z], 1):\n\tsum_b += b\n\t(x, del_b) = heappushpop(q, (b - c, b))\n\tsum_b -= del_b\n\tsum_c += del_b - x\n\tB[i] = sum_b\n\tRC[i] = sum_c\nanswer = max((sum(x) for x in zip(A, LC, B[::-1], RC[::-1])))\nprint(answer)\n", "import heapq, sys\ninput = sys.stdin.readline\n(X, Y, Z) = map(int, input().split())\nN = X + Y + Z\ncoin = [tuple(map(int, input().split())) for i in range(N)]\ncoin.sort(key=lambda x: x[0] - x[1])\ny = [0] * N\nS = 0\nn = 0\nque = []\nfor i in range(N):\n\tval = coin[i][1] - coin[i][2]\n\tif Y > n:\n\t\theapq.heappush(que, val)\n\t\tS += val\n\t\tn += 1\n\t\ty[i] = S\n\telse:\n\t\tif que[0] < val:\n\t\t\tS += val - que[0]\n\t\t\theapq.heappop(que)\n\t\t\theapq.heappush(que, val)\n\t\ty[i] = S\nx = [0] * N\nS = 0\nn = 0\nque = []\nfor i in range(N - 1, -1, -1):\n\tval = coin[i][0] - coin[i][2]\n\tif X > n:\n\t\theapq.heappush(que, val)\n\t\tS += val\n\t\tn += 1\n\t\tx[i] = S\n\telse:\n\t\tif que[0] < val:\n\t\t\tS += val - que[0]\n\t\t\theapq.heappop(que)\n\t\t\theapq.heappush(que, val)\n\t\tx[i] = S\nbase = sum((coin[i][2] for i in range(N)))\nans = -1\nfor i in range(N):\n\tif i >= Y - 1 and N - (i + 1) >= X:\n\t\ttemp = base + x[i + 1] + y[i]\n\t\tans = max(ans, temp)\nprint(ans)\n", "(X, Y, Z) = map(int, input().split())\nans = 0\nBC = []\nfor _ in range(X + Y + Z):\n\t(a, b, c) = map(int, input().split())\n\tans += a\n\tBC.append([b - a, c - a])\nBC.sort(key=lambda x: x[1] - x[0])\nimport heapq\nq = []\nan = 0\nfor (b, _) in BC[:Y]:\n\theapq.heappush(q, b)\n\tan += b\nA = [an]\nfor (b, _) in BC[Y:-Z]:\n\theapq.heappush(q, b)\n\tan += b\n\tb_ = heapq.heappop(q)\n\tan -= b_\n\tA.append(an)\nq = []\nan = 0\nfor (_, c) in BC[-Z:]:\n\theapq.heappush(q, c)\n\tan += c\nA[-1] += an\nfor (i, (_, c)) in enumerate(BC[-Z - 1:Y - 1:-1], 2):\n\theapq.heappush(q, c)\n\tan += c\n\tc_ = heapq.heappop(q)\n\tan -= c_\n\tA[-i] += an\nprint(ans + max(A))\n" ]
Implement a function called makeAcronym that returns the first letters of each word in a passed in string. Make sure the letters returned are uppercase. If the value passed in is not a string return 'Not a string'. If the value passed in is a string which contains characters other than spaces and alphabet letters, return 'Not letters'. If the string is empty, just return the string itself: "". **EXAMPLES:** ``` 'Hello codewarrior' -> 'HC' 'a42' -> 'Not letters' 42 -> 'Not a string' [2,12] -> 'Not a string' {name: 'Abraham'} -> 'Not a string' ```
{"fn_name": "make_acronym", "inputs": [["My aunt sally"], ["Please excuse my dear aunt Sally"], ["How much wood would a woodchuck chuck if a woodchuck could chuck wood"], ["Unique New York"], ["a42"], ["1111"], [64], [[]], [{}], [""]], "outputs": [["MAS"], ["PEMDAS"], ["HMWWAWCIAWCCW"], ["UNY"], ["Not letters"], ["Not letters"], ["Not a string"], ["Not a string"], ["Not a string"], [""]]}
[ "def make_acronym(phrase):\n\ttry:\n\t\treturn ''.join((word[0].upper() if word.isalpha() else 0 for word in phrase.split()))\n\texcept AttributeError:\n\t\treturn 'Not a string'\n\texcept TypeError:\n\t\treturn 'Not letters'\n", "from operator import itemgetter\n\ndef make_acronym(phrase):\n\tif type(phrase) != str:\n\t\treturn 'Not a string'\n\tif not all((c.isalpha() or c.isspace() for c in phrase)):\n\t\treturn 'Not letters'\n\treturn ''.join(map(itemgetter(0), phrase.split())).upper()\n", "def make_acronym(phrase):\n\tif not isinstance(phrase, str):\n\t\treturn 'Not a string'\n\tarr = phrase.split()\n\treturn ''.join((a[0] for a in arr)).upper() if all(map(str.isalpha, arr)) else 'Not letters'\n", "def make_acronym(phrase):\n\tif not isinstance(phrase, str):\n\t\treturn 'Not a string'\n\telif phrase == '':\n\t\treturn ''\n\telif not phrase.replace(' ', '').isalpha():\n\t\treturn 'Not letters'\n\telse:\n\t\treturn ''.join((word[0].upper() for word in phrase.split(' ')))\n", "def make_acronym(phrase):\n\tif isinstance(phrase, str):\n\t\twords = phrase.split()\n\t\tif all((x.isalpha() or x.isspace() for x in words)):\n\t\t\treturn ''.join((x[0] for x in words)).upper()\n\t\telse:\n\t\t\treturn 'Not letters'\n\treturn 'Not a string'\n", "from string import ascii_letters\n\ndef make_acronym(phrase):\n\tacronym = ''\n\tif isinstance(phrase, str):\n\t\twords = phrase.split()\n\t\tfor word in words:\n\t\t\tfor char in word:\n\t\t\t\tif char in ascii_letters:\n\t\t\t\t\tpass\n\t\t\t\telse:\n\t\t\t\t\treturn 'Not letters'\n\t\t\tacronym += word[0].upper()\n\t\treturn acronym\n\treturn 'Not a string'\n", "def make_acronym(phrase):\n\tif not isinstance(phrase, str):\n\t\treturn 'Not a string'\n\twords = phrase.split()\n\tif not all((i.isalpha() for i in words)):\n\t\treturn 'Not letters'\n\treturn ''.join((p[0] for p in words)).upper()\n", "def make_acronym(phrase):\n\timport re\n\tif type(phrase) is not str:\n\t\treturn 'Not a string'\n\tif re.search('[^A-Za-z\\\\s]', phrase):\n\t\treturn 'Not letters'\n\tacronym = ''\n\tfor x in phrase.split():\n\t\tacronym += x[0]\n\treturn acronym.upper()\n" ]
"When little Petya grew up and entered the university, he started to take part in АСМ contests. L(...TRUNCATED)
"{\"inputs\": [\"7 6\\nAlena\\nOlya\\nVanya\\nBrus\\nJohn\\nAlice\\nMariana\\nAlena John\\nAlena Ali(...TRUNCATED)
["tmp = input().split(' ')\nN_ppl = int(tmp[0])\nN_cstrt = int(tmp[1])\nppl_to_id = {}\nid_ppl = [0](...TRUNCATED)
"# Solve For X\n\nYou will be given an equation as a string and you will need to [solve for X](https(...TRUNCATED)
"{\"fn_name\": \"solve_for_x\", \"inputs\": [[\"x - 5 = 20\"], [\"5 * x + 5 = 30\"], [\"20 = 5 * x -(...TRUNCATED)
["from itertools import count\n\ndef solve_for_x(equation):\n\treturn next((x for n in count(0) for (...TRUNCATED)
"Write a function that takes an array/list of numbers and returns a number such that \n\nExplanation(...TRUNCATED)
"{\"fn_name\": \"total\", \"inputs\": [[[1, 2, 3, 4, 5]], [[1, 2, 3, 4]], [[1, 2, 3]], [[4, 4, 52, 2(...TRUNCATED)
["def total(arr):\n\twhile len(arr) > 1:\n\t\tarr = [x + y for (x, y) in zip(arr, arr[1:])]\n\tretur(...TRUNCATED)
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
45