This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
7
3 8
7 2
4 2
1 4
1 9
3 4
2 3
*/
#include <bits/stdc++.h>
using namespace std;
namespace my_template {
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
}
using namespace my_template;
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 1000000;
int N;
pi edges[MX];
vpi edgesVisos[MX];
bitset<MX> been;
vi circleV;
ll ats = 0;
ll proc = 0;
ll maxDis = 0;
int kur;
void fill(int x) {
been[x] = true;
for (auto u : edgesVisos[x])
if (!been[u.x])
fill(u.x);
auto u = edges[x];
if (!been[u.x])
fill(u.x);
}
// -1 ner rato
void find_circle(int x) {
int prad = x;
int st = x;
been[x] = true;
while (true) {
auto u = edges[x];
if (been[u.x]) {
x = u.x;
st = u.x;
circleV.emplace_back(x);
x = edges[x].x;
while (x != st) {
circleV.emplace_back(x);
x = edges[x].x;
}
break;
}
x = u.x;
been[x] = true;
}
while (prad != st) {
been[prad] = false;
prad = edges[prad].x;
}
}
void longest(int x, int p, ll dis) {
if (dis > maxDis) {
maxDis = dis;
kur = x;
}
for (auto u : edgesVisos[x]) {
if (u.x == p or been[u.x]) continue;
longest(u.x, x, dis + u.y);
}
auto u = edges[x];
if (u.x == p or been[u.x]) return;
longest(u.x, x, dis + u.y);
}
void solve() {
proc = 0;
for (auto u : circleV)
been[u] = true;
int C = (int)circleV.size();
vl D(C, 0);
for (int i = 0; i < C; ++i)
{
int x = circleV[i];
ll did = 0;
ll did2 = 0;
D[i] = 0;
for (auto u : edgesVisos[x]) {
if (been[u.x]) continue;
maxDis = 0;
longest(u.x, x, u.y);
proc = max(proc, maxDis);
if (maxDis >= did) {
did2 = did;
did = maxDis;
} else if (maxDis > did2) {
did2 = maxDis;
}
D[i] = max(D[i], maxDis);
maxDis = 0;
longest(kur, -1, 0);
proc = max(proc, maxDis);
}
proc = max(proc, did + did2);
}
deque<pair<ll, int>> dq;
ll viso = 0;
for (int i = 0; i < C; ++i) viso += (ll)edges[circleV[i]].y;
ll suma = edges[circleV[0]].y;
{
for (int i = 1; i < C; ++i)
{
// edges[circleV[i]].y += edges[i -.y 1];
ll newVal = suma + D[i];
suma += (ll)edges[circleV[i]].y;
while (dq.size() and dq.back().x <= newVal)
dq.pop_back();
dq.push_back({newVal, i});
}
suma = 0;
for (int i = 0; i < C; ++i)
{
while (dq.size() and dq.front().y <= i) {
dq.pop_front();
}
proc = max(proc, dq.front().x + D[i] - suma);
ll newVal = D[i] + viso + suma;
while (dq.size() and dq.back().x <= newVal)
dq.pop_back();
dq.push_back({newVal, i + C});
suma += (ll)edges[circleV[i]].y;
}
}
for (auto u : circleV)
been[u] = false;
ats += proc;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
for (int i = 0; i < N; ++i)
{
int a, w;
cin >> a >> w; a--;
edges[i] = {a, w};
// edgesVisos[i].emplace_back(a, w);
edgesVisos[a].emplace_back(i, w);
}
for (int i = 0; i < N; ++i)
{
if (been[i]) continue;
circleV.clear();
find_circle(i);
solve();
fill(i);
}
printf("%lld\n", ats);
}
/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |