#include <bits/stdc++.h>
using namespace std;
#define int long long
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define fori(x,a,b) for (int x=a;x<=b;x++)
#define ford(x,a,b) for (int x=a;x>=b;x--)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define getbit(x,i) ((x>>i)&1)
#define batbit(x,i) (x|(1ll<<i))
#define tatbit(x,i) (x&~(1<<i))
#define gg exit(0);
const int N = 3e5+10;
int n,a[N],k;
int f[N][2],cnt[N][2];
bool check(int val)
{
//0 : ko chon i
//1 : chon i
fori(i,1,n) fori(j,0,1) f[i][j] = 0;
f[1][0] = 0;
f[1][1] = a[1]-val;
cnt[1][0] = 0;
cnt[1][1] = 1;
fori(i,2,n)
{
f[i][0] = max(f[i-1][0],f[i-1][1]);
if(f[i-1][0] > f[i-1][1]) cnt[i][0] = cnt[i-1][0];
else cnt[i][0] = cnt[i-1][1];
f[i][1] = max(f[i-1][0]-val,f[i-1][1]) + a[i];
if(f[i-1][0]-val > f[i-1][1]) cnt[i][1] = cnt[i-1][0] + 1;
else cnt[i][1] = cnt[i-1][1];
}
if(f[n][0] > f[n][1]) return (cnt[n][0]>=k);
return (cnt[n][1]>=k);
}
main()
{
fasty;
//freopen("feast.inp","r",stdin);
//freopen("feast.out","w",stdout);
cin>>n>>k;
fori(i,1,n) cin>>a[i];
int l=0,r=1e15,res=0;
while(l<=r)
{
int mid = (l+r)/2;
if(check(mid)) l=mid+1,res=mid;
else r=mid-1;
}
fori(i,1,n) fori(j,0,1) f[i][j] = 0;
f[1][0] = 0;
f[1][1] = a[1]-res;
cnt[1][0] = 0;
cnt[1][1] = 1;
fori(i,2,n)
{
f[i][0] = max(f[i-1][0],f[i-1][1]);
if(f[i-1][0] > f[i-1][1]) cnt[i][0] = cnt[i-1][0];
else cnt[i][0] = cnt[i-1][1];
f[i][1] = max(f[i-1][0]-res,f[i-1][1]) + a[i];
if(f[i-1][0]-res > f[i-1][1]) cnt[i][1] = cnt[i-1][0] + 1;
else cnt[i][1] = cnt[i-1][1];
}
if(f[n][0] > f[n][1]) cout<<cnt[n][0]*res + f[n][0];
else cout<<cnt[n][1]*res+f[n][1];
}
Compilation message
feast.cpp:48:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
48 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
14432 KB |
Output is correct |
2 |
Correct |
144 ms |
14760 KB |
Output is correct |
3 |
Correct |
138 ms |
14844 KB |
Output is correct |
4 |
Correct |
143 ms |
14816 KB |
Output is correct |
5 |
Correct |
136 ms |
14680 KB |
Output is correct |
6 |
Correct |
134 ms |
14500 KB |
Output is correct |
7 |
Correct |
129 ms |
14492 KB |
Output is correct |
8 |
Correct |
152 ms |
14740 KB |
Output is correct |
9 |
Correct |
135 ms |
14492 KB |
Output is correct |
10 |
Correct |
140 ms |
14672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
12780 KB |
Output is correct |
2 |
Correct |
128 ms |
13160 KB |
Output is correct |
3 |
Correct |
138 ms |
12860 KB |
Output is correct |
4 |
Correct |
128 ms |
12948 KB |
Output is correct |
5 |
Correct |
133 ms |
14512 KB |
Output is correct |
6 |
Correct |
130 ms |
12740 KB |
Output is correct |
7 |
Correct |
124 ms |
13180 KB |
Output is correct |
8 |
Correct |
135 ms |
14848 KB |
Output is correct |
9 |
Correct |
136 ms |
14464 KB |
Output is correct |
10 |
Correct |
123 ms |
13024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
14940 KB |
Output is correct |
2 |
Correct |
165 ms |
14752 KB |
Output is correct |
3 |
Correct |
155 ms |
14900 KB |
Output is correct |
4 |
Correct |
152 ms |
14680 KB |
Output is correct |
5 |
Correct |
157 ms |
14964 KB |
Output is correct |
6 |
Correct |
157 ms |
14992 KB |
Output is correct |
7 |
Correct |
155 ms |
15040 KB |
Output is correct |
8 |
Correct |
155 ms |
14848 KB |
Output is correct |
9 |
Correct |
155 ms |
15080 KB |
Output is correct |
10 |
Correct |
149 ms |
14992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
424 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
352 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
14432 KB |
Output is correct |
2 |
Correct |
144 ms |
14760 KB |
Output is correct |
3 |
Correct |
138 ms |
14844 KB |
Output is correct |
4 |
Correct |
143 ms |
14816 KB |
Output is correct |
5 |
Correct |
136 ms |
14680 KB |
Output is correct |
6 |
Correct |
134 ms |
14500 KB |
Output is correct |
7 |
Correct |
129 ms |
14492 KB |
Output is correct |
8 |
Correct |
152 ms |
14740 KB |
Output is correct |
9 |
Correct |
135 ms |
14492 KB |
Output is correct |
10 |
Correct |
140 ms |
14672 KB |
Output is correct |
11 |
Correct |
128 ms |
12780 KB |
Output is correct |
12 |
Correct |
128 ms |
13160 KB |
Output is correct |
13 |
Correct |
138 ms |
12860 KB |
Output is correct |
14 |
Correct |
128 ms |
12948 KB |
Output is correct |
15 |
Correct |
133 ms |
14512 KB |
Output is correct |
16 |
Correct |
130 ms |
12740 KB |
Output is correct |
17 |
Correct |
124 ms |
13180 KB |
Output is correct |
18 |
Correct |
135 ms |
14848 KB |
Output is correct |
19 |
Correct |
136 ms |
14464 KB |
Output is correct |
20 |
Correct |
123 ms |
13024 KB |
Output is correct |
21 |
Correct |
152 ms |
14940 KB |
Output is correct |
22 |
Correct |
165 ms |
14752 KB |
Output is correct |
23 |
Correct |
155 ms |
14900 KB |
Output is correct |
24 |
Correct |
152 ms |
14680 KB |
Output is correct |
25 |
Correct |
157 ms |
14964 KB |
Output is correct |
26 |
Correct |
157 ms |
14992 KB |
Output is correct |
27 |
Correct |
155 ms |
15040 KB |
Output is correct |
28 |
Correct |
155 ms |
14848 KB |
Output is correct |
29 |
Correct |
155 ms |
15080 KB |
Output is correct |
30 |
Correct |
149 ms |
14992 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
324 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
320 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
2 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
2 ms |
424 KB |
Output is correct |
54 |
Correct |
2 ms |
332 KB |
Output is correct |
55 |
Correct |
2 ms |
352 KB |
Output is correct |
56 |
Correct |
2 ms |
332 KB |
Output is correct |
57 |
Correct |
2 ms |
332 KB |
Output is correct |
58 |
Correct |
2 ms |
332 KB |
Output is correct |
59 |
Correct |
2 ms |
332 KB |
Output is correct |
60 |
Correct |
2 ms |
332 KB |
Output is correct |
61 |
Correct |
211 ms |
14740 KB |
Output is correct |
62 |
Correct |
208 ms |
15100 KB |
Output is correct |
63 |
Correct |
174 ms |
14700 KB |
Output is correct |
64 |
Correct |
200 ms |
14996 KB |
Output is correct |
65 |
Correct |
190 ms |
14916 KB |
Output is correct |
66 |
Correct |
190 ms |
14880 KB |
Output is correct |
67 |
Correct |
193 ms |
14712 KB |
Output is correct |
68 |
Correct |
163 ms |
14916 KB |
Output is correct |
69 |
Correct |
150 ms |
14716 KB |
Output is correct |
70 |
Correct |
152 ms |
14636 KB |
Output is correct |