1903C - Theofanis' Nightmare - CodeForces Solution


constructive algorithms dp greedy

Please click on ads to support us..

C++ Code:

/*
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠛⠉⠙⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⠿⠋     ⢦⣀⡈⢻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣇        ⠈⣷⡄⠙⠛⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣷⣦⣄  ⢀⣤⣤⣾⣿⠧  ⠈⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠃ ⠉⠉    ⢀⣀⣠⣾⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⠁       ⢀⣾⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿        ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⠇        ⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⠟⠋         ⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⠇ ⡀         ⠹⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⠋⢀⣾⣿⠇        ⢈⠻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⡇ ⠸⣿⣿         ⠈⢦⠈⢿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣷⣾⣿⠃    ⣸⡆     ⢣⡀⠻⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡟    ⢀⣿⣿⡀    ⢀⣵⡀⠙⣻⣿⣿
⣿⣿⣿⣿⣿⣿⠁    ⢸⣿⣿⣧    ⠈⢻⣿⣾⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣤  ⢀⣀⣼⣿⣿⣿⡄   ⢴⣾⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡏  ⣿⣿⣿⣿⣿⣿⣿⣿⣇ ⠈⢿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⠇ ⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄ ⠘⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿  ⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄ ⢻⣿⣿⣿⣿
⣿⣿⣿⣿⣿⡏ ⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⠈⣿⣿⣿⣿
⣿⣿⣿⣿⡿⠁⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⠘⣿⣿⣿
⣿⡿⠛⠉  ⠈⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇ ⠈⠻⣿
⣿⣷⣶⣶⣶⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣤⣤⣤⣼
*/
#include<bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define dl double
#define OstorYaRab  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define sorts sort(s.begin(),s.end());
#define sorta sort(arr,arr+n);
#define sortv sort(v.begin(),v.end());
#define all(v) v.begin(),v.end();
#define all(s) s.begin(),s.end();
#define loop0 for(int i = 0 ; i < n ; i++)
#define loop1 for(int i = 1 ; i<= n ; i++)
#define endl "\n"
const dl pi = 3.141592653589793;
const ll INF = (int)1e9;
const ll mod = 1000000000 + 7;
using namespace std;
bool prime(ll n)
{
    if (n == 1)
        return 0;
    for (ll i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0)
            return 0;
    }
    return 1;
}
void printBin(int n)
{
    if (n <= 1)
    {
        cout << n;
        return;
    }
    printBin(n >> 1);
    cout << (n & 1);
}
ll CountBits(ll n)
{
    ll cnt = 0;
    while (n)
    {
        n = n >> 1;
        cnt++;
    }
    return cnt;
}
int CountBits1(int n)
{
    int cnt = 0;
    while (n)
    {
        cnt++;
        n &= (n - 1);
    }
    return cnt;
}
ll getBit(ll n, ll idx) { return (n >> idx) & 1; }
int SetLastZero1(int n) { return n | (n + 1); }
int SetLastOne0(int n) { return n & (n - 1); }
int SetFirstConsecutiveZeros1(int n) { return n | (n - 1); }
int SetFirstConsecutiveOnes0(int n) { return n & (n + 1); }
ll setBit1(ll n, ll idx) { return n | (1ll << idx); }
ll setBit0(ll n, ll idx) { return n & ~(1ll << idx); }
int flipBit(int n, int idx) { return n ^ (1 << idx); }
int lastBitValue1(int n) { return n & ~(n - 1); }
int lastBitValue(int n) { return n - (n & (n - 1)); }



int main()
{
    OstorYaRab;
    //freopen("equal.in", "r", stdin); 
    //("output.txt", "w", stdout);
    ll t = 1;
    cin >> t;
    while (t--) {
        ll n; cin >> n;
        vector<ll>arr(n);
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        ll ans = 0, sum = 0;
        for (int i = n - 1; i >= 0; i--) {
            sum += arr[i];
            if (sum > 0)ans += sum;
            // 2 11
            //13
        }
        if (sum < 0)ans += sum;
        cout << ans << endl;
    }
}


Comments

Submit
0 Comments
More Questions

746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent
805A - Fake NP
1163A - Eating Soup
787A - The Monster
807A - Is it rated
1096A - Find Divisible
1430C - Numbers on Whiteboard