/** 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 = 1e5 + 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 = -1;
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, 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]);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Incorrect |
1 ms |
1100 KB |
Unexpected end of file - int32 expected |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
1 ms |
1228 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
2 ms |
1100 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
2 ms |
1100 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
2 ms |
1100 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
2 ms |
2124 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
2 ms |
1808 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Incorrect |
3 ms |
1996 KB |
Unexpected end of file - int32 expected |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1100 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
7 ms |
2764 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
1 ms |
1100 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
11 ms |
2764 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
9 ms |
2508 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
9 ms |
2764 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
7 ms |
2764 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
4 ms |
1356 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
5 ms |
1740 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1472 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
3 ms |
1484 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
62 ms |
10156 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
3 ms |
1484 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
74 ms |
6536 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
73 ms |
7412 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
68 ms |
7828 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
45 ms |
6724 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
65 ms |
7360 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
93 ms |
9252 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
4936 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
31 ms |
4912 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
636 ms |
82056 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
27 ms |
4940 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
1408 ms |
82044 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
843 ms |
58628 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
692 ms |
62464 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
491 ms |
51568 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
684 ms |
58516 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
862 ms |
74308 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |