C. Polycarp Restores Permutation(思路题)

An array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each number from 11 to nn exactly once. For example, the following arrays are permutations: [3,1,2][3,1,2], [1][1], [1,2,3,4,5][1,2,3,4,5] and [4,3,1,2][4,3,1,2]. The following arrays are not permutations: [2][2], [1,1][1,1], [2,3,4][2,3,4].

Polycarp invented a really cool permutation p1,p2,…,pnp1,p2,…,pn of length nn. It is very disappointing, but he forgot this permutation. He only remembers the array q1,q2,…,qn−1q1,q2,…,qn−1 of length n−1n−1, where qi=pi+1−piqi=pi+1−pi.

Given nn and q=q1,q2,…,qn−1q=q1,q2,…,qn−1, help Polycarp restore the invented permutation.

Input

The first line contains the integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of the permutation to restore. The second line contains n−1n−1 integers q1,q2,…,qn−1q1,q2,…,qn−1 (−n<qi<n−n<qi<n).

Output

Print the integer -1 if there is no such permutation of length nn which corresponds to the given array qq. Otherwise, if it exists, print p1,p2,…,pnp1,p2,…,pn. Print any such permutation if there are many of them.

Examples

input

3
-2 1

output

3 1 2 

input

5
1 1 1 1

output

1 2 3 4 5 

input

4
-1 2 2

output

-1

题意:给数字n,表示数组大小,然后是n-1个数字,表示后一位减去前一位的值

求回复原数组的值(原数组内1——n各出现一次)不能输出-1。

 

#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<iomanip>
#define mem(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N=200005;
const int inf=0x3f3f3f3f;
//priority_queue<int,vector<int>,greater<int> >q;
const int MAXN=6;
const ll mod=123456789;
int ans;
int a[200005];
int b[N];
int p[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
        scanf("%d",&a[i]),p[i]=p[i-1]+a[i];
    int mi=p[0];
    for(int i=1;i<n;i++)
        mi=min(mi,p[i]);
    for(int i=0;i<n;i++)
    {
        if(p[i]-mi+1>n||b[p[i]-mi+1])
        {
            printf("-1\n");
            return 0;
        }
        b[p[i]-mi+1]=1;
    }
    for(int i=0;i<n;i++)
        printf("%d ",p[i]-mi+1);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值