Submission #875558

# Submission time Handle Problem Language Result Execution time Memory
875558 2023-11-20T03:46:16 Z NeroZein Fireworks (APIO16_fireworks) C++17
7 / 100
1 ms 600 KB
#include "bits/stdc++.h"
using namespace std;
 
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
 
const int N = 5002;
 
long long dis[N]; 
long long mn[N], mx[N]; 
vector<pair<int, int>> g[N]; 
 
void dfs(int v) {
  if (g[v].size() == 0) {
    mn[v] = mx[v] = dis[v];
    return; 
  }
  mn[v] = LLONG_MAX;
  mx[v] = -LLONG_MAX;
  for (auto [u, w] : g[v]) {
    dis[u] = dis[v] + w; 
    dfs(u); 
    mn[v] = min(mn[v], mn[u]);
    mx[v] = max(mx[v], mx[u]);
  }
}
 
long long dfs2(int v, long long add, const long long req) {
  if (g[v].size() == 0 && dis[v] + add != req) {
    return (long long) 1e15;  
  }
  long long ret = 0;
  for (auto [u, w] : g[v]) {
    long long t = 0, r = 1; 
    if (mn[u] + add > req) {
      assert(add <= 0); 
      r = -1; 
      t = min((long long) w, mn[u] + add - req); 
    } else if (mx[u] + add < req) {
      assert(add >= 0);
      t = req - (mx[u] + add); 
    }
    ret += t + dfs2(u, add + t * r, req); 
  }
  return ret;
}
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for(int i = 2; i <= n + m; ++i) {
    int p, c;
    cin >> p >> c;
    g[p].emplace_back(i, c); 
  }
  dfs(1); 
  long long ans = LLONG_MAX; 
  for (int i = n + 1; i <= n + m; ++i) {
    ans = min(ans, dfs2(1, 0, dis[i])); 
  }
  cout << ans << '\n'; 
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 600 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Incorrect 1 ms 600 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Incorrect 1 ms 600 KB Output isn't correct
13 Halted 0 ms 0 KB -