https://programmers.co.kr/learn/courses/30/lessons/72416
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 300001;
int sale[MAX];
int v[MAX][2];
vector<int> team[MAX];
void FindMinValue(int turn){
v[turn][0] = 0;
v[turn][1] = sale[turn];
int minvalue = 987654321;
if(team[turn].size() == 0)
return;
for(int i=0; i<team[turn].size(); i++){
FindMinValue(team[turn][i]);
if(v[team[turn][i]][0] < v[team[turn][i]][1] ){
v[turn][0] += v[team[turn][i]][0];
v[turn][1] += v[team[turn][i]][0];
minvalue = min(minvalue,v[team[turn][i]][1]-v[team[turn][i]][0]);
}
else{
v[turn][0] += v[team[turn][i]][1];
v[turn][1] += v[team[turn][i]][1];
minvalue = 0;
}
}
v[turn][0] += minvalue;
}
int solution(vector<int> sales, vector<vector<int>> links) {
for(int i=0; i<sales.size(); i++){
sale[i] = sales[i];
}
for(int i=0; i<links.size(); i++){
team[links[i][0]-1].push_back(links[i][1]-1);
}
FindMinValue(0);
return min(v[0][0],v[0][1]);
}
'프로그래머스' 카테고리의 다른 글
불량 사용자 C++ (0) | 2021.10.02 |
---|---|
자물쇠와 열쇠 C++ (0) | 2021.08.22 |
괄호 변환 C++ (0) | 2021.08.08 |
카드 짝 맞추기 C++ (0) | 2021.08.01 |
광고 삽입 C++ (0) | 2021.07.07 |