// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#define debug(...) ((void)0)
#endif
const int kN = 200002;
int N, A[kN], H[kN], C[kN];
map<int, ll> dp[kN];
void add(map<int, ll>& mp, int pos, ll val) {
mp[pos] += val;
while (val > 0) {
auto it = mp.lower_bound(pos);
if (it == begin(mp)) break;
it = prev(it);
ll d = min(val, it->ss);
val -= d;
it->ss -= d;
if (it->ss == 0) mp.erase(it);
}
}
void merge(map<int, ll>& a, map<int, ll>& b) {
if (a.size() < b.size()) swap(a, b);
for (const auto& [pos, val]: b)
a[pos] += val;
}
int indeg[kN];
vector<vector<int>> cycles;
int vis[kN], tot;
void dfs(int u) {
vis[u] = tot;
int v = A[u];
if (vis[v] == 0) dfs(v);
else if (vis[v] == tot) {
vector<int> cycle;
do {
cycle.emplace_back(v);
v = A[v];
} while (v != u);
cycles.push_back(cycle);
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> A[i] >> H[i] >> C[i];
++indeg[A[i]];
}
for (int i = 1; i <= N; ++i)
if (vis[i] == 0) {
++tot;
dfs(i);
}
queue<int> q;
for (int i = 1; i <= N; ++i)
if (indeg[i] == 0) q.push(i);
while (!q.empty()) {
int i = q.front(); q.pop();
add(dp[i], H[i], C[i]);
merge(dp[A[i]], dp[i]);
if ((--indeg[A[i]]) == 0)
q.push(A[i]);
}
ll ans = accumulate(C + 1, C + N + 1, 0ll);
for (const vector<int>& cycle: cycles) {
map<int, ll> mp;
for (const int& u: cycle) {
add(dp[u], H[u], C[u]);
merge(mp, dp[u]);
}
for (const auto& [pos, val]: mp)
ans -= val;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
8 ms |
9736 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
10 ms |
11168 KB |
Output is correct |
6 |
Correct |
9 ms |
10524 KB |
Output is correct |
7 |
Correct |
12 ms |
10248 KB |
Output is correct |
8 |
Correct |
10 ms |
11148 KB |
Output is correct |
9 |
Correct |
8 ms |
10484 KB |
Output is correct |
10 |
Correct |
8 ms |
10316 KB |
Output is correct |
11 |
Correct |
7 ms |
10060 KB |
Output is correct |
12 |
Correct |
8 ms |
9932 KB |
Output is correct |
13 |
Correct |
9 ms |
9920 KB |
Output is correct |
14 |
Correct |
12 ms |
9864 KB |
Output is correct |
15 |
Correct |
8 ms |
9932 KB |
Output is correct |
16 |
Correct |
14 ms |
11280 KB |
Output is correct |
17 |
Correct |
10 ms |
10572 KB |
Output is correct |
18 |
Correct |
7 ms |
10060 KB |
Output is correct |
19 |
Correct |
8 ms |
10188 KB |
Output is correct |
20 |
Correct |
7 ms |
10060 KB |
Output is correct |
21 |
Correct |
7 ms |
10100 KB |
Output is correct |
22 |
Correct |
8 ms |
10444 KB |
Output is correct |
23 |
Correct |
7 ms |
10188 KB |
Output is correct |
24 |
Correct |
9 ms |
10188 KB |
Output is correct |
25 |
Correct |
7 ms |
10060 KB |
Output is correct |
26 |
Correct |
7 ms |
9860 KB |
Output is correct |
27 |
Correct |
12 ms |
10428 KB |
Output is correct |
28 |
Correct |
9 ms |
10268 KB |
Output is correct |
29 |
Correct |
8 ms |
10188 KB |
Output is correct |
30 |
Correct |
10 ms |
10380 KB |
Output is correct |
31 |
Correct |
10 ms |
10488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
8 ms |
9736 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
10 ms |
11168 KB |
Output is correct |
6 |
Correct |
9 ms |
10524 KB |
Output is correct |
7 |
Correct |
12 ms |
10248 KB |
Output is correct |
8 |
Correct |
10 ms |
11148 KB |
Output is correct |
9 |
Correct |
8 ms |
10484 KB |
Output is correct |
10 |
Correct |
8 ms |
10316 KB |
Output is correct |
11 |
Correct |
7 ms |
10060 KB |
Output is correct |
12 |
Correct |
8 ms |
9932 KB |
Output is correct |
13 |
Correct |
9 ms |
9920 KB |
Output is correct |
14 |
Correct |
12 ms |
9864 KB |
Output is correct |
15 |
Correct |
8 ms |
9932 KB |
Output is correct |
16 |
Correct |
14 ms |
11280 KB |
Output is correct |
17 |
Correct |
10 ms |
10572 KB |
Output is correct |
18 |
Correct |
7 ms |
10060 KB |
Output is correct |
19 |
Correct |
8 ms |
10188 KB |
Output is correct |
20 |
Correct |
7 ms |
10060 KB |
Output is correct |
21 |
Correct |
7 ms |
10100 KB |
Output is correct |
22 |
Correct |
8 ms |
10444 KB |
Output is correct |
23 |
Correct |
7 ms |
10188 KB |
Output is correct |
24 |
Correct |
9 ms |
10188 KB |
Output is correct |
25 |
Correct |
7 ms |
10060 KB |
Output is correct |
26 |
Correct |
7 ms |
9860 KB |
Output is correct |
27 |
Correct |
12 ms |
10428 KB |
Output is correct |
28 |
Correct |
9 ms |
10268 KB |
Output is correct |
29 |
Correct |
8 ms |
10188 KB |
Output is correct |
30 |
Correct |
10 ms |
10380 KB |
Output is correct |
31 |
Correct |
10 ms |
10488 KB |
Output is correct |
32 |
Correct |
13 ms |
11084 KB |
Output is correct |
33 |
Correct |
380 ms |
83856 KB |
Output is correct |
34 |
Correct |
282 ms |
53392 KB |
Output is correct |
35 |
Correct |
361 ms |
83232 KB |
Output is correct |
36 |
Correct |
287 ms |
53204 KB |
Output is correct |
37 |
Correct |
158 ms |
26924 KB |
Output is correct |
38 |
Correct |
103 ms |
21492 KB |
Output is correct |
39 |
Correct |
121 ms |
15208 KB |
Output is correct |
40 |
Correct |
115 ms |
15020 KB |
Output is correct |
41 |
Correct |
93 ms |
14984 KB |
Output is correct |
42 |
Correct |
113 ms |
15092 KB |
Output is correct |
43 |
Correct |
109 ms |
15076 KB |
Output is correct |
44 |
Correct |
328 ms |
94212 KB |
Output is correct |
45 |
Correct |
206 ms |
58568 KB |
Output is correct |
46 |
Correct |
91 ms |
21052 KB |
Output is correct |
47 |
Correct |
170 ms |
27844 KB |
Output is correct |
48 |
Correct |
97 ms |
20932 KB |
Output is correct |
49 |
Correct |
92 ms |
20932 KB |
Output is correct |
50 |
Correct |
226 ms |
39344 KB |
Output is correct |
51 |
Correct |
89 ms |
26800 KB |
Output is correct |
52 |
Correct |
252 ms |
28108 KB |
Output is correct |
53 |
Correct |
95 ms |
21060 KB |
Output is correct |
54 |
Correct |
80 ms |
14960 KB |
Output is correct |
55 |
Correct |
156 ms |
32724 KB |
Output is correct |
56 |
Correct |
151 ms |
28148 KB |
Output is correct |
57 |
Correct |
119 ms |
27544 KB |
Output is correct |
58 |
Correct |
176 ms |
33776 KB |
Output is correct |
59 |
Correct |
174 ms |
33800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
8 ms |
9736 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
10 ms |
11168 KB |
Output is correct |
6 |
Correct |
9 ms |
10524 KB |
Output is correct |
7 |
Correct |
12 ms |
10248 KB |
Output is correct |
8 |
Correct |
10 ms |
11148 KB |
Output is correct |
9 |
Correct |
8 ms |
10484 KB |
Output is correct |
10 |
Correct |
8 ms |
10316 KB |
Output is correct |
11 |
Correct |
7 ms |
10060 KB |
Output is correct |
12 |
Correct |
8 ms |
9932 KB |
Output is correct |
13 |
Correct |
9 ms |
9920 KB |
Output is correct |
14 |
Correct |
12 ms |
9864 KB |
Output is correct |
15 |
Correct |
8 ms |
9932 KB |
Output is correct |
16 |
Correct |
14 ms |
11280 KB |
Output is correct |
17 |
Correct |
10 ms |
10572 KB |
Output is correct |
18 |
Correct |
7 ms |
10060 KB |
Output is correct |
19 |
Correct |
8 ms |
10188 KB |
Output is correct |
20 |
Correct |
7 ms |
10060 KB |
Output is correct |
21 |
Correct |
7 ms |
10100 KB |
Output is correct |
22 |
Correct |
8 ms |
10444 KB |
Output is correct |
23 |
Correct |
7 ms |
10188 KB |
Output is correct |
24 |
Correct |
9 ms |
10188 KB |
Output is correct |
25 |
Correct |
7 ms |
10060 KB |
Output is correct |
26 |
Correct |
7 ms |
9860 KB |
Output is correct |
27 |
Correct |
12 ms |
10428 KB |
Output is correct |
28 |
Correct |
9 ms |
10268 KB |
Output is correct |
29 |
Correct |
8 ms |
10188 KB |
Output is correct |
30 |
Correct |
10 ms |
10380 KB |
Output is correct |
31 |
Correct |
10 ms |
10488 KB |
Output is correct |
32 |
Correct |
13 ms |
11084 KB |
Output is correct |
33 |
Correct |
380 ms |
83856 KB |
Output is correct |
34 |
Correct |
282 ms |
53392 KB |
Output is correct |
35 |
Correct |
361 ms |
83232 KB |
Output is correct |
36 |
Correct |
287 ms |
53204 KB |
Output is correct |
37 |
Correct |
158 ms |
26924 KB |
Output is correct |
38 |
Correct |
103 ms |
21492 KB |
Output is correct |
39 |
Correct |
121 ms |
15208 KB |
Output is correct |
40 |
Correct |
115 ms |
15020 KB |
Output is correct |
41 |
Correct |
93 ms |
14984 KB |
Output is correct |
42 |
Correct |
113 ms |
15092 KB |
Output is correct |
43 |
Correct |
109 ms |
15076 KB |
Output is correct |
44 |
Correct |
328 ms |
94212 KB |
Output is correct |
45 |
Correct |
206 ms |
58568 KB |
Output is correct |
46 |
Correct |
91 ms |
21052 KB |
Output is correct |
47 |
Correct |
170 ms |
27844 KB |
Output is correct |
48 |
Correct |
97 ms |
20932 KB |
Output is correct |
49 |
Correct |
92 ms |
20932 KB |
Output is correct |
50 |
Correct |
226 ms |
39344 KB |
Output is correct |
51 |
Correct |
89 ms |
26800 KB |
Output is correct |
52 |
Correct |
252 ms |
28108 KB |
Output is correct |
53 |
Correct |
95 ms |
21060 KB |
Output is correct |
54 |
Correct |
80 ms |
14960 KB |
Output is correct |
55 |
Correct |
156 ms |
32724 KB |
Output is correct |
56 |
Correct |
151 ms |
28148 KB |
Output is correct |
57 |
Correct |
119 ms |
27544 KB |
Output is correct |
58 |
Correct |
176 ms |
33776 KB |
Output is correct |
59 |
Correct |
174 ms |
33800 KB |
Output is correct |
60 |
Correct |
5 ms |
9676 KB |
Output is correct |
61 |
Correct |
5 ms |
9676 KB |
Output is correct |
62 |
Correct |
6 ms |
9676 KB |
Output is correct |
63 |
Correct |
6 ms |
9676 KB |
Output is correct |
64 |
Incorrect |
299 ms |
55644 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |