이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
컴파일 시 표준 에러 (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... |