#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.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 |
106 ms |
115320 KB |
Output is correct |
2 |
Correct |
106 ms |
115436 KB |
Output is correct |
3 |
Correct |
118 ms |
118392 KB |
Output is correct |
4 |
Correct |
108 ms |
118264 KB |
Output is correct |
5 |
Correct |
112 ms |
118264 KB |
Output is correct |
6 |
Correct |
110 ms |
118136 KB |
Output is correct |
7 |
Correct |
116 ms |
118136 KB |
Output is correct |
8 |
Correct |
103 ms |
118320 KB |
Output is correct |
9 |
Correct |
103 ms |
118136 KB |
Output is correct |
10 |
Correct |
105 ms |
118264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
115448 KB |
Output is correct |
2 |
Correct |
84 ms |
115452 KB |
Output is correct |
3 |
Correct |
85 ms |
115320 KB |
Output is correct |
4 |
Correct |
983 ms |
157512 KB |
Output is correct |
5 |
Correct |
114 ms |
115320 KB |
Output is correct |
6 |
Correct |
94 ms |
115320 KB |
Output is correct |
7 |
Correct |
994 ms |
157608 KB |
Output is correct |
8 |
Correct |
103 ms |
115452 KB |
Output is correct |
9 |
Correct |
102 ms |
115320 KB |
Output is correct |
10 |
Correct |
982 ms |
157328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
115320 KB |
Output is correct |
2 |
Correct |
111 ms |
118392 KB |
Output is correct |
3 |
Correct |
113 ms |
118392 KB |
Output is correct |
4 |
Correct |
120 ms |
118392 KB |
Output is correct |
5 |
Correct |
109 ms |
118368 KB |
Output is correct |
6 |
Correct |
108 ms |
118392 KB |
Output is correct |
7 |
Correct |
109 ms |
118520 KB |
Output is correct |
8 |
Correct |
120 ms |
118440 KB |
Output is correct |
9 |
Correct |
110 ms |
118520 KB |
Output is correct |
10 |
Correct |
113 ms |
118424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
113020 KB |
Output is correct |
2 |
Correct |
65 ms |
113016 KB |
Output is correct |
3 |
Correct |
67 ms |
113020 KB |
Output is correct |
4 |
Correct |
71 ms |
113016 KB |
Output is correct |
5 |
Correct |
64 ms |
113144 KB |
Output is correct |
6 |
Correct |
64 ms |
113000 KB |
Output is correct |
7 |
Correct |
65 ms |
113016 KB |
Output is correct |
8 |
Correct |
65 ms |
113016 KB |
Output is correct |
9 |
Correct |
65 ms |
113016 KB |
Output is correct |
10 |
Correct |
69 ms |
113016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
113020 KB |
Output is correct |
2 |
Correct |
65 ms |
113016 KB |
Output is correct |
3 |
Correct |
67 ms |
113020 KB |
Output is correct |
4 |
Correct |
71 ms |
113016 KB |
Output is correct |
5 |
Correct |
64 ms |
113144 KB |
Output is correct |
6 |
Correct |
64 ms |
113000 KB |
Output is correct |
7 |
Correct |
65 ms |
113016 KB |
Output is correct |
8 |
Correct |
65 ms |
113016 KB |
Output is correct |
9 |
Correct |
65 ms |
113016 KB |
Output is correct |
10 |
Correct |
69 ms |
113016 KB |
Output is correct |
11 |
Correct |
72 ms |
113144 KB |
Output is correct |
12 |
Correct |
66 ms |
113016 KB |
Output is correct |
13 |
Correct |
67 ms |
113144 KB |
Output is correct |
14 |
Correct |
67 ms |
113144 KB |
Output is correct |
15 |
Correct |
69 ms |
113144 KB |
Output is correct |
16 |
Correct |
73 ms |
113152 KB |
Output is correct |
17 |
Correct |
70 ms |
113168 KB |
Output is correct |
18 |
Correct |
71 ms |
113144 KB |
Output is correct |
19 |
Correct |
72 ms |
113144 KB |
Output is correct |
20 |
Correct |
80 ms |
113196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
113020 KB |
Output is correct |
2 |
Correct |
65 ms |
113016 KB |
Output is correct |
3 |
Correct |
67 ms |
113020 KB |
Output is correct |
4 |
Correct |
71 ms |
113016 KB |
Output is correct |
5 |
Correct |
64 ms |
113144 KB |
Output is correct |
6 |
Correct |
64 ms |
113000 KB |
Output is correct |
7 |
Correct |
65 ms |
113016 KB |
Output is correct |
8 |
Correct |
65 ms |
113016 KB |
Output is correct |
9 |
Correct |
65 ms |
113016 KB |
Output is correct |
10 |
Correct |
69 ms |
113016 KB |
Output is correct |
11 |
Correct |
72 ms |
113144 KB |
Output is correct |
12 |
Correct |
66 ms |
113016 KB |
Output is correct |
13 |
Correct |
67 ms |
113144 KB |
Output is correct |
14 |
Correct |
67 ms |
113144 KB |
Output is correct |
15 |
Correct |
69 ms |
113144 KB |
Output is correct |
16 |
Correct |
73 ms |
113152 KB |
Output is correct |
17 |
Correct |
70 ms |
113168 KB |
Output is correct |
18 |
Correct |
71 ms |
113144 KB |
Output is correct |
19 |
Correct |
72 ms |
113144 KB |
Output is correct |
20 |
Correct |
80 ms |
113196 KB |
Output is correct |
21 |
Correct |
84 ms |
113272 KB |
Output is correct |
22 |
Correct |
75 ms |
113272 KB |
Output is correct |
23 |
Correct |
83 ms |
113272 KB |
Output is correct |
24 |
Correct |
82 ms |
113360 KB |
Output is correct |
25 |
Correct |
74 ms |
113332 KB |
Output is correct |
26 |
Correct |
88 ms |
113272 KB |
Output is correct |
27 |
Correct |
74 ms |
113272 KB |
Output is correct |
28 |
Correct |
78 ms |
113272 KB |
Output is correct |
29 |
Correct |
80 ms |
113464 KB |
Output is correct |
30 |
Correct |
87 ms |
113272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
115320 KB |
Output is correct |
2 |
Correct |
106 ms |
115436 KB |
Output is correct |
3 |
Correct |
118 ms |
118392 KB |
Output is correct |
4 |
Correct |
108 ms |
118264 KB |
Output is correct |
5 |
Correct |
112 ms |
118264 KB |
Output is correct |
6 |
Correct |
110 ms |
118136 KB |
Output is correct |
7 |
Correct |
116 ms |
118136 KB |
Output is correct |
8 |
Correct |
103 ms |
118320 KB |
Output is correct |
9 |
Correct |
103 ms |
118136 KB |
Output is correct |
10 |
Correct |
105 ms |
118264 KB |
Output is correct |
11 |
Correct |
94 ms |
115448 KB |
Output is correct |
12 |
Correct |
84 ms |
115452 KB |
Output is correct |
13 |
Correct |
85 ms |
115320 KB |
Output is correct |
14 |
Correct |
983 ms |
157512 KB |
Output is correct |
15 |
Correct |
114 ms |
115320 KB |
Output is correct |
16 |
Correct |
94 ms |
115320 KB |
Output is correct |
17 |
Correct |
994 ms |
157608 KB |
Output is correct |
18 |
Correct |
103 ms |
115452 KB |
Output is correct |
19 |
Correct |
102 ms |
115320 KB |
Output is correct |
20 |
Correct |
982 ms |
157328 KB |
Output is correct |
21 |
Correct |
117 ms |
115320 KB |
Output is correct |
22 |
Correct |
111 ms |
118392 KB |
Output is correct |
23 |
Correct |
113 ms |
118392 KB |
Output is correct |
24 |
Correct |
120 ms |
118392 KB |
Output is correct |
25 |
Correct |
109 ms |
118368 KB |
Output is correct |
26 |
Correct |
108 ms |
118392 KB |
Output is correct |
27 |
Correct |
109 ms |
118520 KB |
Output is correct |
28 |
Correct |
120 ms |
118440 KB |
Output is correct |
29 |
Correct |
110 ms |
118520 KB |
Output is correct |
30 |
Correct |
113 ms |
118424 KB |
Output is correct |
31 |
Correct |
66 ms |
113020 KB |
Output is correct |
32 |
Correct |
65 ms |
113016 KB |
Output is correct |
33 |
Correct |
67 ms |
113020 KB |
Output is correct |
34 |
Correct |
71 ms |
113016 KB |
Output is correct |
35 |
Correct |
64 ms |
113144 KB |
Output is correct |
36 |
Correct |
64 ms |
113000 KB |
Output is correct |
37 |
Correct |
65 ms |
113016 KB |
Output is correct |
38 |
Correct |
65 ms |
113016 KB |
Output is correct |
39 |
Correct |
65 ms |
113016 KB |
Output is correct |
40 |
Correct |
69 ms |
113016 KB |
Output is correct |
41 |
Correct |
72 ms |
113144 KB |
Output is correct |
42 |
Correct |
66 ms |
113016 KB |
Output is correct |
43 |
Correct |
67 ms |
113144 KB |
Output is correct |
44 |
Correct |
67 ms |
113144 KB |
Output is correct |
45 |
Correct |
69 ms |
113144 KB |
Output is correct |
46 |
Correct |
73 ms |
113152 KB |
Output is correct |
47 |
Correct |
70 ms |
113168 KB |
Output is correct |
48 |
Correct |
71 ms |
113144 KB |
Output is correct |
49 |
Correct |
72 ms |
113144 KB |
Output is correct |
50 |
Correct |
80 ms |
113196 KB |
Output is correct |
51 |
Correct |
84 ms |
113272 KB |
Output is correct |
52 |
Correct |
75 ms |
113272 KB |
Output is correct |
53 |
Correct |
83 ms |
113272 KB |
Output is correct |
54 |
Correct |
82 ms |
113360 KB |
Output is correct |
55 |
Correct |
74 ms |
113332 KB |
Output is correct |
56 |
Correct |
88 ms |
113272 KB |
Output is correct |
57 |
Correct |
74 ms |
113272 KB |
Output is correct |
58 |
Correct |
78 ms |
113272 KB |
Output is correct |
59 |
Correct |
80 ms |
113464 KB |
Output is correct |
60 |
Correct |
87 ms |
113272 KB |
Output is correct |
61 |
Execution timed out |
1099 ms |
140048 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |