// NOI19 Task 3: Feast
// https://oj.uz/problem/view/NOI19_feast
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(ll i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18;
ll n,k,a[maxn],L,R,mid;
pair<ll,ll> dp[2][maxn];
pair<ll,ll> check(ll lambda){
// Penalty is lambda per subarray
dp[0][1] = {0,0};
dp[1][1] = {a[1] - lambda,1};
for(ll i=2;i<=n;i++){
dp[0][i] = max(dp[0][i-1], dp[1][i-1]);
dp[1][i] = max(make_pair(dp[1][i-1].first + a[i], dp[1][i-1].second),
make_pair(dp[0][i-1].first + a[i] - lambda, dp[0][i-1].second + 1));
}
return max(dp[0][n], dp[1][n]);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(__file_name ".inp", "r")){
freopen(__file_name ".inp","r",stdin);
freopen(__file_name ".out","w",stdout);
}
// code here
cin >> n >> k;
f1(i,n) cin >> a[i];
L = 0; R = 1e18;
while(L < R){
mid = (L+R+1)/2;
if(check(mid).second >= k) L = mid;
else R = mid-1;
}
// cout << L << ' ' << R << ' ' << check(L).second;el;
pair<ll,ll> res = check(L);
// cout << res.second;el;
cout << res.first + k * L;
return 0;
}
/*
Code by: Nguyen Viet Trung Nhan
Cau Giay Secondary School
*/
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen(__file_name ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen(__file_name ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
14672 KB |
Output is correct |
2 |
Correct |
51 ms |
14936 KB |
Output is correct |
3 |
Correct |
61 ms |
14936 KB |
Output is correct |
4 |
Correct |
52 ms |
14796 KB |
Output is correct |
5 |
Correct |
54 ms |
14948 KB |
Output is correct |
6 |
Correct |
51 ms |
14676 KB |
Output is correct |
7 |
Correct |
52 ms |
14680 KB |
Output is correct |
8 |
Correct |
55 ms |
14944 KB |
Output is correct |
9 |
Correct |
51 ms |
14680 KB |
Output is correct |
10 |
Correct |
71 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
14684 KB |
Output is correct |
2 |
Correct |
50 ms |
14936 KB |
Output is correct |
3 |
Correct |
50 ms |
14812 KB |
Output is correct |
4 |
Correct |
48 ms |
14892 KB |
Output is correct |
5 |
Correct |
62 ms |
14680 KB |
Output is correct |
6 |
Correct |
48 ms |
14696 KB |
Output is correct |
7 |
Correct |
47 ms |
14940 KB |
Output is correct |
8 |
Correct |
54 ms |
14940 KB |
Output is correct |
9 |
Correct |
53 ms |
14604 KB |
Output is correct |
10 |
Correct |
63 ms |
14980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
14804 KB |
Output is correct |
2 |
Correct |
74 ms |
14680 KB |
Output is correct |
3 |
Correct |
78 ms |
14940 KB |
Output is correct |
4 |
Correct |
87 ms |
14672 KB |
Output is correct |
5 |
Correct |
85 ms |
14932 KB |
Output is correct |
6 |
Correct |
79 ms |
14940 KB |
Output is correct |
7 |
Correct |
79 ms |
14940 KB |
Output is correct |
8 |
Correct |
77 ms |
14672 KB |
Output is correct |
9 |
Correct |
80 ms |
14940 KB |
Output is correct |
10 |
Correct |
85 ms |
14936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
14672 KB |
Output is correct |
2 |
Correct |
51 ms |
14936 KB |
Output is correct |
3 |
Correct |
61 ms |
14936 KB |
Output is correct |
4 |
Correct |
52 ms |
14796 KB |
Output is correct |
5 |
Correct |
54 ms |
14948 KB |
Output is correct |
6 |
Correct |
51 ms |
14676 KB |
Output is correct |
7 |
Correct |
52 ms |
14680 KB |
Output is correct |
8 |
Correct |
55 ms |
14944 KB |
Output is correct |
9 |
Correct |
51 ms |
14680 KB |
Output is correct |
10 |
Correct |
71 ms |
14684 KB |
Output is correct |
11 |
Correct |
49 ms |
14684 KB |
Output is correct |
12 |
Correct |
50 ms |
14936 KB |
Output is correct |
13 |
Correct |
50 ms |
14812 KB |
Output is correct |
14 |
Correct |
48 ms |
14892 KB |
Output is correct |
15 |
Correct |
62 ms |
14680 KB |
Output is correct |
16 |
Correct |
48 ms |
14696 KB |
Output is correct |
17 |
Correct |
47 ms |
14940 KB |
Output is correct |
18 |
Correct |
54 ms |
14940 KB |
Output is correct |
19 |
Correct |
53 ms |
14604 KB |
Output is correct |
20 |
Correct |
63 ms |
14980 KB |
Output is correct |
21 |
Correct |
80 ms |
14804 KB |
Output is correct |
22 |
Correct |
74 ms |
14680 KB |
Output is correct |
23 |
Correct |
78 ms |
14940 KB |
Output is correct |
24 |
Correct |
87 ms |
14672 KB |
Output is correct |
25 |
Correct |
85 ms |
14932 KB |
Output is correct |
26 |
Correct |
79 ms |
14940 KB |
Output is correct |
27 |
Correct |
79 ms |
14940 KB |
Output is correct |
28 |
Correct |
77 ms |
14672 KB |
Output is correct |
29 |
Correct |
80 ms |
14940 KB |
Output is correct |
30 |
Correct |
85 ms |
14936 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4696 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
4444 KB |
Output is correct |
51 |
Correct |
1 ms |
4444 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
4444 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2396 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
4444 KB |
Output is correct |
59 |
Correct |
1 ms |
4444 KB |
Output is correct |
60 |
Correct |
2 ms |
4444 KB |
Output is correct |
61 |
Correct |
113 ms |
14808 KB |
Output is correct |
62 |
Correct |
109 ms |
14936 KB |
Output is correct |
63 |
Correct |
91 ms |
14800 KB |
Output is correct |
64 |
Correct |
111 ms |
14932 KB |
Output is correct |
65 |
Correct |
114 ms |
14928 KB |
Output is correct |
66 |
Correct |
119 ms |
14940 KB |
Output is correct |
67 |
Correct |
116 ms |
14680 KB |
Output is correct |
68 |
Correct |
84 ms |
14792 KB |
Output is correct |
69 |
Correct |
79 ms |
14796 KB |
Output is correct |
70 |
Correct |
81 ms |
14672 KB |
Output is correct |