-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
43 lines (35 loc) · 1.1 KB
/
Copy pathtest.cpp
File metadata and controls
43 lines (35 loc) · 1.1 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N), b(N);
for (int i = 0; i < N; ++i) cin >> a[i];
for (int i = 0; i < N; ++i) cin >> b[i];
// Step 1: Calculate the valid ranges and store their midpoints
vector<int> possible_values;
for (int i = 0; i < N; ++i) {
possible_values.push_back(a[i]);
possible_values.push_back(a[i] + b[i]);
}
// Step 2: Sort to find the median
sort(possible_values.begin(), possible_values.end());
int median = possible_values[possible_values.size() / 2];
// Step 3: Clamp each value to be as close as possible to the median
vector<int> x(N);
for (int i = 0; i < N; ++i) {
x[i] = max(a[i], min(a[i] + b[i], median));
}
// Step 4: Calculate the minimized value of S
long long sum_x = 0, sum_x2 = 0;
for (int i = 0; i < N; ++i) {
sum_x += x[i];
sum_x2 += 1LL * x[i] * x[i];
}
long long result = 2LL * N * sum_x2 - 2LL * sum_x * sum_x;
cout << result << endl;
return 0;
}