Submission #19377912


Source Code Expand

Copy
//#include <atcoder/all>
//using namespace atcoder;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPR(i, n) for(int i=n-1; i>=0; i--)
#define FOR(i, m, n) for(int i=m; i<n; i++)
#define ALL(v) v.begin(), v.end()
#define bit(n) (1LL<<(n))
#define FIX(d) fixed << setprecision(d)
using P = pair<int, int>;
using LP = pair<ll, ll>;
using Graph = vector<vector<int>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = 1e9;
const ll LINF = (1LL<<60);

int n,m;
Graph G;
vector<int> cnt;

void dfs1(int cur) {
  for(auto nx: G[cur]){
    dfs1(nx);
    chmin(cnt[cur], cnt[nx]);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  G.resize(n);
  FOR(i,1,n){
    int p; cin >> p;
    G[p].push_back(i);
  }
  cnt.assign(n, INF);
  cnt[0] = 0;
  REP(i,m){
    int b,c; cin >> b >> c;
    cnt[b] = c;
  }
  
  dfs1(0);
  
  int ans = 0;
  queue<int> que;
  que.push(0);
  while(!que.empty()){
    int cur = que.front();
    que.pop();
    for(auto nx: G[cur]){
      ans += cnt[nx] - cnt[cur];
      que.push(nx);
    }
  }
  cout << ans << endl;
  return 0;
}

Submission Info

Submission Time
Task B - PackDrop
User vistoron15
Language C++ (GCC 9.2.1)
Score 300
Code Size 1375 Byte
Status AC
Exec Time 7 ms
Memory 3660 KB

Judge Result

Set Name All
Score / Max Score 300 / 300
Status
AC × 27
Set Name Test Cases
All 00_sample_1, 00_sample_2, 00_sample_3, 10_random_00_n_5, 10_random_01_n_10, 10_random_02_n_2, 10_random_03_n_7, 10_random_04_n_6, 20_random_00_n_64, 20_random_01_n_95, 20_random_02_n_20, 20_random_03_n_33, 20_random_04_n_91, 30_random_00_n_793, 30_random_01_n_611, 30_random_02_n_852, 40_random_00_n_1000, 40_random_01_n_1000, 50_edge_one_00_n_11, 50_edge_one_01_n_101, 50_edge_one_02_n_999, 98_almost_straight_00_n_1000, 98_almost_straight_01_n_1000, 98_almost_straight_02_n_1000, 99_straight_00_n_10, 99_straight_01_n_100, 99_straight_02_n_1000
Case Name Status Exec Time Memory
00_sample_1 AC 7 ms 3552 KB
00_sample_2 AC 2 ms 3420 KB
00_sample_3 AC 2 ms 3452 KB
10_random_00_n_5 AC 3 ms 3508 KB
10_random_01_n_10 AC 2 ms 3448 KB
10_random_02_n_2 AC 2 ms 3504 KB
10_random_03_n_7 AC 2 ms 3572 KB
10_random_04_n_6 AC 2 ms 3456 KB
20_random_00_n_64 AC 2 ms 3552 KB
20_random_01_n_95 AC 4 ms 3460 KB
20_random_02_n_20 AC 2 ms 3552 KB
20_random_03_n_33 AC 2 ms 3512 KB
20_random_04_n_91 AC 2 ms 3520 KB
30_random_00_n_793 AC 2 ms 3548 KB
30_random_01_n_611 AC 3 ms 3488 KB
30_random_02_n_852 AC 2 ms 3592 KB
40_random_00_n_1000 AC 2 ms 3528 KB
40_random_01_n_1000 AC 3 ms 3632 KB
50_edge_one_00_n_11 AC 2 ms 3448 KB
50_edge_one_01_n_101 AC 2 ms 3488 KB
50_edge_one_02_n_999 AC 2 ms 3540 KB
98_almost_straight_00_n_1000 AC 2 ms 3608 KB
98_almost_straight_01_n_1000 AC 5 ms 3612 KB
98_almost_straight_02_n_1000 AC 3 ms 3660 KB
99_straight_00_n_10 AC 4 ms 3604 KB
99_straight_01_n_100 AC 3 ms 3560 KB
99_straight_02_n_1000 AC 2 ms 3528 KB