WOJ1474 Young Frog's Life Cycle

Time Limit: 1 second

Memory Limit: 512 mebibytes
As we know, frog is one of the world’s oldest amphibious animal, so the frogs have a special counting method of their own, each of them has a deck of nnn mogic stones labeled with 0,1,2,....(n1)0,1,2,....(n-1)0,1,2,....(n−1).

Initially, the stones are placed in the order p1,p2,p3...pnp_1,p_2,p_3...p_np1​,p2​,p3​...pn​from top to bottom. In each round, if the top stones labeled with xxx, our young frog will place it xxx stones downward, so it becomes the stones number (x+1)(x+1)(x+1) in the deck, counting from 1, the relative order of other stones will not be changed, and then our yound frog will become one second older.

How many seconds will the frog get until the mogic stone labeled with 000 comes to the top?

Input

The first line contains an integer n(1n20)n(1\le n \le 20)n(1≤n≤20)
The second line contains nnn distinct integer p1,p2,pn(0pin)p_1,p_2,\ldots p_n(0 \le p_i \le n)p1​,p2​,…pn​(0≤pi​≤n).

Output

Output an integer which denotes the number of seconds.If the stone labeled with 0 never comes to the top, output -1s instead.

Examples

Sample Input 1

5
1 3 2 4 0

Sample Output 1

13

Sample Input 2

2
0 1

Sample Output 2

0

Note

Why not play it by yourself to find something interesting?

——————,——————。

#include <stdio.h>
#define MAX 20
int main()
{
    int arr1[MAX], arr2[MAX];
    int i, j;
    int n;
    int age = 0;
    scanf("%d", &n);
    for(i=0; i<n; i++)
        scanf("%d", &arr1[i]);
    while(arr1[0] != 0)
    {
        for(j=0; j<arr1[0]; j++)
            arr2[j] = arr1[j+1];
        arr2[j++] = arr1[0];
        for(; j<n; j++)
            arr2[j] = arr1[j];
        age++;
        for(i=0; i<n; i++)
            arr1[i] = arr2[i];
    }
    printf("%d\n", age);

    return 0;
}
WOJ1474 Young Frog's Life CycleWOJ1474 Young Frog's Life Cycle deathpure 发布了2 篇原创文章 · 获赞 0 · 访问量 8 私信 关注
上一篇:My very beginning of another life--暨linux命令中英文对照


下一篇:【雅思】雅思核心词汇精讲-unit 4