제출 #1344645

#제출 시각아이디문제언어결과실행 시간메모리
1344645avighnaWorst Reporter 4 (JOI21_worst_reporter4)C++20
14 / 100
1720 ms589824 KiB
#include <bits/stdc++.h>

using namespace std;

const int64_t inf = 1e15;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  // dp[i][j] = min cost to change subtree rooted at i, when the elem at i >= h[j]

  int n;
  cin >> n;
  vector<vector<int>> adj(n + 1);
  vector<int> h(n + 1), c(n + 1);
  vector<pair<int, int>> sort_h(n);
  for (int i = 1, a; i <= n; ++i) {
    cin >> a >> h[i] >> c[i];
    if (a != i) {
      adj[a].push_back(i);
    }
    sort_h[i - 1] = {h[i], i};
  }
  sort(sort_h.begin(), sort_h.end());

  vector<int> nodes;
  auto dfs = [&](auto &&self, int u) -> void {
    for (int &i : adj[u]) {
      self(self, i);
    }
    nodes.push_back(u);
  };
  dfs(dfs, 1);

  vector dp(n + 1, vector<int64_t>(n + 1, inf));
  vector dp_suff(n + 1, vector<int64_t>(n));
  for (int &i : nodes) {
    for (int j = 1; j <= n; ++j) {
      dp[i][j] = c[i] * (i != j); // cost to change our value
      // either we dont change root, so all children get a value >= ours
      for (int &ch : adj[i]) {
        pair<int, int> p = {h[j], -1};
        auto it = lower_bound(sort_h.begin(), sort_h.end(), p);
        int64_t best = dp_suff[ch][it - sort_h.begin()];
        dp[i][j] += best;
      }
    }
    for (int j = n - 1; j >= 0; --j) {
      dp_suff[i][j] = dp[i][sort_h[j].second];
      if (j != n - 1) {
        dp_suff[i][j] = min(dp_suff[i][j], dp_suff[i][j + 1]);
      }
    }
  }

  cout << *min_element(dp[1].begin(), dp[1].end()) << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...