#include <bits/stdc++.h>
#define PI acos(-1.0)
#define pll pair<ll, ll>
#define ll long long
#define ld long double
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define pll2 pair<pll,ll>
#define bit(i,k) ((k>>i)&1)
#define pii pair<int,int>
#define mp make_pair
using namespace std;
const long long mod = 1e9+7;
const ll inf = 1e18;
const ll maxN = 3e5+5;
ll n,k;
ll a[maxN];
pll go(ll lambda){
pll dp[n+5][2]={};//lay i hoac deo
dp[0][1].ff=-inf;
for(int i=1;i<=n;i++){
dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
pll p1={dp[i-1][0].ff+a[i]-lambda,dp[i-1][0].ss+1};
pll p2={dp[i-1][1].ff+a[i],dp[i-1][1].ss};
dp[i][1]=max(p1,p2);
}
return max(dp[n][1],dp[n][0]);
}
void solve() {
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
ll l=0,r=inf;
ll lmd;
ll ans=-inf;
while(l<r){
ll md=(l+r+1)/2;
pll p=go(md);
if(p.ss>=k){ans=p.ff;l=md;}
else{r=md-1;}
}
cout<<max(0ll,go(l).ff+k*l);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("rao.inp", "r", stdin);
// freopen("rao.out", "w", stdout);
ll nhim = 1;
// cin >> nhim;
// cout<<nhim<<'\n';
while (nhim--) {
solve();
}
}
Compilation message
feast.cpp: In function 'void solve()':
feast.cpp:42:6: warning: unused variable 'lmd' [-Wunused-variable]
42 | ll lmd;
| ^~~
feast.cpp:43:6: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
43 | ll ans=-inf;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
11804 KB |
Output is correct |
2 |
Correct |
67 ms |
14832 KB |
Output is correct |
3 |
Correct |
66 ms |
15032 KB |
Output is correct |
4 |
Correct |
68 ms |
14932 KB |
Output is correct |
5 |
Correct |
80 ms |
14860 KB |
Output is correct |
6 |
Correct |
69 ms |
14416 KB |
Output is correct |
7 |
Correct |
66 ms |
14424 KB |
Output is correct |
8 |
Correct |
69 ms |
14672 KB |
Output is correct |
9 |
Correct |
84 ms |
14416 KB |
Output is correct |
10 |
Correct |
68 ms |
14672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
11864 KB |
Output is correct |
2 |
Correct |
66 ms |
13284 KB |
Output is correct |
3 |
Correct |
61 ms |
12892 KB |
Output is correct |
4 |
Correct |
60 ms |
12888 KB |
Output is correct |
5 |
Correct |
67 ms |
14416 KB |
Output is correct |
6 |
Correct |
60 ms |
12764 KB |
Output is correct |
7 |
Correct |
62 ms |
13172 KB |
Output is correct |
8 |
Correct |
76 ms |
14964 KB |
Output is correct |
9 |
Correct |
68 ms |
14428 KB |
Output is correct |
10 |
Correct |
61 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
11856 KB |
Output is correct |
2 |
Correct |
95 ms |
14888 KB |
Output is correct |
3 |
Correct |
96 ms |
15192 KB |
Output is correct |
4 |
Correct |
95 ms |
14708 KB |
Output is correct |
5 |
Correct |
93 ms |
14936 KB |
Output is correct |
6 |
Correct |
97 ms |
14932 KB |
Output is correct |
7 |
Correct |
94 ms |
14936 KB |
Output is correct |
8 |
Correct |
94 ms |
14964 KB |
Output is correct |
9 |
Correct |
112 ms |
15456 KB |
Output is correct |
10 |
Correct |
96 ms |
14932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
464 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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
464 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 |
344 KB |
Output is correct |
9 |
Correct |
0 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 |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
464 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 |
344 KB |
Output is correct |
9 |
Correct |
0 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 |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
476 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
496 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
11804 KB |
Output is correct |
2 |
Correct |
67 ms |
14832 KB |
Output is correct |
3 |
Correct |
66 ms |
15032 KB |
Output is correct |
4 |
Correct |
68 ms |
14932 KB |
Output is correct |
5 |
Correct |
80 ms |
14860 KB |
Output is correct |
6 |
Correct |
69 ms |
14416 KB |
Output is correct |
7 |
Correct |
66 ms |
14424 KB |
Output is correct |
8 |
Correct |
69 ms |
14672 KB |
Output is correct |
9 |
Correct |
84 ms |
14416 KB |
Output is correct |
10 |
Correct |
68 ms |
14672 KB |
Output is correct |
11 |
Correct |
59 ms |
11864 KB |
Output is correct |
12 |
Correct |
66 ms |
13284 KB |
Output is correct |
13 |
Correct |
61 ms |
12892 KB |
Output is correct |
14 |
Correct |
60 ms |
12888 KB |
Output is correct |
15 |
Correct |
67 ms |
14416 KB |
Output is correct |
16 |
Correct |
60 ms |
12764 KB |
Output is correct |
17 |
Correct |
62 ms |
13172 KB |
Output is correct |
18 |
Correct |
76 ms |
14964 KB |
Output is correct |
19 |
Correct |
68 ms |
14428 KB |
Output is correct |
20 |
Correct |
61 ms |
13148 KB |
Output is correct |
21 |
Correct |
108 ms |
11856 KB |
Output is correct |
22 |
Correct |
95 ms |
14888 KB |
Output is correct |
23 |
Correct |
96 ms |
15192 KB |
Output is correct |
24 |
Correct |
95 ms |
14708 KB |
Output is correct |
25 |
Correct |
93 ms |
14936 KB |
Output is correct |
26 |
Correct |
97 ms |
14932 KB |
Output is correct |
27 |
Correct |
94 ms |
14936 KB |
Output is correct |
28 |
Correct |
94 ms |
14964 KB |
Output is correct |
29 |
Correct |
112 ms |
15456 KB |
Output is correct |
30 |
Correct |
96 ms |
14932 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
464 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 |
344 KB |
Output is correct |
39 |
Correct |
0 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 |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
432 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
492 KB |
Output is correct |
52 |
Correct |
1 ms |
344 KB |
Output is correct |
53 |
Correct |
1 ms |
476 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
2 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
344 KB |
Output is correct |
58 |
Correct |
1 ms |
344 KB |
Output is correct |
59 |
Correct |
1 ms |
496 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
131 ms |
14856 KB |
Output is correct |
62 |
Correct |
125 ms |
15220 KB |
Output is correct |
63 |
Correct |
121 ms |
14820 KB |
Output is correct |
64 |
Correct |
131 ms |
15116 KB |
Output is correct |
65 |
Correct |
140 ms |
15104 KB |
Output is correct |
66 |
Correct |
137 ms |
14968 KB |
Output is correct |
67 |
Correct |
135 ms |
14816 KB |
Output is correct |
68 |
Correct |
121 ms |
14956 KB |
Output is correct |
69 |
Correct |
96 ms |
14684 KB |
Output is correct |
70 |
Correct |
97 ms |
14684 KB |
Output is correct |