forked from av1shek/All-Coding-Platform-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooks.cpp
More file actions
53 lines (46 loc) · 1021 Bytes
/
books.cpp
File metadata and controls
53 lines (46 loc) · 1021 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int ans = 0, books;
void solve(map<int, vector<int>> v, vector<int> time, int cnt) {
int n = v.size();
if(books==cnt)
return;
unordered_map<int, int> dependents;
for(auto i: v) {
vector<int> b = i.second;
for(auto j: b)
dependents[j]=1;
}
vector<int> independents;
for(auto i: v) {
int a = i.first;
if(dependents.find(a) == dependents.end())
independents.push_back(a);
}
for(auto x: independents) {
ans+=time[x-1];
}
for(auto x: independents) {
v.erase(x);
}
solve(v, time, cnt);
}
int main() {
int n;
cin >> n;
books = n;
int t;
cin >> t;
map<int, vector<int>> v;
vector<int> time(n, 0);
for(int i = 0; i < t; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
}
for(int i = 0; i < n; i++) {
cin >> time[i];
}
solve(v, time, n);
cout << ans << endl;
}