#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
const ll INF = 1e18;
void solve(){
int n, k; cin>>n>>k;
vi a(n+1); FOR(i,1,n+1) cin>>a[i];
vector<ll> s(n+1); FOR(i,1,n+1) s[i]=s[i-1]+a[i];
vector<ll> dp(n+1);
ll l=0,r=1e15;
while (r-l>1){
ll m=(l+r)/2;
vi seg(n+1); FOR(i,1,n+1) dp[i]=INF;
ll mx=0;
int cur=0;
FOR(i,1,n+1){
dp[i]=s[i]+mx-m;
seg[i]=cur+1;
if (dp[i-1]>dp[i]){
dp[i]=dp[i-1];
seg[i]=seg[i-1];
}
if (dp[i]-s[i]==mx) cur=max(cur,seg[i]);
if (dp[i]-s[i]>mx){
mx=dp[i]-s[i]; cur=seg[i];
}
}
if (seg[n]<k) r=m;
else l=m;
}
vi seg(n+1); FOR(i,1,n+1) dp[i]=INF;
ll mx=0;
int cur=0;
FOR(i,1,n+1){
dp[i]=s[i]+mx-l;
seg[i]=cur+1;
if (dp[i-1]>dp[i]){
dp[i]=dp[i-1];
seg[i]=seg[i-1];
}
if (dp[i]-s[i]==mx) cur=max(cur,seg[i]);
if (dp[i]-s[i]>mx){
mx=dp[i]-s[i]; cur=seg[i];
}
}
cout<<dp[n]+(ll)k*(ll)l;
}
int main(){
//MOD=MOD1;
ios::sync_with_stdio(false);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:66:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:67:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
9932 KB |
Output is correct |
2 |
Correct |
53 ms |
10052 KB |
Output is correct |
3 |
Correct |
54 ms |
10184 KB |
Output is correct |
4 |
Correct |
53 ms |
10112 KB |
Output is correct |
5 |
Correct |
53 ms |
10068 KB |
Output is correct |
6 |
Correct |
67 ms |
9920 KB |
Output is correct |
7 |
Correct |
53 ms |
9956 KB |
Output is correct |
8 |
Correct |
53 ms |
10036 KB |
Output is correct |
9 |
Correct |
52 ms |
9912 KB |
Output is correct |
10 |
Correct |
57 ms |
10044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
8308 KB |
Output is correct |
2 |
Correct |
60 ms |
8492 KB |
Output is correct |
3 |
Correct |
57 ms |
8276 KB |
Output is correct |
4 |
Correct |
54 ms |
8372 KB |
Output is correct |
5 |
Correct |
66 ms |
9916 KB |
Output is correct |
6 |
Correct |
57 ms |
8284 KB |
Output is correct |
7 |
Correct |
51 ms |
8500 KB |
Output is correct |
8 |
Correct |
66 ms |
10176 KB |
Output is correct |
9 |
Correct |
65 ms |
9928 KB |
Output is correct |
10 |
Correct |
51 ms |
8444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
10316 KB |
Output is correct |
2 |
Correct |
88 ms |
10096 KB |
Output is correct |
3 |
Correct |
86 ms |
10172 KB |
Output is correct |
4 |
Correct |
86 ms |
10172 KB |
Output is correct |
5 |
Correct |
87 ms |
10372 KB |
Output is correct |
6 |
Correct |
87 ms |
10340 KB |
Output is correct |
7 |
Correct |
87 ms |
10312 KB |
Output is correct |
8 |
Correct |
84 ms |
10280 KB |
Output is correct |
9 |
Correct |
87 ms |
10312 KB |
Output is correct |
10 |
Correct |
88 ms |
10312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
312 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
312 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
284 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
9932 KB |
Output is correct |
2 |
Correct |
53 ms |
10052 KB |
Output is correct |
3 |
Correct |
54 ms |
10184 KB |
Output is correct |
4 |
Correct |
53 ms |
10112 KB |
Output is correct |
5 |
Correct |
53 ms |
10068 KB |
Output is correct |
6 |
Correct |
67 ms |
9920 KB |
Output is correct |
7 |
Correct |
53 ms |
9956 KB |
Output is correct |
8 |
Correct |
53 ms |
10036 KB |
Output is correct |
9 |
Correct |
52 ms |
9912 KB |
Output is correct |
10 |
Correct |
57 ms |
10044 KB |
Output is correct |
11 |
Correct |
58 ms |
8308 KB |
Output is correct |
12 |
Correct |
60 ms |
8492 KB |
Output is correct |
13 |
Correct |
57 ms |
8276 KB |
Output is correct |
14 |
Correct |
54 ms |
8372 KB |
Output is correct |
15 |
Correct |
66 ms |
9916 KB |
Output is correct |
16 |
Correct |
57 ms |
8284 KB |
Output is correct |
17 |
Correct |
51 ms |
8500 KB |
Output is correct |
18 |
Correct |
66 ms |
10176 KB |
Output is correct |
19 |
Correct |
65 ms |
9928 KB |
Output is correct |
20 |
Correct |
51 ms |
8444 KB |
Output is correct |
21 |
Correct |
86 ms |
10316 KB |
Output is correct |
22 |
Correct |
88 ms |
10096 KB |
Output is correct |
23 |
Correct |
86 ms |
10172 KB |
Output is correct |
24 |
Correct |
86 ms |
10172 KB |
Output is correct |
25 |
Correct |
87 ms |
10372 KB |
Output is correct |
26 |
Correct |
87 ms |
10340 KB |
Output is correct |
27 |
Correct |
87 ms |
10312 KB |
Output is correct |
28 |
Correct |
84 ms |
10280 KB |
Output is correct |
29 |
Correct |
87 ms |
10312 KB |
Output is correct |
30 |
Correct |
88 ms |
10312 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
316 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
320 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
312 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
352 KB |
Output is correct |
52 |
Correct |
1 ms |
352 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
284 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
125 ms |
10172 KB |
Output is correct |
62 |
Correct |
116 ms |
10428 KB |
Output is correct |
63 |
Correct |
98 ms |
10152 KB |
Output is correct |
64 |
Correct |
121 ms |
10352 KB |
Output is correct |
65 |
Correct |
122 ms |
10316 KB |
Output is correct |
66 |
Correct |
123 ms |
10324 KB |
Output is correct |
67 |
Correct |
120 ms |
10160 KB |
Output is correct |
68 |
Correct |
98 ms |
10564 KB |
Output is correct |
69 |
Correct |
85 ms |
10036 KB |
Output is correct |
70 |
Correct |
84 ms |
10108 KB |
Output is correct |