#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define st first
#define nd second
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
int inf=1000000007;
ll infl=1000000000000000007;
const int N=100007;
set<int>points[N];
map<int,ll>DP[2][N];
map<int,ll>L[N];
map<int,ll>R[N];
map<int,ll>M[N];
vector<pair<int,ll>>V[N];
ll max_weights(int n,int m,vector<int>X,vector<int>Y,vector<int>W)
{
for(int i=0;i<m;i++)
{
X[i]++;
Y[i]++;
points[X[i]].insert(Y[i]-1);
points[X[i]-1].insert(Y[i]);
points[X[i]+1].insert(Y[i]);
V[X[i]].pb({Y[i],W[i]});
}
for(int i=1;i<=n;i++)
{
points[i].insert(0);
sort(all(V[i]));
}
for(int i=1;i<=n;i++)
{
vector<pair<int,ll>>P;
for(auto [y,c]:V[i-1]) P.pb({y,c});
for(auto y:points[i]) P.pb({y,infl});
sort(all(P));
ll sum=0;
for(auto [y,c]:P)
{
if(c==infl) L[i][y]=sum;
else sum+=c;
}
P.clear();
for(auto [y,c]:V[i+1]) P.pb({y,c});
for(auto y:points[i]) P.pb({y,infl});
sort(all(P));
sum=0;
for(auto [y,c]:P)
{
if(c==infl) R[i][y]=sum;
else sum+=c;
}
P.clear();
for(auto [y,c]:V[i]) P.pb({y,c});
for(auto y:points[i]) P.pb({y,infl});
sort(all(P));
sum=0;
for(auto [y,c]:P)
{
if(c==infl) M[i][y]=sum;
else sum+=c;
}
}
for(auto x:points[1]) DP[1][1][x]=-infl;
ll ans=0;
for(int i=2;i<=n;i++)
{
vector<pair<int,int>>P;
for(auto y:points[i-2]) P.pb({y,0});
for(auto y:points[i]) P.pb({y,1});
sort(all(P));
ll mx=0;
for(auto [y,c]:P)
{
if(c==0) mx=max({mx,DP[0][i-2][y],DP[1][i-2][y]});
else DP[0][i][y]=max(DP[0][i][y],mx+L[i][y]);
}
reverse(all(P));
mx=0;
for(auto [y,c]:P)
{
if(c==0) mx=max(mx,max(DP[0][i-2][y],DP[1][i-2][y])+R[i-2][y]);
else DP[0][i][y]=max(DP[0][i][y],mx);
}
P.clear();
for(auto y:points[i-1]) P.pb({y,0});
for(auto y:points[i]) P.pb({y,1});
sort(all(P));
mx=0;
for(auto [y,c]:P)
{
if(c==0) mx=max(mx,DP[0][i-1][y]-M[i-1][y]);
else DP[0][i][y]=max(DP[0][i][y],mx+L[i][y]);
}
reverse(all(P));
mx=0;
for(auto [y,c]:P)
{
if(c==0) mx=max(mx,max(DP[0][i-1][y],DP[1][i-1][y])+R[i-1][y]);
else DP[1][i][y]=max(DP[1][i][y],mx-M[i][y]);
}
for(auto y:points[i]) ans=max({ans,DP[0][i][y],DP[1][i][y]});
}
return ans;
}
/*
int main()
{
cout<<max_weights(5, 4,{0, 1, 4, 3},{2, 1, 4, 3},{5, 2, 1, 3})<<endl;
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
807 ms |
144888 KB |
Output is correct |
2 |
Execution timed out |
1035 ms |
167940 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
30804 KB |
Output is correct |
2 |
Execution timed out |
1085 ms |
175172 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
66716 KB |
Output is correct |
2 |
Correct |
75 ms |
66764 KB |
Output is correct |
3 |
Correct |
150 ms |
88012 KB |
Output is correct |
4 |
Correct |
135 ms |
84044 KB |
Output is correct |
5 |
Correct |
228 ms |
109928 KB |
Output is correct |
6 |
Correct |
239 ms |
109284 KB |
Output is correct |
7 |
Correct |
224 ms |
109812 KB |
Output is correct |
8 |
Correct |
238 ms |
109856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
30732 KB |
Output is correct |
2 |
Correct |
15 ms |
30808 KB |
Output is correct |
3 |
Correct |
14 ms |
30712 KB |
Output is correct |
4 |
Correct |
14 ms |
30804 KB |
Output is correct |
5 |
Correct |
14 ms |
30836 KB |
Output is correct |
6 |
Correct |
14 ms |
30824 KB |
Output is correct |
7 |
Correct |
14 ms |
30804 KB |
Output is correct |
8 |
Correct |
17 ms |
30816 KB |
Output is correct |
9 |
Correct |
15 ms |
31088 KB |
Output is correct |
10 |
Correct |
22 ms |
31984 KB |
Output is correct |
11 |
Correct |
16 ms |
31236 KB |
Output is correct |
12 |
Correct |
17 ms |
31700 KB |
Output is correct |
13 |
Correct |
15 ms |
30932 KB |
Output is correct |
14 |
Correct |
16 ms |
31444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
30732 KB |
Output is correct |
2 |
Correct |
15 ms |
30808 KB |
Output is correct |
3 |
Correct |
14 ms |
30712 KB |
Output is correct |
4 |
Correct |
14 ms |
30804 KB |
Output is correct |
5 |
Correct |
14 ms |
30836 KB |
Output is correct |
6 |
Correct |
14 ms |
30824 KB |
Output is correct |
7 |
Correct |
14 ms |
30804 KB |
Output is correct |
8 |
Correct |
17 ms |
30816 KB |
Output is correct |
9 |
Correct |
15 ms |
31088 KB |
Output is correct |
10 |
Correct |
22 ms |
31984 KB |
Output is correct |
11 |
Correct |
16 ms |
31236 KB |
Output is correct |
12 |
Correct |
17 ms |
31700 KB |
Output is correct |
13 |
Correct |
15 ms |
30932 KB |
Output is correct |
14 |
Correct |
16 ms |
31444 KB |
Output is correct |
15 |
Correct |
14 ms |
31060 KB |
Output is correct |
16 |
Correct |
20 ms |
31956 KB |
Output is correct |
17 |
Correct |
154 ms |
55444 KB |
Output is correct |
18 |
Correct |
138 ms |
52464 KB |
Output is correct |
19 |
Correct |
136 ms |
53368 KB |
Output is correct |
20 |
Correct |
121 ms |
50536 KB |
Output is correct |
21 |
Correct |
115 ms |
50124 KB |
Output is correct |
22 |
Correct |
274 ms |
69492 KB |
Output is correct |
23 |
Correct |
56 ms |
40276 KB |
Output is correct |
24 |
Correct |
150 ms |
55576 KB |
Output is correct |
25 |
Correct |
19 ms |
31868 KB |
Output is correct |
26 |
Correct |
52 ms |
38832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
30732 KB |
Output is correct |
2 |
Correct |
15 ms |
30808 KB |
Output is correct |
3 |
Correct |
14 ms |
30712 KB |
Output is correct |
4 |
Correct |
14 ms |
30804 KB |
Output is correct |
5 |
Correct |
14 ms |
30836 KB |
Output is correct |
6 |
Correct |
14 ms |
30824 KB |
Output is correct |
7 |
Correct |
14 ms |
30804 KB |
Output is correct |
8 |
Correct |
17 ms |
30816 KB |
Output is correct |
9 |
Correct |
15 ms |
31088 KB |
Output is correct |
10 |
Correct |
22 ms |
31984 KB |
Output is correct |
11 |
Correct |
16 ms |
31236 KB |
Output is correct |
12 |
Correct |
17 ms |
31700 KB |
Output is correct |
13 |
Correct |
15 ms |
30932 KB |
Output is correct |
14 |
Correct |
16 ms |
31444 KB |
Output is correct |
15 |
Correct |
14 ms |
31060 KB |
Output is correct |
16 |
Correct |
20 ms |
31956 KB |
Output is correct |
17 |
Correct |
154 ms |
55444 KB |
Output is correct |
18 |
Correct |
138 ms |
52464 KB |
Output is correct |
19 |
Correct |
136 ms |
53368 KB |
Output is correct |
20 |
Correct |
121 ms |
50536 KB |
Output is correct |
21 |
Correct |
115 ms |
50124 KB |
Output is correct |
22 |
Correct |
274 ms |
69492 KB |
Output is correct |
23 |
Correct |
56 ms |
40276 KB |
Output is correct |
24 |
Correct |
150 ms |
55576 KB |
Output is correct |
25 |
Correct |
19 ms |
31868 KB |
Output is correct |
26 |
Correct |
52 ms |
38832 KB |
Output is correct |
27 |
Correct |
21 ms |
34260 KB |
Output is correct |
28 |
Correct |
785 ms |
142448 KB |
Output is correct |
29 |
Execution timed out |
1045 ms |
193384 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
66716 KB |
Output is correct |
2 |
Correct |
75 ms |
66764 KB |
Output is correct |
3 |
Correct |
150 ms |
88012 KB |
Output is correct |
4 |
Correct |
135 ms |
84044 KB |
Output is correct |
5 |
Correct |
228 ms |
109928 KB |
Output is correct |
6 |
Correct |
239 ms |
109284 KB |
Output is correct |
7 |
Correct |
224 ms |
109812 KB |
Output is correct |
8 |
Correct |
238 ms |
109856 KB |
Output is correct |
9 |
Correct |
337 ms |
145768 KB |
Output is correct |
10 |
Correct |
200 ms |
91964 KB |
Output is correct |
11 |
Correct |
457 ms |
153232 KB |
Output is correct |
12 |
Correct |
15 ms |
30748 KB |
Output is correct |
13 |
Correct |
16 ms |
30720 KB |
Output is correct |
14 |
Correct |
14 ms |
30836 KB |
Output is correct |
15 |
Correct |
14 ms |
30844 KB |
Output is correct |
16 |
Correct |
14 ms |
30832 KB |
Output is correct |
17 |
Correct |
14 ms |
30804 KB |
Output is correct |
18 |
Correct |
77 ms |
66824 KB |
Output is correct |
19 |
Correct |
72 ms |
66740 KB |
Output is correct |
20 |
Correct |
81 ms |
66812 KB |
Output is correct |
21 |
Correct |
75 ms |
66744 KB |
Output is correct |
22 |
Correct |
500 ms |
181852 KB |
Output is correct |
23 |
Correct |
642 ms |
221332 KB |
Output is correct |
24 |
Correct |
686 ms |
230296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
807 ms |
144888 KB |
Output is correct |
2 |
Execution timed out |
1035 ms |
167940 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |