This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i, n) for(int i = 1; (i) <= (n); ++i)
#define forn(i, l, r) for(int i = (l); i <= (r); ++i)
#define ford(i, r, l) for(int i = (r); i >= (l); --i)
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define FORD(i, n) for(int i = ((n) - 1); i >= 0; --i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task "FFILL"
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
const int N = 3e5 + 3;
const int LIM = (1 << 16) + 3;
const int INF = 1e9 + 1;
int n;
ll k;
int val[N], par[N];
priority_queue<pll, vector<pll>, greater<pll>> pq[N];
void solve()
{
cin >> n >> k;
rep(i, n) cin >> val[i] >> par[i];
ford(i, n, 0)
{
ll p = max(0ll, -val[i]), v = val[i];
while(!pq[i].empty() && v <= 0)
{
pll tmp = pq[i].top(); pq[i].pop();
p = max(p, tmp.fi - v);v += tmp.se;
}
while(!pq[i].empty())
{
pll tmp = pq[i].top();
if(tmp.fi > p + v) break;
pq[i].pop();
v += tmp.se;
}
if(v > 0) pq[i].push({p, v});
int u = par[i];
if(i != 0)
{
if(sz(pq[u]) < sz(pq[i])) swap(pq[u], pq[i]);
while(!pq[i].empty())
{
pq[u].push(pq[i].top());
pq[i].pop();
}
}
}
ll res = k;
while(!pq[0].empty() && pq[0].top().fi <= res) res += pq[0].top().se, pq[0].pop();
cout << res - k;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int TC = 1;
if(fopen("note.inp", "r"))
{
freopen("note.inp", "r", stdin);
freopen("note.out", "w", stdout);
}
while(TC--)
{
solve();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen("note.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen("note.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |