#include "wiring.h"
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const long long inf = 1LL << 61;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
if(r.back() < b.front()) {
int rn = r.size(), bn = b.size();
long long ans = 0;
for(int i : r) ans -= i;
for(int i : b) ans += i;
if(rn > bn) ans += (long long)b.front() * (rn - bn);
if(rn < bn) ans -= (long long)r.back() * (bn - rn);
return ans;
}
int n = r.size() + b.size();
vector<pair<int, int> > v;
for(int i : r) v.push_back(make_pair(i, 0));
for(int i : b) v.push_back(make_pair(i, 1));
sort(v.begin(), v.end());
vector<long long> dp(2 * n + 1, inf);
dp[n] = 0;
for(pair<int, int> i : v) {
vector<long long> ndp(2 * n + 1, inf);
if(i.second == 1) {
long long opt = inf;
for(int j = 0; j <= 2 * n; ++j) {
ndp[j] = min(ndp[j], opt - (long long)(abs(j - n)) * i.first);
opt = min(opt, dp[j] + (long long)(abs(j - n)) * i.first);
}
}
else {
long long opt = inf;
for(int j = 2 * n; j >= 0; --j) {
ndp[j] = min(ndp[j], opt - (long long)(abs(j - n)) * i.first);
opt = min(opt, dp[j] + (long long)(abs(j - n)) * i.first);
}
}
dp = ndp;
}
return dp[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
3 ms |
256 KB |
Output is correct |
10 |
Correct |
3 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
256 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
4 ms |
256 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
29 ms |
1704 KB |
Output is correct |
4 |
Correct |
32 ms |
2936 KB |
Output is correct |
5 |
Correct |
31 ms |
2908 KB |
Output is correct |
6 |
Correct |
37 ms |
3744 KB |
Output is correct |
7 |
Correct |
37 ms |
3816 KB |
Output is correct |
8 |
Correct |
48 ms |
3832 KB |
Output is correct |
9 |
Correct |
51 ms |
3848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Execution timed out |
1051 ms |
10088 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Execution timed out |
1050 ms |
10088 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
3 ms |
256 KB |
Output is correct |
10 |
Correct |
3 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
256 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
4 ms |
256 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
29 ms |
1704 KB |
Output is correct |
20 |
Correct |
32 ms |
2936 KB |
Output is correct |
21 |
Correct |
31 ms |
2908 KB |
Output is correct |
22 |
Correct |
37 ms |
3744 KB |
Output is correct |
23 |
Correct |
37 ms |
3816 KB |
Output is correct |
24 |
Correct |
48 ms |
3832 KB |
Output is correct |
25 |
Correct |
51 ms |
3848 KB |
Output is correct |
26 |
Correct |
3 ms |
256 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Execution timed out |
1051 ms |
10088 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |