math *1500

Please click on ads to support us..

Python Code:

n = int(input())
diff = list(map(int,input().split()))
nums = [1]
for i in diff:
    nums.append(nums[-1]+i)
mini = min(nums)
extra = 0
if mini<=0:
    extra = (-1)*mini + 1
for i in range(len(nums)):
    nums[i]+=extra
result = [i for i in range(1,n+1)]
if sorted(nums)==result:
    print(*nums)
else:
    print(-1)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector <int> vi;
typedef pair <int, int> pii;
#define PB push_back

void solve () {
    int n;
    cin >> n;
    int maxi = 0, current = 0;
    vi diff;
    for (int i = 0; i < n - 1; i ++) {
        int q;
        cin >> q;
        diff.PB (q);
        current += q;
        maxi = max (maxi, current);
    }
    int num = n - maxi;
    vi array, ref;
    array.PB (num);
    ref.PB (num);
    for (int i = 0; i < n - 1; i ++) {
        num += diff [i];
        array.PB (num);
        ref.PB (num);
    }
    sort (ref.begin (), ref.end ());
    for (int i = 0; i < n; i ++) {
        if (ref [i] != i + 1) {
            cout << "-1\n";
            return;
        }
    }
    for (int i = 0; i < n; i ++) {
        if (i == n - 1) cout << array [i] << "\n";
        else cout << array [i] << " ";
    }
    
}
 
int main () {
    ios_base :: sync_with_stdio (0);
    cin.tie (0);
    solve ();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti