Submission #1066665

# Submission time Handle Problem Language Result Execution time Memory
1066665 2024-08-20T04:31:13 Z NeroZein Jobs (BOI24_jobs) C++17
0 / 100
164 ms 35668 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif

const int N = 3e5 + 5;

int up[N];
int dep[N]; 
int a[N], p[N];
set<int> roots;
long long dp[N];
vector<int> g[N];
long long needed[N];

int get(int v) {
  return (v == 0 ? 0 : dp[v] < 0 ? v : up[v] = get(up[v]));
}

void dfs(int v, long long sum) {
  sum += a[v];
  needed[v] = min(needed[v], sum);
  for (int u : g[v]) {
    dep[u] = dep[v] + 1;
    needed[u] = needed[v];
    dfs(u, sum); 
  }
}

void activate(int v) {
  dp[v] += a[v];
  if (dp[v] < 0) {
    return;
  }
  while (true) {
    if (v == 0 || dp[v] < 0) {
      return;
    }
    if (p[v] == 0 && dp[v] > 0) {
      roots.insert(v); 
    }
    int nv = get(v); 
    dp[nv] += dp[v];
    v = nv; 
  }
}

void dfs2(int v, long long& s) {
  s += a[v];
  for (int u : g[v]) {
    if (dp[u] > 0) {
      dfs2(u, s);
    } else {
      p[u] = 0;//new root. 
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  long long s;
  cin >> n >> s;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i] >> p[i];
    g[p[i]].push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    up[i] = p[i];
    if (p[i] == 0) {
      dfs(i, 0); 
    }
  }
  vector<int> ord(n);
  iota(ord.begin(), ord.end(), 1);
  sort(ord.begin(), ord.end(), [&](int i, int j) {
    return (needed[i] == needed[j] ? dep[i] > dep[j] : needed[i] < needed[j]);
  });
  long long curr = s; 
  while (true) {
    while (!ord.empty() && -needed[ord.back()] <= curr) {
      int v = ord.back();
      ord.pop_back();
      activate(v);
    }
    if (roots.empty()) {
      break;
    }
    while (!roots.empty()) {
      int v = *roots.begin();
      roots.erase(v);
      dfs2(v, curr); 
    }
  }
  cout << curr - s << '\n'; 
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 132 ms 30032 KB Output is correct
2 Correct 132 ms 28752 KB Output is correct
3 Correct 164 ms 32540 KB Output is correct
4 Incorrect 99 ms 35668 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 7516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 7516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 7516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 132 ms 30032 KB Output is correct
2 Correct 132 ms 28752 KB Output is correct
3 Correct 164 ms 32540 KB Output is correct
4 Incorrect 99 ms 35668 KB Output isn't correct
5 Halted 0 ms 0 KB -