#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18+7;
inline void clear(vector<ll>&v)
{
v.clear();
v.shrink_to_fit();
return;
}
inline void merge(vector<ll>&rt,vector<ll>&l,vector<ll>&r)
{
rt.clear();
rt.reserve((int)l.size()+(int)r.size()-1);
rt.eb(l[0]+r[0]);
int li=1,ri=1;
while(li<(int)l.size()&&ri<(int)r.size())
{
if(l[li]-l[li-1]>r[ri]-r[ri-1])
rt.eb(rt.back()+l[li]-l[li-1]),li++;
else
rt.eb(rt.back()+r[ri]-r[ri-1]),ri++;
}
while(li<(int)l.size())
rt.eb(rt.back()+l[li]-l[li-1]),li++;
while(ri<(int)r.size())
rt.eb(rt.back()+r[ri]-r[ri-1]),ri++;
return;
}
inline void mx(vector<ll>&rt,vector<ll>&r)
{
if(rt.size()<r.size())
rt.resize(r.size(),-INF);
for(int i=0;i<(int)r.size();i++)
rt[i]=max(rt[i],r[i]);
return;
}
vector<ll>no[1200010],l[1200010],r[1200010],lr[1200010];
void dnc(int n,int s,int e,vector<ll>&v)
{
if(s==e)
{
no[n].eb(0);
no[n].eb(v[s]);
l[n].eb(v[s]);
r[n].eb(v[s]);
lr[n].eb(v[s]);
return;
}
int m=s+(e-s)/2;
dnc(n*2,s,m,v);
dnc(n*2+1,m+1,e,v);
vector<ll>v1,v2,v3,v4;
merge(no[n],no[n*2],no[n*2+1]);
merge(l[n],lr[n*2],l[n*2+1]);
merge(r[n],r[n*2],lr[n*2+1]);
merge(lr[n],lr[n*2],lr[n*2+1]);
merge(v1,r[n*2],l[n*2+1]);
merge(v2,l[n*2],no[n*2+1]);
merge(v3,no[n*2],r[n*2+1]);
merge(v4,l[n*2],r[n*2+1]);
v1.insert(v1.begin(),-INF);
v4.insert(v4.begin(),-INF);
mx(no[n],v1);
mx(l[n],v2);
mx(r[n],v3);
mx(lr[n],v4);
for(int i=n*2;i<n*2+2;i++)
clear(no[i]),clear(l[i]),clear(r[i]),clear(lr[i]);
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,k;
cin>>n>>k;
vector<ll>v(n);
for(ll&t:v)
cin>>t;
if(k==1)
{
ll ans=0;
ll ca=0;
for(ll&t:v)
ans=max(ans,ca=max(ca+t,0ll));
cout<<ans<<endl;
return 0;
}
bool allp=1;
for(ll&t:v)
if(t<0)
allp=0;
if(allp)
{
ll ans=0;
for(ll&t:v)
ans+=t;
cout<<ans<<endl;
return 0;
}
v.insert(v.begin(),0ll);
dnc(1,1,n,v);
ll ans=0;
for(int i=0;i<=k;i++)
ans=max(ans,no[1][i]);
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
115384 KB |
Output is correct |
2 |
Correct |
110 ms |
115448 KB |
Output is correct |
3 |
Correct |
111 ms |
115424 KB |
Output is correct |
4 |
Correct |
113 ms |
115448 KB |
Output is correct |
5 |
Correct |
111 ms |
115320 KB |
Output is correct |
6 |
Correct |
115 ms |
115392 KB |
Output is correct |
7 |
Correct |
107 ms |
115320 KB |
Output is correct |
8 |
Correct |
117 ms |
115424 KB |
Output is correct |
9 |
Correct |
129 ms |
115296 KB |
Output is correct |
10 |
Correct |
115 ms |
115448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
115320 KB |
Output is correct |
2 |
Correct |
98 ms |
115424 KB |
Output is correct |
3 |
Correct |
91 ms |
115296 KB |
Output is correct |
4 |
Correct |
767 ms |
158736 KB |
Output is correct |
5 |
Correct |
122 ms |
115424 KB |
Output is correct |
6 |
Correct |
101 ms |
115320 KB |
Output is correct |
7 |
Correct |
758 ms |
162996 KB |
Output is correct |
8 |
Correct |
112 ms |
115448 KB |
Output is correct |
9 |
Correct |
117 ms |
115448 KB |
Output is correct |
10 |
Correct |
737 ms |
159724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
115452 KB |
Output is correct |
2 |
Correct |
113 ms |
115320 KB |
Output is correct |
3 |
Correct |
121 ms |
115448 KB |
Output is correct |
4 |
Correct |
116 ms |
115428 KB |
Output is correct |
5 |
Correct |
119 ms |
115476 KB |
Output is correct |
6 |
Correct |
122 ms |
115488 KB |
Output is correct |
7 |
Correct |
118 ms |
115448 KB |
Output is correct |
8 |
Correct |
124 ms |
115468 KB |
Output is correct |
9 |
Correct |
115 ms |
115448 KB |
Output is correct |
10 |
Correct |
120 ms |
115448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
113016 KB |
Output is correct |
2 |
Correct |
85 ms |
112996 KB |
Output is correct |
3 |
Correct |
72 ms |
113120 KB |
Output is correct |
4 |
Correct |
76 ms |
113120 KB |
Output is correct |
5 |
Correct |
73 ms |
113144 KB |
Output is correct |
6 |
Correct |
80 ms |
113144 KB |
Output is correct |
7 |
Correct |
78 ms |
113016 KB |
Output is correct |
8 |
Correct |
90 ms |
113144 KB |
Output is correct |
9 |
Correct |
70 ms |
113144 KB |
Output is correct |
10 |
Correct |
77 ms |
113144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
113016 KB |
Output is correct |
2 |
Correct |
85 ms |
112996 KB |
Output is correct |
3 |
Correct |
72 ms |
113120 KB |
Output is correct |
4 |
Correct |
76 ms |
113120 KB |
Output is correct |
5 |
Correct |
73 ms |
113144 KB |
Output is correct |
6 |
Correct |
80 ms |
113144 KB |
Output is correct |
7 |
Correct |
78 ms |
113016 KB |
Output is correct |
8 |
Correct |
90 ms |
113144 KB |
Output is correct |
9 |
Correct |
70 ms |
113144 KB |
Output is correct |
10 |
Correct |
77 ms |
113144 KB |
Output is correct |
11 |
Correct |
77 ms |
113272 KB |
Output is correct |
12 |
Correct |
70 ms |
113144 KB |
Output is correct |
13 |
Correct |
71 ms |
113144 KB |
Output is correct |
14 |
Correct |
71 ms |
113144 KB |
Output is correct |
15 |
Correct |
76 ms |
113144 KB |
Output is correct |
16 |
Correct |
73 ms |
113144 KB |
Output is correct |
17 |
Correct |
82 ms |
113144 KB |
Output is correct |
18 |
Correct |
77 ms |
113144 KB |
Output is correct |
19 |
Correct |
75 ms |
113144 KB |
Output is correct |
20 |
Correct |
71 ms |
113164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
113016 KB |
Output is correct |
2 |
Correct |
85 ms |
112996 KB |
Output is correct |
3 |
Correct |
72 ms |
113120 KB |
Output is correct |
4 |
Correct |
76 ms |
113120 KB |
Output is correct |
5 |
Correct |
73 ms |
113144 KB |
Output is correct |
6 |
Correct |
80 ms |
113144 KB |
Output is correct |
7 |
Correct |
78 ms |
113016 KB |
Output is correct |
8 |
Correct |
90 ms |
113144 KB |
Output is correct |
9 |
Correct |
70 ms |
113144 KB |
Output is correct |
10 |
Correct |
77 ms |
113144 KB |
Output is correct |
11 |
Correct |
77 ms |
113272 KB |
Output is correct |
12 |
Correct |
70 ms |
113144 KB |
Output is correct |
13 |
Correct |
71 ms |
113144 KB |
Output is correct |
14 |
Correct |
71 ms |
113144 KB |
Output is correct |
15 |
Correct |
76 ms |
113144 KB |
Output is correct |
16 |
Correct |
73 ms |
113144 KB |
Output is correct |
17 |
Correct |
82 ms |
113144 KB |
Output is correct |
18 |
Correct |
77 ms |
113144 KB |
Output is correct |
19 |
Correct |
75 ms |
113144 KB |
Output is correct |
20 |
Correct |
71 ms |
113164 KB |
Output is correct |
21 |
Correct |
82 ms |
113376 KB |
Output is correct |
22 |
Correct |
83 ms |
113368 KB |
Output is correct |
23 |
Correct |
78 ms |
113472 KB |
Output is correct |
24 |
Correct |
84 ms |
113400 KB |
Output is correct |
25 |
Correct |
78 ms |
113380 KB |
Output is correct |
26 |
Correct |
76 ms |
113376 KB |
Output is correct |
27 |
Correct |
85 ms |
113364 KB |
Output is correct |
28 |
Correct |
75 ms |
113376 KB |
Output is correct |
29 |
Correct |
75 ms |
113400 KB |
Output is correct |
30 |
Correct |
79 ms |
113504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
115384 KB |
Output is correct |
2 |
Correct |
110 ms |
115448 KB |
Output is correct |
3 |
Correct |
111 ms |
115424 KB |
Output is correct |
4 |
Correct |
113 ms |
115448 KB |
Output is correct |
5 |
Correct |
111 ms |
115320 KB |
Output is correct |
6 |
Correct |
115 ms |
115392 KB |
Output is correct |
7 |
Correct |
107 ms |
115320 KB |
Output is correct |
8 |
Correct |
117 ms |
115424 KB |
Output is correct |
9 |
Correct |
129 ms |
115296 KB |
Output is correct |
10 |
Correct |
115 ms |
115448 KB |
Output is correct |
11 |
Correct |
97 ms |
115320 KB |
Output is correct |
12 |
Correct |
98 ms |
115424 KB |
Output is correct |
13 |
Correct |
91 ms |
115296 KB |
Output is correct |
14 |
Correct |
767 ms |
158736 KB |
Output is correct |
15 |
Correct |
122 ms |
115424 KB |
Output is correct |
16 |
Correct |
101 ms |
115320 KB |
Output is correct |
17 |
Correct |
758 ms |
162996 KB |
Output is correct |
18 |
Correct |
112 ms |
115448 KB |
Output is correct |
19 |
Correct |
117 ms |
115448 KB |
Output is correct |
20 |
Correct |
737 ms |
159724 KB |
Output is correct |
21 |
Correct |
116 ms |
115452 KB |
Output is correct |
22 |
Correct |
113 ms |
115320 KB |
Output is correct |
23 |
Correct |
121 ms |
115448 KB |
Output is correct |
24 |
Correct |
116 ms |
115428 KB |
Output is correct |
25 |
Correct |
119 ms |
115476 KB |
Output is correct |
26 |
Correct |
122 ms |
115488 KB |
Output is correct |
27 |
Correct |
118 ms |
115448 KB |
Output is correct |
28 |
Correct |
124 ms |
115468 KB |
Output is correct |
29 |
Correct |
115 ms |
115448 KB |
Output is correct |
30 |
Correct |
120 ms |
115448 KB |
Output is correct |
31 |
Correct |
77 ms |
113016 KB |
Output is correct |
32 |
Correct |
85 ms |
112996 KB |
Output is correct |
33 |
Correct |
72 ms |
113120 KB |
Output is correct |
34 |
Correct |
76 ms |
113120 KB |
Output is correct |
35 |
Correct |
73 ms |
113144 KB |
Output is correct |
36 |
Correct |
80 ms |
113144 KB |
Output is correct |
37 |
Correct |
78 ms |
113016 KB |
Output is correct |
38 |
Correct |
90 ms |
113144 KB |
Output is correct |
39 |
Correct |
70 ms |
113144 KB |
Output is correct |
40 |
Correct |
77 ms |
113144 KB |
Output is correct |
41 |
Correct |
77 ms |
113272 KB |
Output is correct |
42 |
Correct |
70 ms |
113144 KB |
Output is correct |
43 |
Correct |
71 ms |
113144 KB |
Output is correct |
44 |
Correct |
71 ms |
113144 KB |
Output is correct |
45 |
Correct |
76 ms |
113144 KB |
Output is correct |
46 |
Correct |
73 ms |
113144 KB |
Output is correct |
47 |
Correct |
82 ms |
113144 KB |
Output is correct |
48 |
Correct |
77 ms |
113144 KB |
Output is correct |
49 |
Correct |
75 ms |
113144 KB |
Output is correct |
50 |
Correct |
71 ms |
113164 KB |
Output is correct |
51 |
Correct |
82 ms |
113376 KB |
Output is correct |
52 |
Correct |
83 ms |
113368 KB |
Output is correct |
53 |
Correct |
78 ms |
113472 KB |
Output is correct |
54 |
Correct |
84 ms |
113400 KB |
Output is correct |
55 |
Correct |
78 ms |
113380 KB |
Output is correct |
56 |
Correct |
76 ms |
113376 KB |
Output is correct |
57 |
Correct |
85 ms |
113364 KB |
Output is correct |
58 |
Correct |
75 ms |
113376 KB |
Output is correct |
59 |
Correct |
75 ms |
113400 KB |
Output is correct |
60 |
Correct |
79 ms |
113504 KB |
Output is correct |
61 |
Correct |
993 ms |
158144 KB |
Output is correct |
62 |
Execution timed out |
1041 ms |
157484 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |