#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
template<typename T>
using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void amin(ll &a, ll b){ a=min(a,b); }
void amax(ll &a, ll b){ a=max(a,b); }
void YES(){cout<<"YES\n";} void NO(){cout<<"NO\n";}
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 998244353;
const bool DEBUG = 0;
const int MAXN = 300005;
int n,K;
ll a[MAXN];
ll dp[MAXN], cnt[MAXN];
ii pre[MAXN]; //best dp[j-1]+sum(j..i) for j<=i
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>K;
int pos=0;
fore(i,1,n)
{
cin>>a[i];
if(a[i]>=0) pos++;
}
K=min(K, pos);
dp[0]=0;
cnt[0]=0;
pre[0]={-INF,0};
ll ans=0;
for(ll L=0,R=1e16;L<=R;)
{
ll mid=(L+R)>>1;
fore(i,1,n)
{
ii tmp=pre[i-1];
tmp.F+=a[i];
pre[i]=max(tmp, {dp[i-1]+a[i]-mid, -(cnt[i-1]+1)});
if(pre[i].F>dp[i-1])
{
dp[i]=pre[i].F;
cnt[i]=-pre[i].S;
}
else
{
dp[i]=dp[i-1];
cnt[i]=cnt[i-1];
}
}
if(cnt[n]<=K)
{
ans=dp[n]+K*mid;
R=mid-1;
}
else L=mid+1;
if(0)
{
cout<<"mid="<<mid<<'\n';
cout<<"a: "; fore(i,1,n) cout<<a[i]<<" "; cout<<'\n';
cout<<"dp: "; fore(i,1,n) cout<<dp[i]<<" "; cout<<'\n';
cout<<"cnt: "; fore(i,1,n) cout<<cnt[i]<<" "; cout<<'\n';
cout<<"pre: "; fore(i,1,n) cout<<"("<<pre[i].F<<","<<pre[i].S<<") "; cout<<'\n';
cout<<"ans: "<<dp[n]+cnt[n]*mid<<"\n\n";
}
}
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
11676 KB |
Output is correct |
2 |
Correct |
106 ms |
11888 KB |
Output is correct |
3 |
Correct |
107 ms |
12020 KB |
Output is correct |
4 |
Correct |
105 ms |
11952 KB |
Output is correct |
5 |
Correct |
104 ms |
11828 KB |
Output is correct |
6 |
Correct |
97 ms |
11580 KB |
Output is correct |
7 |
Correct |
99 ms |
11640 KB |
Output is correct |
8 |
Correct |
104 ms |
11856 KB |
Output is correct |
9 |
Correct |
110 ms |
11628 KB |
Output is correct |
10 |
Correct |
101 ms |
11832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
11756 KB |
Output is correct |
2 |
Correct |
93 ms |
11980 KB |
Output is correct |
3 |
Correct |
89 ms |
11716 KB |
Output is correct |
4 |
Correct |
89 ms |
11788 KB |
Output is correct |
5 |
Correct |
100 ms |
11588 KB |
Output is correct |
6 |
Correct |
91 ms |
11840 KB |
Output is correct |
7 |
Correct |
91 ms |
12036 KB |
Output is correct |
8 |
Correct |
107 ms |
11896 KB |
Output is correct |
9 |
Correct |
105 ms |
11564 KB |
Output is correct |
10 |
Correct |
91 ms |
11952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
11916 KB |
Output is correct |
2 |
Correct |
126 ms |
11716 KB |
Output is correct |
3 |
Correct |
122 ms |
11780 KB |
Output is correct |
4 |
Correct |
119 ms |
11764 KB |
Output is correct |
5 |
Correct |
121 ms |
11844 KB |
Output is correct |
6 |
Correct |
122 ms |
11984 KB |
Output is correct |
7 |
Correct |
121 ms |
12064 KB |
Output is correct |
8 |
Correct |
121 ms |
11876 KB |
Output is correct |
9 |
Correct |
121 ms |
12016 KB |
Output is correct |
10 |
Correct |
124 ms |
12004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 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 |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
11676 KB |
Output is correct |
2 |
Correct |
106 ms |
11888 KB |
Output is correct |
3 |
Correct |
107 ms |
12020 KB |
Output is correct |
4 |
Correct |
105 ms |
11952 KB |
Output is correct |
5 |
Correct |
104 ms |
11828 KB |
Output is correct |
6 |
Correct |
97 ms |
11580 KB |
Output is correct |
7 |
Correct |
99 ms |
11640 KB |
Output is correct |
8 |
Correct |
104 ms |
11856 KB |
Output is correct |
9 |
Correct |
110 ms |
11628 KB |
Output is correct |
10 |
Correct |
101 ms |
11832 KB |
Output is correct |
11 |
Correct |
92 ms |
11756 KB |
Output is correct |
12 |
Correct |
93 ms |
11980 KB |
Output is correct |
13 |
Correct |
89 ms |
11716 KB |
Output is correct |
14 |
Correct |
89 ms |
11788 KB |
Output is correct |
15 |
Correct |
100 ms |
11588 KB |
Output is correct |
16 |
Correct |
91 ms |
11840 KB |
Output is correct |
17 |
Correct |
91 ms |
12036 KB |
Output is correct |
18 |
Correct |
107 ms |
11896 KB |
Output is correct |
19 |
Correct |
105 ms |
11564 KB |
Output is correct |
20 |
Correct |
91 ms |
11952 KB |
Output is correct |
21 |
Correct |
127 ms |
11916 KB |
Output is correct |
22 |
Correct |
126 ms |
11716 KB |
Output is correct |
23 |
Correct |
122 ms |
11780 KB |
Output is correct |
24 |
Correct |
119 ms |
11764 KB |
Output is correct |
25 |
Correct |
121 ms |
11844 KB |
Output is correct |
26 |
Correct |
122 ms |
11984 KB |
Output is correct |
27 |
Correct |
121 ms |
12064 KB |
Output is correct |
28 |
Correct |
121 ms |
11876 KB |
Output is correct |
29 |
Correct |
121 ms |
12016 KB |
Output is correct |
30 |
Correct |
124 ms |
12004 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
332 KB |
Output is correct |
38 |
Correct |
0 ms |
332 KB |
Output is correct |
39 |
Correct |
0 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 |
332 KB |
Output is correct |
54 |
Correct |
1 ms |
332 KB |
Output is correct |
55 |
Correct |
2 ms |
332 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
158 ms |
11748 KB |
Output is correct |
62 |
Correct |
168 ms |
12032 KB |
Output is correct |
63 |
Correct |
137 ms |
11604 KB |
Output is correct |
64 |
Correct |
159 ms |
11852 KB |
Output is correct |
65 |
Correct |
179 ms |
12016 KB |
Output is correct |
66 |
Correct |
162 ms |
11912 KB |
Output is correct |
67 |
Correct |
159 ms |
11728 KB |
Output is correct |
68 |
Correct |
135 ms |
11952 KB |
Output is correct |
69 |
Correct |
120 ms |
11744 KB |
Output is correct |
70 |
Correct |
118 ms |
11648 KB |
Output is correct |