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.
#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
- Hello, World · The classic first C++ program.
- Vectors and sort · Read numbers into a vector, sort them and find the largest.
- Classes and objects · A class with a constructor, private data and member functions.
- Count words with a map · Use std::map to count how often each word appears.
- Binary search · Find a value in a sorted array in O(log n) steps.