Submission #19502252


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
#include <cstdint>
#include <string>
#include <sstream>

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; }
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using P = pair<ll,ll>;

const int INF=1001001001;
const int mod =1e9+7;

vector<vector<int>>G;
vector<int>C;
int dfs(int i){
  if(C[i]>0){return C[i];}
  int mn=INF;
  for(int nc:G[i]){
    chmin(mn,dfs(nc));
  }
  return C[i]=mn;
}
ll ans;
void dfs2(int i){
  for(int nc:G[i]){
    dfs2(nc);
    ans+=C[nc]-C[i];
  }
}
int main() {
  int N,M;
  cin>>N>>M;
  G.resize(N);
  for(int i=1;i<N;i++){
    int p;
    cin>>p;
    G[p].push_back(i);
  }
  C.resize(N);
  rep(i,M){
    int b,c;
    cin>>b>>c;
    C[b]=c;
  }
  dfs(0);
  C[0]=0;
  dfs2(0);
  cout<<ans<<endl;
  return 0;
}
  

Submission Info

Submission Time
Task B - PackDrop
User abc3
Language C++ (GCC 9.2.1)
Score 300
Code Size 1033 Byte
Status AC
Exec Time 7 ms
Memory 3736 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 3388 KB
00_sample_2 AC 2 ms 3588 KB
00_sample_3 AC 2 ms 3592 KB
10_random_00_n_5 AC 2 ms 3524 KB
10_random_01_n_10 AC 2 ms 3672 KB
10_random_02_n_2 AC 6 ms 3540 KB
10_random_03_n_7 AC 2 ms 3560 KB
10_random_04_n_6 AC 2 ms 3404 KB
20_random_00_n_64 AC 2 ms 3564 KB
20_random_01_n_95 AC 6 ms 3592 KB
20_random_02_n_20 AC 2 ms 3596 KB
20_random_03_n_33 AC 2 ms 3520 KB
20_random_04_n_91 AC 2 ms 3392 KB
30_random_00_n_793 AC 2 ms 3600 KB
30_random_01_n_611 AC 2 ms 3648 KB
30_random_02_n_852 AC 3 ms 3600 KB
40_random_00_n_1000 AC 3 ms 3632 KB
40_random_01_n_1000 AC 3 ms 3676 KB
50_edge_one_00_n_11 AC 3 ms 3600 KB
50_edge_one_01_n_101 AC 2 ms 3556 KB
50_edge_one_02_n_999 AC 2 ms 3684 KB
98_almost_straight_00_n_1000 AC 2 ms 3624 KB
98_almost_straight_01_n_1000 AC 2 ms 3540 KB
98_almost_straight_02_n_1000 AC 2 ms 3736 KB
99_straight_00_n_10 AC 2 ms 3520 KB
99_straight_01_n_100 AC 2 ms 3640 KB
99_straight_02_n_1000 AC 2 ms 3620 KB