1373F - Network Coverage - CodeForces Solution


binary search constructive algorithms data structures greedy *2400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

const int N = 1000100, mod = 998244353;

void solve(){
    int n;
    cin >> n;
    vector <int> a(n), b(n);
    for(int i = 0; i < n; ++i)
        cin >> a[i];
    for(int i = 0; i < n; ++i)
        cin >> b[i];
    long long mn = 0;
    long long cur = 0;
    for(int i = 0; i < n + n; ++i){
        cur += a[i % n];
        cur -= b[(i - 1 + n) % n];
        if(cur - mn - b[i % n] > 0){
            //cout << mn << " " << cur << " " << i << endl;
            cout << "NO\n";
            return;
        }
        mn = min(mn, cur);
    }
    if(cur > 0)
        cout << "NO\n";
    else cout << "YES\n";
}

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	int T;
	cin >> T;
	//T = 1;
	while(T--)
		solve();

	return 0;
}


Comments

Submit
0 Comments
More Questions

1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom