#include <iostream> using namespace std; int main() { int station, foreign; cin >> station >> foreign; int * left=new int [station-1]; int * right=new int [station-1]; int * counter=new int [foreign]; int * Sortcounter=new int [foreign]; int cost=0; int * test=new int [foreign]; int mini=500000, maxi=0; for(int i=0;i<station-1;i++) { cin >> left[i] >> right[i]; } for(int i=0;i<foreign;i++) { cin >> counter[i]; } int T=0; for(int i=0;i<station-1;i++) { if(left[i]<foreign) { Sortcounter[T]= counter[left[i]]; T++; } if(right[i]<foreign) { Sortcounter[T]= counter[right[i]]; T++; } } for(int i=1;i<foreign;i++) { if(Sortcounter[i-1]>=Sortcounter[i]) { mini=Sortcounter[i]; cost+=Sortcounter[i-1]-Sortcounter[i]; } else { maxi=Sortcounter[i]; cost+=Sortcounter[i]-Sortcounter[i-1]; } if(Sortcounter[i-1]<Sortcounter[i]) { maxi=Sortcounter[i]; cost+=Sortcounter[i]-Sortcounter[i-1]; } else { mini=Sortcounter[i]; cost+=Sortcounter[i-1]-Sortcounter[i]; } if(i+2<foreign) { if(Sortcounter[i+1]>maxi || Sortcounter[i+2] >maxi) { Sortcounter[i]=maxi; } else { Sortcounter[i]=mini; } } } cout << cost << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #include <iostream> using namespace std; int main() { int station, foreign; cin >> station >> foreign; int * left=new int [station-1]; int * right=new int [station-1]; int * counter=new int [foreign]; int * Sortcounter=new int [foreign]; int cost=0; int * test=new int [foreign]; int mini=500000, maxi=0; for(int i=0;i<station-1;i++) { cin >> left[i] >> right[i]; } for(int i=0;i<foreign;i++) { cin >> counter[i]; } int T=0; for(int i=0;i<station-1;i++) { if(left[i]<foreign) { Sortcounter[T]= counter[left[i]]; T++; } if(right[i]<foreign) { Sortcounter[T]= counter[right[i]]; T++; } } for(int i=1;i<foreign;i++) { if(Sortcounter[i-1]>=Sortcounter[i]) { mini=Sortcounter[i]; cost+=Sortcounter[i-1]-Sortcounter[i]; } else { maxi=Sortcounter[i]; cost+=Sortcounter[i]-Sortcounter[i-1]; } if(Sortcounter[i-1]<Sortcounter[i]) { maxi=Sortcounter[i]; cost+=Sortcounter[i]-Sortcounter[i-1]; } else { mini=Sortcounter[i]; cost+=Sortcounter[i-1]-Sortcounter[i]; } if(i+2<foreign) { if(Sortcounter[i+1]>maxi || Sortcounter[i+2] >maxi) { Sortcounter[i]=maxi; } else { Sortcounter[i]=mini; } } } cout << cost << endl; return 0; } |