#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 pair<ii,ll> iii;
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 p[MAXN];
ll dp[MAXN], cnt[MAXN];
iii pre[MAXN]; //best dp[j-1]+sum(j..i) for j<=i
ll sum(int l, int r){ return p[r]-(l?p[l-1]:0LL); }
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>K;
fore(i,1,n)
{
cin>>a[i];
p[i]=p[i-1]+a[i];
}
dp[0]=0;
cnt[0]=0;
pre[0]={{-INF,0},0};
ll ans=0;
for(ll L=0,R=1e16;L<=R;)
{
ll mid=(L+R)>>1;
fore(i,1,n)
{
iii tmp=pre[i-1];
tmp.F.F+=a[i];
pre[i]=max({{dp[i-1]+a[i],cnt[i-1]+1}, i}, tmp);
if(pre[i].F.F-mid>=dp[i-1])
{
dp[i]=pre[i].F.F-mid;
cnt[i]=pre[i].F.S;
}
else
{
dp[i]=dp[i-1];
cnt[i]=cnt[i-1];
}
}
if(cnt[n]>=K)
{
ans=dp[n]+min((ll)K,cnt[n])*mid;
L=mid+1;
}
else R=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.F<<","<<pre[i].F.S<<") "; cout<<'\n';
cout<<"ans: "<<dp[n]+cnt[n]*mid<<"\n\n";
}
}
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
16228 KB |
Output is correct |
2 |
Correct |
234 ms |
16456 KB |
Output is correct |
3 |
Correct |
261 ms |
16832 KB |
Output is correct |
4 |
Correct |
244 ms |
16616 KB |
Output is correct |
5 |
Correct |
235 ms |
16528 KB |
Output is correct |
6 |
Correct |
234 ms |
16324 KB |
Output is correct |
7 |
Correct |
232 ms |
16236 KB |
Output is correct |
8 |
Correct |
238 ms |
16472 KB |
Output is correct |
9 |
Correct |
230 ms |
16348 KB |
Output is correct |
10 |
Correct |
236 ms |
16380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
16324 KB |
Output is correct |
2 |
Correct |
228 ms |
16700 KB |
Output is correct |
3 |
Correct |
219 ms |
16312 KB |
Output is correct |
4 |
Correct |
222 ms |
16412 KB |
Output is correct |
5 |
Correct |
237 ms |
16196 KB |
Output is correct |
6 |
Correct |
219 ms |
16312 KB |
Output is correct |
7 |
Correct |
231 ms |
16708 KB |
Output is correct |
8 |
Correct |
238 ms |
16648 KB |
Output is correct |
9 |
Correct |
244 ms |
16144 KB |
Output is correct |
10 |
Correct |
223 ms |
16508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
16452 KB |
Output is correct |
2 |
Correct |
239 ms |
16228 KB |
Output is correct |
3 |
Correct |
235 ms |
16448 KB |
Output is correct |
4 |
Correct |
232 ms |
16328 KB |
Output is correct |
5 |
Correct |
242 ms |
16584 KB |
Output is correct |
6 |
Correct |
239 ms |
16576 KB |
Output is correct |
7 |
Correct |
239 ms |
16580 KB |
Output is correct |
8 |
Correct |
236 ms |
16512 KB |
Output is correct |
9 |
Correct |
246 ms |
16580 KB |
Output is correct |
10 |
Correct |
245 ms |
16664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
16228 KB |
Output is correct |
2 |
Correct |
234 ms |
16456 KB |
Output is correct |
3 |
Correct |
261 ms |
16832 KB |
Output is correct |
4 |
Correct |
244 ms |
16616 KB |
Output is correct |
5 |
Correct |
235 ms |
16528 KB |
Output is correct |
6 |
Correct |
234 ms |
16324 KB |
Output is correct |
7 |
Correct |
232 ms |
16236 KB |
Output is correct |
8 |
Correct |
238 ms |
16472 KB |
Output is correct |
9 |
Correct |
230 ms |
16348 KB |
Output is correct |
10 |
Correct |
236 ms |
16380 KB |
Output is correct |
11 |
Correct |
226 ms |
16324 KB |
Output is correct |
12 |
Correct |
228 ms |
16700 KB |
Output is correct |
13 |
Correct |
219 ms |
16312 KB |
Output is correct |
14 |
Correct |
222 ms |
16412 KB |
Output is correct |
15 |
Correct |
237 ms |
16196 KB |
Output is correct |
16 |
Correct |
219 ms |
16312 KB |
Output is correct |
17 |
Correct |
231 ms |
16708 KB |
Output is correct |
18 |
Correct |
238 ms |
16648 KB |
Output is correct |
19 |
Correct |
244 ms |
16144 KB |
Output is correct |
20 |
Correct |
223 ms |
16508 KB |
Output is correct |
21 |
Correct |
234 ms |
16452 KB |
Output is correct |
22 |
Correct |
239 ms |
16228 KB |
Output is correct |
23 |
Correct |
235 ms |
16448 KB |
Output is correct |
24 |
Correct |
232 ms |
16328 KB |
Output is correct |
25 |
Correct |
242 ms |
16584 KB |
Output is correct |
26 |
Correct |
239 ms |
16576 KB |
Output is correct |
27 |
Correct |
239 ms |
16580 KB |
Output is correct |
28 |
Correct |
236 ms |
16512 KB |
Output is correct |
29 |
Correct |
246 ms |
16580 KB |
Output is correct |
30 |
Correct |
245 ms |
16664 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |