#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
long long scheduling_cost(vector<int> p, vector<int> u, vector<int> d) {
int n = (int) p.size();
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
g[p[i]].emplace_back(i);
}
auto Merge = [&](vector<int> a, vector<int> b) {
int sa = (int) a.size(), sb = (int) b.size();
vector<long long> da(sa + 1), db(sb + 1);
for (int i = 0; i < sa; i++) {
da[i + 1] = da[i] + d[a[i]];
}
for (int i = 0; i < sb; i++) {
db[i + 1] = db[i] + d[b[i]];
}
const long long inf = (long long) 1e18;
vector<vector<long long>> dp(sa + 1, vector<long long>(sb + 1, inf));
dp[0][0] = 0;
for (int i = 0; i <= sa; i++) {
for (int j = 0; j <= sb; j++) {
if (i < sa) {
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (da[i + 1] + db[j]) * u[a[i]]);
}
if (j < sb) {
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + (da[i] + db[j + 1]) * u[b[j]]);
}
}
}
vector<int> c;
int x = sa, y = sb;
while (x > 0 || y > 0) {
if (x > 0 && dp[x][y] == dp[x - 1][y] + (da[x] + db[y]) * u[a[x - 1]]) {
c.emplace_back(a[x - 1]);
x--;
} else if (y > 0 && dp[x][y] == dp[x][y - 1] + (da[x] + db[y]) * u[b[y - 1]]) {
c.emplace_back(b[y - 1]);
y--;
} else {
assert(false);
}
}
reverse(c.begin(), c.end());
return c;
};
vector<vector<int>> a(n);
function<void(int)> Dfs = [&](int v) {
for (int to : g[v]) {
Dfs(to);
a[v] = Merge(a[v], a[to]);
}
a[v].insert(a[v].begin(), v);
};
Dfs(0);
long long res = 0;
long long t = 0;
for (int i : a[0]) {
t += d[i];
res += t * u[i];
}
return res;
}
#ifdef tabr
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
debug(scheduling_cost({-1, 0, 0}, {5, 2, 5}, {3, 4, 1}));
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
8 ms |
4052 KB |
Output is correct |
5 |
Runtime error |
507 ms |
262144 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
3058 ms |
16988 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
39 ms |
484 KB |
Output is correct |
5 |
Execution timed out |
3052 ms |
2676 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
501 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
428 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
8 ms |
4052 KB |
Output is correct |
5 |
Runtime error |
507 ms |
262144 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |