/** Im the best because i work as hard as i possibly can **/
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Mp make_pair
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl "\n"
const int N = 15e4 + 10;
const ll inf = 1e18;
int n, k, par[210][N];
ll dp[2][N], ps[N], A[N];
inline ll calc(int l, int m)
{
return (ps[n] - ps[m]) * (ps[m] - (l <= 0? 0 : ps[l - 1]));
}
void solve(int j, int l, int r, int le, int ri)
{
int j2 = j & 1;
if(l > r || le > ri) return;
int mid = (l + r) >> 1, opt = le;
dp[j2][mid] = -inf;
ll best = -inf;
for(int i = max(1, le); i <= min(mid, ri); i ++)
{
ll cu = dp[1 ^ j2][i - 1] + calc(i, mid);
if(cu > best)
{
best = cu;
opt = i;
}
}
dp[j2][mid] = best;
par[j][mid] = opt - 1;
solve(j, l, mid - 1, le, opt);
solve(j, mid + 1, r, max(le, opt), ri);
}
int main()
{
scanf("%d%d", &n, &k);
k ++;
for(int i = 1; i <= n; i ++)
{
scanf("%lld", &A[i]);
ps[i] = ps[i - 1] + A[i];
}
for(int i = 1; i < N; i ++) dp[0][i] = -inf;
for(int j = 1; j <= k; j ++)
{
solve(j, 1, n, 1, n);
}
printf("%lld\n", dp[k & 1][n]);
vector < int > ans;
int i = n, j = k;
while(j > 0 && i > 0 && par[j][i] > 0)
{
ans.push_back(par[j][i]);
i = par[j][i];
j --;
}
reverse(all(ans));
for(auto x : ans) printf("%d ", x);
return 0;
}
Compilation message
sequence.cpp: In function 'int main()':
sequence.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
55 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
59 | scanf("%lld", &A[i]);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
1484 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Incorrect |
2 ms |
1452 KB |
Unexpected end of file - int32 expected |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1484 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
1 ms |
1484 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
1 ms |
1740 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
1484 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
1 ms |
1484 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
1484 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
2 ms |
1612 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
1484 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
2 ms |
1484 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
2 ms |
1484 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1484 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
1484 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
4 ms |
2636 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
2 ms |
1484 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
3 ms |
2252 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Incorrect |
3 ms |
2508 KB |
Unexpected end of file - int32 expected |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1484 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
2 ms |
1484 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
8 ms |
3252 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
2 ms |
1464 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
11 ms |
3248 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
9 ms |
3020 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
9 ms |
3276 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
7 ms |
3248 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
3 ms |
1868 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
5 ms |
2208 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1868 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
4 ms |
1868 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
66 ms |
10564 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
4 ms |
1868 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
76 ms |
6980 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
79 ms |
7992 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
69 ms |
8320 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
47 ms |
7172 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
66 ms |
7932 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
85 ms |
9684 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
5324 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
32 ms |
5568 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
697 ms |
83496 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
28 ms |
5580 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
1381 ms |
83640 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
859 ms |
59784 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
723 ms |
63812 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
515 ms |
52736 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
688 ms |
59844 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
902 ms |
75636 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |