#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#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]);
register 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(register 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(register int i=0;i<=k;i++)
ans=max(ans,no[1][i]);
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
115576 KB |
Output is correct |
2 |
Correct |
114 ms |
115576 KB |
Output is correct |
3 |
Correct |
114 ms |
115576 KB |
Output is correct |
4 |
Correct |
111 ms |
115576 KB |
Output is correct |
5 |
Correct |
120 ms |
115576 KB |
Output is correct |
6 |
Correct |
112 ms |
115576 KB |
Output is correct |
7 |
Correct |
115 ms |
115576 KB |
Output is correct |
8 |
Correct |
117 ms |
115672 KB |
Output is correct |
9 |
Correct |
118 ms |
115576 KB |
Output is correct |
10 |
Correct |
116 ms |
115576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
115520 KB |
Output is correct |
2 |
Correct |
95 ms |
115692 KB |
Output is correct |
3 |
Correct |
95 ms |
115580 KB |
Output is correct |
4 |
Correct |
651 ms |
159092 KB |
Output is correct |
5 |
Correct |
120 ms |
115704 KB |
Output is correct |
6 |
Correct |
99 ms |
115576 KB |
Output is correct |
7 |
Correct |
664 ms |
163276 KB |
Output is correct |
8 |
Correct |
116 ms |
115704 KB |
Output is correct |
9 |
Correct |
114 ms |
115576 KB |
Output is correct |
10 |
Correct |
644 ms |
160092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
115448 KB |
Output is correct |
2 |
Correct |
124 ms |
115536 KB |
Output is correct |
3 |
Correct |
116 ms |
115564 KB |
Output is correct |
4 |
Correct |
125 ms |
115448 KB |
Output is correct |
5 |
Correct |
121 ms |
115448 KB |
Output is correct |
6 |
Correct |
122 ms |
115448 KB |
Output is correct |
7 |
Correct |
115 ms |
115576 KB |
Output is correct |
8 |
Correct |
116 ms |
115576 KB |
Output is correct |
9 |
Correct |
125 ms |
115576 KB |
Output is correct |
10 |
Correct |
117 ms |
115576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
113016 KB |
Output is correct |
2 |
Correct |
81 ms |
113016 KB |
Output is correct |
3 |
Correct |
76 ms |
113016 KB |
Output is correct |
4 |
Correct |
80 ms |
113016 KB |
Output is correct |
5 |
Correct |
76 ms |
113016 KB |
Output is correct |
6 |
Correct |
75 ms |
113016 KB |
Output is correct |
7 |
Correct |
76 ms |
113016 KB |
Output is correct |
8 |
Correct |
79 ms |
113016 KB |
Output is correct |
9 |
Correct |
80 ms |
113016 KB |
Output is correct |
10 |
Correct |
76 ms |
113016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
113016 KB |
Output is correct |
2 |
Correct |
81 ms |
113016 KB |
Output is correct |
3 |
Correct |
76 ms |
113016 KB |
Output is correct |
4 |
Correct |
80 ms |
113016 KB |
Output is correct |
5 |
Correct |
76 ms |
113016 KB |
Output is correct |
6 |
Correct |
75 ms |
113016 KB |
Output is correct |
7 |
Correct |
76 ms |
113016 KB |
Output is correct |
8 |
Correct |
79 ms |
113016 KB |
Output is correct |
9 |
Correct |
80 ms |
113016 KB |
Output is correct |
10 |
Correct |
76 ms |
113016 KB |
Output is correct |
11 |
Correct |
78 ms |
113144 KB |
Output is correct |
12 |
Correct |
83 ms |
113144 KB |
Output is correct |
13 |
Correct |
77 ms |
113096 KB |
Output is correct |
14 |
Correct |
75 ms |
113144 KB |
Output is correct |
15 |
Correct |
78 ms |
113144 KB |
Output is correct |
16 |
Correct |
73 ms |
113144 KB |
Output is correct |
17 |
Correct |
78 ms |
113272 KB |
Output is correct |
18 |
Correct |
77 ms |
113144 KB |
Output is correct |
19 |
Correct |
74 ms |
113144 KB |
Output is correct |
20 |
Correct |
76 ms |
113272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
113016 KB |
Output is correct |
2 |
Correct |
81 ms |
113016 KB |
Output is correct |
3 |
Correct |
76 ms |
113016 KB |
Output is correct |
4 |
Correct |
80 ms |
113016 KB |
Output is correct |
5 |
Correct |
76 ms |
113016 KB |
Output is correct |
6 |
Correct |
75 ms |
113016 KB |
Output is correct |
7 |
Correct |
76 ms |
113016 KB |
Output is correct |
8 |
Correct |
79 ms |
113016 KB |
Output is correct |
9 |
Correct |
80 ms |
113016 KB |
Output is correct |
10 |
Correct |
76 ms |
113016 KB |
Output is correct |
11 |
Correct |
78 ms |
113144 KB |
Output is correct |
12 |
Correct |
83 ms |
113144 KB |
Output is correct |
13 |
Correct |
77 ms |
113096 KB |
Output is correct |
14 |
Correct |
75 ms |
113144 KB |
Output is correct |
15 |
Correct |
78 ms |
113144 KB |
Output is correct |
16 |
Correct |
73 ms |
113144 KB |
Output is correct |
17 |
Correct |
78 ms |
113272 KB |
Output is correct |
18 |
Correct |
77 ms |
113144 KB |
Output is correct |
19 |
Correct |
74 ms |
113144 KB |
Output is correct |
20 |
Correct |
76 ms |
113272 KB |
Output is correct |
21 |
Correct |
81 ms |
113400 KB |
Output is correct |
22 |
Correct |
77 ms |
113400 KB |
Output is correct |
23 |
Correct |
80 ms |
113500 KB |
Output is correct |
24 |
Correct |
86 ms |
113400 KB |
Output is correct |
25 |
Correct |
80 ms |
113400 KB |
Output is correct |
26 |
Correct |
80 ms |
113400 KB |
Output is correct |
27 |
Correct |
86 ms |
113400 KB |
Output is correct |
28 |
Correct |
78 ms |
113400 KB |
Output is correct |
29 |
Correct |
78 ms |
113400 KB |
Output is correct |
30 |
Correct |
85 ms |
113376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
115576 KB |
Output is correct |
2 |
Correct |
114 ms |
115576 KB |
Output is correct |
3 |
Correct |
114 ms |
115576 KB |
Output is correct |
4 |
Correct |
111 ms |
115576 KB |
Output is correct |
5 |
Correct |
120 ms |
115576 KB |
Output is correct |
6 |
Correct |
112 ms |
115576 KB |
Output is correct |
7 |
Correct |
115 ms |
115576 KB |
Output is correct |
8 |
Correct |
117 ms |
115672 KB |
Output is correct |
9 |
Correct |
118 ms |
115576 KB |
Output is correct |
10 |
Correct |
116 ms |
115576 KB |
Output is correct |
11 |
Correct |
105 ms |
115520 KB |
Output is correct |
12 |
Correct |
95 ms |
115692 KB |
Output is correct |
13 |
Correct |
95 ms |
115580 KB |
Output is correct |
14 |
Correct |
651 ms |
159092 KB |
Output is correct |
15 |
Correct |
120 ms |
115704 KB |
Output is correct |
16 |
Correct |
99 ms |
115576 KB |
Output is correct |
17 |
Correct |
664 ms |
163276 KB |
Output is correct |
18 |
Correct |
116 ms |
115704 KB |
Output is correct |
19 |
Correct |
114 ms |
115576 KB |
Output is correct |
20 |
Correct |
644 ms |
160092 KB |
Output is correct |
21 |
Correct |
125 ms |
115448 KB |
Output is correct |
22 |
Correct |
124 ms |
115536 KB |
Output is correct |
23 |
Correct |
116 ms |
115564 KB |
Output is correct |
24 |
Correct |
125 ms |
115448 KB |
Output is correct |
25 |
Correct |
121 ms |
115448 KB |
Output is correct |
26 |
Correct |
122 ms |
115448 KB |
Output is correct |
27 |
Correct |
115 ms |
115576 KB |
Output is correct |
28 |
Correct |
116 ms |
115576 KB |
Output is correct |
29 |
Correct |
125 ms |
115576 KB |
Output is correct |
30 |
Correct |
117 ms |
115576 KB |
Output is correct |
31 |
Correct |
77 ms |
113016 KB |
Output is correct |
32 |
Correct |
81 ms |
113016 KB |
Output is correct |
33 |
Correct |
76 ms |
113016 KB |
Output is correct |
34 |
Correct |
80 ms |
113016 KB |
Output is correct |
35 |
Correct |
76 ms |
113016 KB |
Output is correct |
36 |
Correct |
75 ms |
113016 KB |
Output is correct |
37 |
Correct |
76 ms |
113016 KB |
Output is correct |
38 |
Correct |
79 ms |
113016 KB |
Output is correct |
39 |
Correct |
80 ms |
113016 KB |
Output is correct |
40 |
Correct |
76 ms |
113016 KB |
Output is correct |
41 |
Correct |
78 ms |
113144 KB |
Output is correct |
42 |
Correct |
83 ms |
113144 KB |
Output is correct |
43 |
Correct |
77 ms |
113096 KB |
Output is correct |
44 |
Correct |
75 ms |
113144 KB |
Output is correct |
45 |
Correct |
78 ms |
113144 KB |
Output is correct |
46 |
Correct |
73 ms |
113144 KB |
Output is correct |
47 |
Correct |
78 ms |
113272 KB |
Output is correct |
48 |
Correct |
77 ms |
113144 KB |
Output is correct |
49 |
Correct |
74 ms |
113144 KB |
Output is correct |
50 |
Correct |
76 ms |
113272 KB |
Output is correct |
51 |
Correct |
81 ms |
113400 KB |
Output is correct |
52 |
Correct |
77 ms |
113400 KB |
Output is correct |
53 |
Correct |
80 ms |
113500 KB |
Output is correct |
54 |
Correct |
86 ms |
113400 KB |
Output is correct |
55 |
Correct |
80 ms |
113400 KB |
Output is correct |
56 |
Correct |
80 ms |
113400 KB |
Output is correct |
57 |
Correct |
86 ms |
113400 KB |
Output is correct |
58 |
Correct |
78 ms |
113400 KB |
Output is correct |
59 |
Correct |
78 ms |
113400 KB |
Output is correct |
60 |
Correct |
85 ms |
113376 KB |
Output is correct |
61 |
Correct |
828 ms |
158192 KB |
Output is correct |
62 |
Correct |
844 ms |
157612 KB |
Output is correct |
63 |
Correct |
834 ms |
162328 KB |
Output is correct |
64 |
Correct |
845 ms |
156468 KB |
Output is correct |
65 |
Correct |
838 ms |
159464 KB |
Output is correct |
66 |
Correct |
840 ms |
157860 KB |
Output is correct |
67 |
Correct |
829 ms |
158448 KB |
Output is correct |
68 |
Correct |
842 ms |
161892 KB |
Output is correct |
69 |
Correct |
832 ms |
158988 KB |
Output is correct |
70 |
Correct |
829 ms |
158028 KB |
Output is correct |