RUN

Competitive programming template in C++

Fast input and output with bits/stdc++.h, ready for contest problems. Press Run to try it, then change the code and run it again.

main.cpp

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

using ll = long long;

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n);
    for (auto &x : a) cin >> x;
    // Maximum subarray sum (Kadane's algorithm)
    ll best = a[0], cur = 0;
    for (ll x : a) {
        cur = max(x, cur + x);
        best = max(best, cur);
    }
    cout << best << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) solve();
}
Output

Press Run to see the output.

More C++ examples

Open the full C++ online compiler →