이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define rall(s) s.rbegin(), s.rend()
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define s second
#define f first
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int N = 1e6;
const ll inf = 1e18;
vector<int> g[N];
ll pos[N], val[N], x[N], p[N];
void dfs(int u) {
if (x[u] < 0) pos[u] = abs(x[u]), val[u] = 0;
else pos[u] = 0, val[u] = x[u];
for (int to: g[u]) {
dfs(to);
if (pos[to] <= val[u]) {
val[u] = val[u] - pos[to] + val[to];
}else {
pos[u] = pos[to] - val[u] + pos[u];
val[u] = val[to];
}
}
if (!val[u]) pos[u] = 0;
}
void slav(int n, ll s) {
vector<int> h;
ll ans = 0, z = s;
for (int i = 1; i <= n + 1; i++) {
if (p[i]) h.push_back(i);
else {
if (sz(h)) {
ll pr[sz(h)], mn = 0, mx = 0, val = 0;
for (int j = 0; j < sz(h); j++) {
if (j) pr[j] = pr[j - 1];
else pr[j] = 0;
pr[j] += x[h[j]];
mn = min(mn, pr[j]);
mx = max(mx, pr[j]);
val = max(pr[j] - mn, val);
}
s += mx;
ans += val;
}
h = {i};
}
}
cout << max(ans, s) - z;
exit(0);
}
void solve() {
int n, ok = 1;
ll s;
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> x[i] >> p[i];
if (p[i] && p[i] != i - 1) ok = 0;
}
slav(n, s);
return;
for (int i = 1; i <= n; i++) {
g[p[i]].push_back(i);
}
dfs(0);
if (s <= pos[0]) cout << val[0] - s;
else cout << val[0] - pos[0];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void solve()':
Main.cpp:64:9: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
64 | int n, ok = 1;
| ^~
# | 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... |