class Solution {
public:
int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {
vector<vector<int>> edges(n + 1);
vector<int> inDegree(n + 1);
for (const auto& info : relations) {
edges[info[0]].push_back(info[1]);
inDegree[info[1]]++;
}
queue<int> que;
for (int i = 1; i <= n; i++) {
if (inDegree[i] == 0)
que.push(i);
}
vector<int> cost(n + 1);
int res = 0;
while (!que.empty()) {
int current = que.front();
que.pop();
cost[current] += time[current - 1];
res = max(res, cost[current]);
for (int v : edges[current]) {
cost[v] = max(cost[v], cost[current]);
if (--inDegree[v] == 0)
que.push(v);
}
}
return res;
}
};