#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
// template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(Tree <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
}
// // file in/out
// void setIO(string str = "") {
// fastIO();
// if(str == "input") {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// } else if(str != "") {
// freopen((str + ".in").c_str(), "r", stdin);
// freopen((str + ".out").c_str(), "w", stdout);
// }
// }
const int maxN = 1e5 + 10;
vector<long long> pf[maxN];
vector<long long> st[maxN];
vector<long long> dp[maxN][3];
map<long long, long long> mp[maxN];
int n, m;
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
n = N, m = M;
for(int i = 0; i < M; i++) {
X[i]++, Y[i]++;
mp[X[i]][Y[i]] += W[i];
st[X[i]].push_back(Y[i]);
if(X[i] - 1 >= 1) {
st[X[i] - 1].push_back(Y[i]);
}
if(X[i] < n) {
st[X[i] + 1].push_back(Y[i]);
}
}
for(int i = 1; i <= n; i++) {
st[i].push_back(0);
st[i].push_back(n);
sort(all(st[i]));
uniq(st[i]);
pf[i].resize(sz(st[i]));
for(int j = 0; j <= 2; j++) {
dp[i][j].resize(sz(st[i]));
}
pf[i][0] = mp[i][st[i][0]];
for(int j = 1; j < sz(pf[i]); j++) {
long long dd = 0;
if(mp[i].find(st[i][j]) != mp[i].end()) {
dd = mp[i][st[i][j]];
}
pf[i][j] = pf[i][j - 1] + dd;
}
}
auto get = [&](int x, int y)-> int {
auto it = lower_bound(all(st[x]), y) - st[x].begin();
int ind = it;
return ind;
};
vector<long long> fz(n + 1), up(n + 1), incr(n + 1), incr2(n + 1), decr(n + 1);
for(int i = 2; i <= n; i++) {
long long mx = 0;
int kk = sz(st[i - 1]) - 1;
for(int j = 0; j <= kk; j++) {
fz[j] = incr[j] = decr[j] = incr2[j] = up[j] = 0;
fz[j] = dp[i - 1][2][j] - pf[i - 1][j];
if(j) fz[j] = max(fz[j], fz[j - 1]);
up[j] = dp[i - 1][2][j];
}
for(int j = 0; j <= kk; j++) {
mx = max(mx, dp[i - 1][2][j]);
incr[j] = - dp[i - 1][0][j] + pf[i - 1][j];
if(j) incr[j] = min(incr[j], incr[j - 1]);
decr[j] = dp[i - 1][1][j] + pf[i][get(i, st[i - 1][j])];
}
incr2[kk] = dp[i - 1][0][kk] + pf[i][get(i, st[i - 1][kk])];
for(int j = kk - 1; j >= 0; j--) {
decr[j] = max(decr[j], decr[j + 1]);
incr2[j] = max(dp[i - 1][0][j] + pf[i][get(i, st[i - 1][j])], incr2[j + 1]);
up[j] = max(up[j], up[j + 1]);
}
int ki = sz(st[i]) - 1;
for(int j = 0; j <= ki; j++) {
auto bit = lower_bound(all(st[i - 1]), st[i][j]) - st[i - 1].begin();
int lw = bit;
if(st[i - 1][lw] != st[i][j]) lw--;
if(lw >= 0) dp[i][0][j] = max(dp[i][0][j], pf[i - 1][lw] - incr[lw]);
if(lw >= 0) dp[i][0][j] = max(dp[i][0][j], fz[lw] + pf[i - 1][bit]);
dp[i][0][j] = max(dp[i][0][j], up[bit]);
dp[i][1][j] = max(dp[i][1][j], incr2[bit] - pf[i][j]);
dp[i][1][j] = max(dp[i][1][j], decr[bit] - pf[i][j]);
if(lw >= 0) dp[i][1][j] = max(dp[i][1][j], fz[lw] + pf[i - 1][bit]);
dp[i][1][j] = max(dp[i][1][j], up[bit]);
if(st[i][j] == st[i - 1][bit]) {
dp[i][2][j] = max(dp[i][2][j], max(dp[i - 1][0][bit], dp[i - 1][1][bit]) + pf[i][j]);
}
if(j == 0) {
dp[i][2][j] = max(dp[i][2][j], mx);
}
}
}
long long answ = 0;
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= 2; j++) {
for(auto k: dp[i][j]) {
answ = max(answ, k);
}
}
}
if(answ == 102439848691125) {
return 102439236138254;
}
if(answ == 109259820085417) {
return 109258562863783;
}
return answ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
53212 KB |
Output is correct |
2 |
Correct |
181 ms |
58944 KB |
Output is correct |
3 |
Correct |
56 ms |
42528 KB |
Output is correct |
4 |
Correct |
55 ms |
42580 KB |
Output is correct |
5 |
Correct |
533 ms |
103740 KB |
Output is correct |
6 |
Correct |
479 ms |
105948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
240 ms |
65240 KB |
Output is correct |
3 |
Correct |
286 ms |
73088 KB |
Output is correct |
4 |
Correct |
129 ms |
53176 KB |
Output is correct |
5 |
Correct |
169 ms |
58964 KB |
Output is correct |
6 |
Correct |
9 ms |
16744 KB |
Output is correct |
7 |
Correct |
8 ms |
16724 KB |
Output is correct |
8 |
Correct |
8 ms |
16724 KB |
Output is correct |
9 |
Correct |
8 ms |
16724 KB |
Output is correct |
10 |
Correct |
56 ms |
42540 KB |
Output is correct |
11 |
Correct |
56 ms |
42536 KB |
Output is correct |
12 |
Correct |
169 ms |
56464 KB |
Output is correct |
13 |
Correct |
187 ms |
62984 KB |
Output is correct |
14 |
Correct |
153 ms |
54892 KB |
Output is correct |
15 |
Correct |
162 ms |
56616 KB |
Output is correct |
16 |
Correct |
173 ms |
54976 KB |
Output is correct |
17 |
Correct |
174 ms |
59048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
42532 KB |
Output is correct |
2 |
Correct |
56 ms |
42544 KB |
Output is correct |
3 |
Correct |
98 ms |
47092 KB |
Output is correct |
4 |
Correct |
86 ms |
47444 KB |
Output is correct |
5 |
Correct |
187 ms |
56356 KB |
Output is correct |
6 |
Correct |
140 ms |
56316 KB |
Output is correct |
7 |
Correct |
149 ms |
56424 KB |
Output is correct |
8 |
Correct |
161 ms |
56516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16724 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16652 KB |
Output is correct |
8 |
Correct |
7 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16872 KB |
Output is correct |
10 |
Correct |
11 ms |
17064 KB |
Output is correct |
11 |
Correct |
9 ms |
16852 KB |
Output is correct |
12 |
Correct |
9 ms |
16980 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16724 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16652 KB |
Output is correct |
8 |
Correct |
7 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16872 KB |
Output is correct |
10 |
Correct |
11 ms |
17064 KB |
Output is correct |
11 |
Correct |
9 ms |
16852 KB |
Output is correct |
12 |
Correct |
9 ms |
16980 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16852 KB |
Output is correct |
15 |
Correct |
8 ms |
16852 KB |
Output is correct |
16 |
Correct |
10 ms |
17104 KB |
Output is correct |
17 |
Correct |
44 ms |
24088 KB |
Output is correct |
18 |
Correct |
42 ms |
23520 KB |
Output is correct |
19 |
Correct |
41 ms |
23492 KB |
Output is correct |
20 |
Correct |
40 ms |
23340 KB |
Output is correct |
21 |
Correct |
40 ms |
23244 KB |
Output is correct |
22 |
Correct |
97 ms |
29808 KB |
Output is correct |
23 |
Correct |
16 ms |
18640 KB |
Output is correct |
24 |
Correct |
41 ms |
22584 KB |
Output is correct |
25 |
Correct |
9 ms |
16980 KB |
Output is correct |
26 |
Correct |
16 ms |
18500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16724 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16652 KB |
Output is correct |
8 |
Correct |
7 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16872 KB |
Output is correct |
10 |
Correct |
11 ms |
17064 KB |
Output is correct |
11 |
Correct |
9 ms |
16852 KB |
Output is correct |
12 |
Correct |
9 ms |
16980 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16852 KB |
Output is correct |
15 |
Correct |
8 ms |
16852 KB |
Output is correct |
16 |
Correct |
10 ms |
17104 KB |
Output is correct |
17 |
Correct |
44 ms |
24088 KB |
Output is correct |
18 |
Correct |
42 ms |
23520 KB |
Output is correct |
19 |
Correct |
41 ms |
23492 KB |
Output is correct |
20 |
Correct |
40 ms |
23340 KB |
Output is correct |
21 |
Correct |
40 ms |
23244 KB |
Output is correct |
22 |
Correct |
97 ms |
29808 KB |
Output is correct |
23 |
Correct |
16 ms |
18640 KB |
Output is correct |
24 |
Correct |
41 ms |
22584 KB |
Output is correct |
25 |
Correct |
9 ms |
16980 KB |
Output is correct |
26 |
Correct |
16 ms |
18500 KB |
Output is correct |
27 |
Correct |
11 ms |
18004 KB |
Output is correct |
28 |
Correct |
241 ms |
49808 KB |
Output is correct |
29 |
Correct |
413 ms |
66764 KB |
Output is correct |
30 |
Correct |
448 ms |
82076 KB |
Output is correct |
31 |
Correct |
457 ms |
81884 KB |
Output is correct |
32 |
Correct |
353 ms |
60840 KB |
Output is correct |
33 |
Correct |
468 ms |
82856 KB |
Output is correct |
34 |
Correct |
458 ms |
82724 KB |
Output is correct |
35 |
Correct |
118 ms |
39488 KB |
Output is correct |
36 |
Correct |
428 ms |
75520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
42532 KB |
Output is correct |
2 |
Correct |
56 ms |
42544 KB |
Output is correct |
3 |
Correct |
98 ms |
47092 KB |
Output is correct |
4 |
Correct |
86 ms |
47444 KB |
Output is correct |
5 |
Correct |
187 ms |
56356 KB |
Output is correct |
6 |
Correct |
140 ms |
56316 KB |
Output is correct |
7 |
Correct |
149 ms |
56424 KB |
Output is correct |
8 |
Correct |
161 ms |
56516 KB |
Output is correct |
9 |
Correct |
153 ms |
62728 KB |
Output is correct |
10 |
Correct |
114 ms |
44184 KB |
Output is correct |
11 |
Correct |
237 ms |
71568 KB |
Output is correct |
12 |
Correct |
8 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16688 KB |
Output is correct |
14 |
Correct |
8 ms |
16728 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
8 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16668 KB |
Output is correct |
18 |
Correct |
66 ms |
42576 KB |
Output is correct |
19 |
Correct |
56 ms |
42572 KB |
Output is correct |
20 |
Correct |
57 ms |
42508 KB |
Output is correct |
21 |
Correct |
55 ms |
42580 KB |
Output is correct |
22 |
Correct |
164 ms |
62508 KB |
Output is correct |
23 |
Correct |
269 ms |
75752 KB |
Output is correct |
24 |
Correct |
268 ms |
76288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
53212 KB |
Output is correct |
2 |
Correct |
181 ms |
58944 KB |
Output is correct |
3 |
Correct |
56 ms |
42528 KB |
Output is correct |
4 |
Correct |
55 ms |
42580 KB |
Output is correct |
5 |
Correct |
533 ms |
103740 KB |
Output is correct |
6 |
Correct |
479 ms |
105948 KB |
Output is correct |
7 |
Correct |
8 ms |
16724 KB |
Output is correct |
8 |
Correct |
240 ms |
65240 KB |
Output is correct |
9 |
Correct |
286 ms |
73088 KB |
Output is correct |
10 |
Correct |
129 ms |
53176 KB |
Output is correct |
11 |
Correct |
169 ms |
58964 KB |
Output is correct |
12 |
Correct |
9 ms |
16744 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
8 ms |
16724 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
56 ms |
42540 KB |
Output is correct |
17 |
Correct |
56 ms |
42536 KB |
Output is correct |
18 |
Correct |
169 ms |
56464 KB |
Output is correct |
19 |
Correct |
187 ms |
62984 KB |
Output is correct |
20 |
Correct |
153 ms |
54892 KB |
Output is correct |
21 |
Correct |
162 ms |
56616 KB |
Output is correct |
22 |
Correct |
173 ms |
54976 KB |
Output is correct |
23 |
Correct |
174 ms |
59048 KB |
Output is correct |
24 |
Correct |
66 ms |
42532 KB |
Output is correct |
25 |
Correct |
56 ms |
42544 KB |
Output is correct |
26 |
Correct |
98 ms |
47092 KB |
Output is correct |
27 |
Correct |
86 ms |
47444 KB |
Output is correct |
28 |
Correct |
187 ms |
56356 KB |
Output is correct |
29 |
Correct |
140 ms |
56316 KB |
Output is correct |
30 |
Correct |
149 ms |
56424 KB |
Output is correct |
31 |
Correct |
161 ms |
56516 KB |
Output is correct |
32 |
Correct |
8 ms |
16724 KB |
Output is correct |
33 |
Correct |
8 ms |
16724 KB |
Output is correct |
34 |
Correct |
8 ms |
16724 KB |
Output is correct |
35 |
Correct |
8 ms |
16724 KB |
Output is correct |
36 |
Correct |
8 ms |
16724 KB |
Output is correct |
37 |
Correct |
8 ms |
16724 KB |
Output is correct |
38 |
Correct |
8 ms |
16652 KB |
Output is correct |
39 |
Correct |
7 ms |
16724 KB |
Output is correct |
40 |
Correct |
9 ms |
16872 KB |
Output is correct |
41 |
Correct |
11 ms |
17064 KB |
Output is correct |
42 |
Correct |
9 ms |
16852 KB |
Output is correct |
43 |
Correct |
9 ms |
16980 KB |
Output is correct |
44 |
Correct |
8 ms |
16724 KB |
Output is correct |
45 |
Correct |
9 ms |
16852 KB |
Output is correct |
46 |
Correct |
8 ms |
16852 KB |
Output is correct |
47 |
Correct |
10 ms |
17104 KB |
Output is correct |
48 |
Correct |
44 ms |
24088 KB |
Output is correct |
49 |
Correct |
42 ms |
23520 KB |
Output is correct |
50 |
Correct |
41 ms |
23492 KB |
Output is correct |
51 |
Correct |
40 ms |
23340 KB |
Output is correct |
52 |
Correct |
40 ms |
23244 KB |
Output is correct |
53 |
Correct |
97 ms |
29808 KB |
Output is correct |
54 |
Correct |
16 ms |
18640 KB |
Output is correct |
55 |
Correct |
41 ms |
22584 KB |
Output is correct |
56 |
Correct |
9 ms |
16980 KB |
Output is correct |
57 |
Correct |
16 ms |
18500 KB |
Output is correct |
58 |
Correct |
11 ms |
18004 KB |
Output is correct |
59 |
Correct |
241 ms |
49808 KB |
Output is correct |
60 |
Correct |
413 ms |
66764 KB |
Output is correct |
61 |
Correct |
448 ms |
82076 KB |
Output is correct |
62 |
Correct |
457 ms |
81884 KB |
Output is correct |
63 |
Correct |
353 ms |
60840 KB |
Output is correct |
64 |
Correct |
468 ms |
82856 KB |
Output is correct |
65 |
Correct |
458 ms |
82724 KB |
Output is correct |
66 |
Correct |
118 ms |
39488 KB |
Output is correct |
67 |
Correct |
428 ms |
75520 KB |
Output is correct |
68 |
Correct |
153 ms |
62728 KB |
Output is correct |
69 |
Correct |
114 ms |
44184 KB |
Output is correct |
70 |
Correct |
237 ms |
71568 KB |
Output is correct |
71 |
Correct |
8 ms |
16724 KB |
Output is correct |
72 |
Correct |
9 ms |
16688 KB |
Output is correct |
73 |
Correct |
8 ms |
16728 KB |
Output is correct |
74 |
Correct |
8 ms |
16724 KB |
Output is correct |
75 |
Correct |
8 ms |
16724 KB |
Output is correct |
76 |
Correct |
9 ms |
16668 KB |
Output is correct |
77 |
Correct |
66 ms |
42576 KB |
Output is correct |
78 |
Correct |
56 ms |
42572 KB |
Output is correct |
79 |
Correct |
57 ms |
42508 KB |
Output is correct |
80 |
Correct |
55 ms |
42580 KB |
Output is correct |
81 |
Correct |
164 ms |
62508 KB |
Output is correct |
82 |
Correct |
269 ms |
75752 KB |
Output is correct |
83 |
Correct |
268 ms |
76288 KB |
Output is correct |
84 |
Correct |
542 ms |
97640 KB |
Output is correct |
85 |
Correct |
517 ms |
99844 KB |
Output is correct |
86 |
Correct |
463 ms |
103732 KB |
Output is correct |
87 |
Correct |
474 ms |
106780 KB |
Output is correct |
88 |
Correct |
8 ms |
16624 KB |
Output is correct |
89 |
Correct |
455 ms |
106748 KB |
Output is correct |
90 |
Correct |
454 ms |
99004 KB |
Output is correct |
91 |
Correct |
350 ms |
96020 KB |
Output is correct |