1285B - Just Eat It - CodeForces Solution


dp greedy implementation *1300

Please click on ads to support us..

Python Code:

t = int(input())
 
for _ in range(t):
    n = int(input())
    nums = list(map(int, input().split()))
 
 
    def maxsum(arr):
        cur = 0
        ans = -float('inf')

        for num in arr:
            cur += num
            ans = max(ans, cur)

            if cur < 0:
                cur = 0
        
        return ans
    
    res1, res2 = maxsum(nums[1:]), maxsum(nums[:-1])
    if max(res1, res2) >= sum(nums):
        print("NO")
    else:
        print("YES")

    
        

C++ Code:

#include<bits/stdc++.h>
#define INF = INT_MAX >> 1
#define MOD 1000000007
#define int long long
using namespace std;

void solve(){
    int n;
    cin >> n;
    vector<int> input(n, 0);
    for(int i = 0; i < n;i++)
    cin >> input[i];
    int prefix = 0;
    for(int i = 0 ; i <n; i++){
        prefix += input[i];
        if(prefix <= 0){
            cout<<"NO"<<endl;
            return;
        }
    }   
    int suffix = 0;
    for(int i = n - 1; i>=0;i--){
        suffix += input[i];
        if(suffix <= 0){
            cout << "NO" << endl;
            return;
        }
    }
     cout << "YES" << endl;
     return; 
}

int32_t main(){
    ios::sync_with_stdio(0);
    cin.tie(0); 
    int t;
    cin >> t;
    while(t--)
      solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation