| Task: | Maximum sum |
| Sender: | aalto26cm_009 |
| Submission time: | 2026-09-14 16:26:51 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.00 s | details |
| #3 | ACCEPTED | 0.00 s | details |
| #4 | ACCEPTED | 0.00 s | details |
| #5 | ACCEPTED | 0.00 s | details |
| #6 | ACCEPTED | 0.02 s | details |
| #7 | ACCEPTED | 0.02 s | details |
| #8 | ACCEPTED | 0.03 s | details |
| #9 | ACCEPTED | 0.03 s | details |
| #10 | ACCEPTED | 0.03 s | details |
| #11 | ACCEPTED | 0.00 s | details |
| #12 | ACCEPTED | 0.00 s | details |
| #13 | ACCEPTED | 0.00 s | details |
| #14 | ACCEPTED | 0.00 s | details |
| #15 | ACCEPTED | 0.00 s | details |
Code
// #ifdef ONLINE_JUDGE
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #endif
#include "bits/stdc++.h"
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fill(arr,val) memset(arr,val,sizeof(arr))
#define FOR(i, a, b) for(__typeof(b) i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for(__typeof(a) i = a, _b = b; i >= _b; --i)
#define ALL(a) (a).begin(), (a).end()
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ii pair<int,int>
#define iii pair<int,pair<int,int>>
#define dq deque<int>
#define nend '\n'
using namespace std;
const ll inf = 1e18;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};
const int MOD = 1e9 + 7;
const ll hashi = 2e9 + 11;
inline ll add(ll a, ll b) { return (a + b) % MOD; }
inline ll sub(ll a, ll b) { return ((a - b) % MOD + MOD) % MOD; }
inline ll mul(ll a, ll b) { return (a * b) % MOD; }
inline ll binpow(ll a, ll b) {
ll res = 1;
a %= MOD;
while (b > 0) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
inline ll modInverse(ll n) {
return binpow(n, MOD - 2);
}
inline ll modDivide(ll a, ll b) {
return (a % MOD * modInverse(b)) % MOD;
}
ll extended_gcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1; y = 0;
return a;
}
ll x1, y1;
ll d = extended_gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
int n;
void init_code() {
fast;
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
}
const int N=2e5+3;
ll a[N];
ll minn=1e9;
void solve() {
// f[i]: số cách để tung được tổng là i
// thcs: f[1..6]=1
// f[i] += f[i-j]
// f[0]=1;
// f[1]=1;
// cin >> n;
// FOR(i,2,n) { /// can tim tong i
// FOR(j,1,6) { /// co so cach tung dc tong j
// if (i-j>=0)
// f[i]=(f[i]+f[i-j])%MOD;
// }
// }
// cout << f[n];
cin >> n;
FOR(i,1,n) {
cin >> a[i];
minn=min(minn,a[i]);
}
ll max_sum = minn, curr_sum =0;
FOR(i,1,n) {
curr_sum += a[i];
if(max_sum < curr_sum)
max_sum = curr_sum;
if(curr_sum < 0)
curr_sum = 0;
}
cout<<max_sum;
}
int main() {
init_code();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
// #ifndef ONLINE_JUDGE
// cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " seconds.\n";
// #endif
return 0;
}Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 10 1 1 1 1 1 1 1 1 1 1 |
| correct output |
|---|
| 10 |
| user output |
|---|
| 10 |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 10 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 |
| correct output |
|---|
| -1 |
| user output |
|---|
| -1 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 10 24 7 -27 17 -67 65 -23 58 85 -... |
| correct output |
|---|
| 185 |
| user output |
|---|
| 185 |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 10 99 -59 31 83 -79 64 -20 -87 40... |
| correct output |
|---|
| 154 |
| user output |
|---|
| 154 |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 10 -19 61 60 33 67 19 -8 92 59 -3... |
| correct output |
|---|
| 383 |
| user output |
|---|
| 383 |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 200000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| correct output |
|---|
| 200000 |
| user output |
|---|
| 200000 |
Test 7
Verdict: ACCEPTED
| input |
|---|
| 200000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... |
| correct output |
|---|
| -1 |
| user output |
|---|
| -1 |
Test 8
Verdict: ACCEPTED
| input |
|---|
| 200000 381082742 830199996 -85684827 ... |
| correct output |
|---|
| 231210956017 |
| user output |
|---|
| 231210956017 |
Test 9
Verdict: ACCEPTED
| input |
|---|
| 200000 -935928962 -795492223 75287481... |
| correct output |
|---|
| 184607318819 |
| user output |
|---|
| 184607318819 |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 200000 524408131 613017181 -62281009 ... |
| correct output |
|---|
| 360019999220 |
| user output |
|---|
| 360019999220 |
Test 11
Verdict: ACCEPTED
| input |
|---|
| 1 1 |
| correct output |
|---|
| 1 |
| user output |
|---|
| 1 |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 1 -2 |
| correct output |
|---|
| -2 |
| user output |
|---|
| -2 |
Test 13
Verdict: ACCEPTED
| input |
|---|
| 5 -1 -1 -1 -1 -2 |
| correct output |
|---|
| -1 |
| user output |
|---|
| -1 |
Test 14
Verdict: ACCEPTED
| input |
|---|
| 2 -3 -2 |
| correct output |
|---|
| -2 |
| user output |
|---|
| -2 |
Test 15
Verdict: ACCEPTED
| input |
|---|
| 1 -1000000000 |
| correct output |
|---|
| -1000000000 |
| user output |
|---|
| -1000000000 |
