#include "bits/stdc++.h"
using namespace std;
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
const int nax = 1e6;
int n;
int p[nax+5];
bool vis[nax+5];
long long c[nax+5];
long long val[nax+5];
vector<pii> adj[nax+5];
vector<int> cyc[nax+5];
vector<pii> edg;
void gp(int u, int par, int ls=0) {
p[u] = par;
for(pii t : adj[u]) {
int v = t.fi;
int e = t.se;
if(e == ls) continue;
if(p[v] == 0) {
gp(v, par, e);
} else {
cyc[par].push_back(e);
}
}
}
bool fc(int u, int go, int ls) {
if(u == go) return true;
for(pii t : adj[u]) {
int v = t.fi;
int e = t.se;
if(e == ls) continue;
bool ok = fc(v, go, e);
if(ok) {
vis[u] = true;
vis[v] = true;
if(edg[e].fi == u) swap(edg[e].fi, edg[e].se);
cyc[p[go]].push_back(e);
return true;
}
}
return false;
}
long long trav(int u) {
if(val[u] != -1) return val[u];
vis[u] = true;
long long res = 0;
for(pii t : adj[u]) {
int v = t.fi;
int e = t.se;
if(!vis[v]) {
res = max(res, c[e] + trav(v));
}
}
return val[u] = res;
}
int main () {
scanf("%d", &n);
edg.pb({0,0});
for(int i=1; i<=n; i++) {
long long l, j;
scanf("%lld %lld", &j, &l);
c[i] = l;
adj[j].pb({i,i});
adj[i].pb({j,i});
edg.pb({i,j});
val[i] = -1;
}
long long ans = 0;
for(int i=1; i<=n; i++) {
if(p[i] == 0) {
gp(i,i);
cyc[i].pop_back();
fc(edg[cyc[i][0]].se, edg[cyc[i][0]].fi, cyc[i][0]);
swap(edg[cyc[i][0]].fi, edg[cyc[i][0]].se);
long long opt = 0;
//priority_queue<long long> pq;
deque<pair<long long, int>> dq;
long long dpq = 0;
long long sum = 0;
int sz = cyc[i].size();
int cnt = 0;
for(int j=0; j<sz; j++) {
int e = cyc[i][j];
sum += c[e];
if(j+1 < sz) {
long long temp = sum + trav(edg[e].se);
while(!dq.empty() && dq.back().fi <= temp) dq.pop_back();
dq.push_back({temp,cnt++});
}
}
for(int j=0; j<sz; j++) {
int e = cyc[i][j];
int u = edg[e].fi;
opt = max(opt, trav(u) + dq.front().fi + dpq);
dpq -= c[e];
long long temp = trav(u) + sum - c[e] - dpq;
while(!dq.empty() && dq.front().se <= cnt-sz) dq.pop_front();
while(!dq.empty() && dq.back().fi <= temp) dq.pop_back();
dq.push_back({temp,cnt++});
}
ans += opt;
}
}
printf("%lld", ans);
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
islands.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%lld %lld", &j, &l);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
47188 KB |
Output is correct |
2 |
Incorrect |
29 ms |
47280 KB |
Output isn't correct |
3 |
Correct |
24 ms |
47288 KB |
Output is correct |
4 |
Correct |
22 ms |
47204 KB |
Output is correct |
5 |
Correct |
23 ms |
47212 KB |
Output is correct |
6 |
Correct |
24 ms |
47316 KB |
Output is correct |
7 |
Correct |
24 ms |
47288 KB |
Output is correct |
8 |
Correct |
23 ms |
47212 KB |
Output is correct |
9 |
Correct |
23 ms |
47188 KB |
Output is correct |
10 |
Incorrect |
26 ms |
47288 KB |
Output isn't correct |
11 |
Correct |
25 ms |
47188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
47316 KB |
Output is correct |
2 |
Correct |
28 ms |
47320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47408 KB |
Output is correct |
2 |
Correct |
30 ms |
47752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
48528 KB |
Output is correct |
2 |
Correct |
43 ms |
51148 KB |
Output is correct |
3 |
Correct |
40 ms |
48568 KB |
Output is correct |
4 |
Incorrect |
33 ms |
47952 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
52652 KB |
Output is correct |
2 |
Correct |
59 ms |
55616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
62336 KB |
Output is correct |
2 |
Correct |
113 ms |
69248 KB |
Output is correct |
3 |
Correct |
173 ms |
79412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
176 ms |
75204 KB |
Output is correct |
2 |
Correct |
248 ms |
100904 KB |
Output is correct |
3 |
Correct |
325 ms |
110664 KB |
Output is correct |
4 |
Correct |
319 ms |
131072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
394 ms |
114680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
427 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |