#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
int sum_order[N], diff_order[N], n;
long long sum[N], diff[N], x[N];
long long find_shortcut(int n, vector<int> l, vector<int> d, int c) {
::n = n;
for (int i = 0; i < n - 1; ++i) {
x[i + 1] = x[i] + l[i];
}
for (int i = 0; i < n; ++i) {
sum[i] = x[i] + d[i];
diff[i] = x[i] - d[i];
}
iota(sum_order, sum_order + n, 0);
sort(sum_order, sum_order + n, [&](int a, int b) {
return sum[a] < sum[b];
});
iota(diff_order, diff_order + n, 0);
sort(diff_order, diff_order + n, [&](int a, int b) {
return diff[a] < diff[b];
});
long long low = 0, high = sum[sum_order[n - 1]] - diff[diff_order[0]];
while (low < high) {
long long mid = (low + high) / 2;
long long min_sum = LLONG_MIN, max_sum = LLONG_MAX;
long long min_diff = LLONG_MIN, max_diff = LLONG_MAX;
long long sum_1 = LLONG_MIN, sum_2 = LLONG_MIN;
long long diff_1 = LLONG_MAX, diff_2 = LLONG_MAX;
for (int i = 0, j = 0; j < n; ++j) {
while (i < n && sum[sum_order[j]] - diff[diff_order[i]] > mid) {
long long s = sum[diff_order[i]], d = diff[diff_order[i]];
++i;
if (s > sum_1) {
sum_2 = sum_1, sum_1 = s;
} else if (s > sum_2) {
sum_2 = s;
}
if (d < diff_1) {
diff_2 = diff_1, diff_1 = d;
} else if (d < diff_2) {
diff_2 = d;
}
}
long long s = sum[sum_order[j]] == sum_1 ? sum_2 : sum_1;
long long d = diff[sum_order[j]] == diff_1 ? diff_2 : diff_1;
if (s > LLONG_MIN) {
min_sum = max(min_sum, -mid + c + sum[sum_order[j]] + s);
max_sum = min(max_sum, mid - c + diff[sum_order[j]] + d);
min_diff = max(min_diff, -mid + c + sum[sum_order[j]] - d);
max_diff = min(max_diff, mid - c + diff[sum_order[j]] - s);
}
}
if (n <= 3000) {
long long alt_min_sum = LLONG_MIN, alt_max_sum = LLONG_MAX;
long long alt_min_diff = LLONG_MIN, alt_max_diff = LLONG_MAX;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (sum[j] - diff[i] > mid) {
alt_min_sum = max(min_sum, -mid + c + sum[j] + sum[i]);
alt_max_sum = min(max_sum, mid - c + diff[j] + diff[i]);
alt_min_diff = max(min_diff, -mid + c + sum[j] - diff[i]);
alt_max_diff = min(max_diff, mid - c + diff[j] - sum[i]);
}
}
}
assert(
alt_min_sum <= min_sum &&
alt_max_sum >= max_sum &&
alt_min_diff <= min_diff &&
alt_max_diff >= max_diff
);
}
bool ok = false;
int l1 = n - 1, l2 = 0, r1 = n - 1, r2 = 0;
for (int i = 0; i < n; ++i) {
while (l1 >= 0 && x[i] + x[l1] >= min_sum) {
--l1;
}
while (r1 >= 0 && x[i] + x[r1] > max_sum) {
--r1;
}
while (l2 < n && x[i] - x[l2] > max_diff) {
++l2;
}
while (r2 < n && x[i] - x[r2] >= min_diff) {
++r2;
}
ok |= max(l1 + 1, l2) < min(r1 + 1, r2);
}
if (ok) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
332 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
332 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
332 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
332 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
332 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
332 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
332 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
332 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
304 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
332 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
1 ms |
332 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
1 ms |
332 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
332 KB |
n = 10, 7000000000 is a correct answer |
20 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
332 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
332 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
332 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
332 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
332 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
332 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
332 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
332 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
304 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
332 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
1 ms |
332 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
1 ms |
332 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
332 KB |
n = 10, 7000000000 is a correct answer |
20 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
332 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
332 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
332 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
332 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
332 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
332 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
332 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
332 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
304 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
332 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
1 ms |
332 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
1 ms |
332 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
332 KB |
n = 10, 7000000000 is a correct answer |
20 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
332 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
332 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
332 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
332 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
332 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
332 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
332 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
332 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
304 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
332 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
1 ms |
332 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
1 ms |
332 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
332 KB |
n = 10, 7000000000 is a correct answer |
20 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
332 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
332 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
332 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
332 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
332 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
332 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
332 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
332 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
304 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
332 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
1 ms |
332 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
1 ms |
332 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
332 KB |
n = 10, 7000000000 is a correct answer |
20 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
332 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
332 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
332 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
332 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
332 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
332 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
332 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
332 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
304 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
332 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
1 ms |
332 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
1 ms |
332 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
332 KB |
n = 10, 7000000000 is a correct answer |
20 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
332 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
332 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
332 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
332 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
332 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
332 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
332 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
332 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
304 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
332 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
1 ms |
332 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
1 ms |
332 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
332 KB |
n = 10, 7000000000 is a correct answer |
20 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
332 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
332 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
332 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
332 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
7 |
Correct |
1 ms |
332 KB |
n = 3, 29 is a correct answer |
8 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
9 |
Correct |
1 ms |
332 KB |
n = 2, 3 is a correct answer |
10 |
Correct |
1 ms |
332 KB |
n = 2, 2000000001 is a correct answer |
11 |
Correct |
1 ms |
332 KB |
n = 2, 3000000000 is a correct answer |
12 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
13 |
Correct |
1 ms |
332 KB |
n = 3, 3000000000 is a correct answer |
14 |
Correct |
1 ms |
332 KB |
n = 4, 3000000001 is a correct answer |
15 |
Correct |
1 ms |
304 KB |
n = 4, 4000000000 is a correct answer |
16 |
Correct |
1 ms |
332 KB |
n = 5, 4000000000 is a correct answer |
17 |
Correct |
1 ms |
332 KB |
n = 10, 1000000343 is a correct answer |
18 |
Correct |
1 ms |
332 KB |
n = 10, 3189 is a correct answer |
19 |
Correct |
1 ms |
332 KB |
n = 10, 7000000000 is a correct answer |
20 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
21 |
Halted |
0 ms |
0 KB |
- |