Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Treat each neighbor relation as directed: person gets a separate contribution from each visible person . The pairwise mess is mostly fake.
Let if person gets a gift and otherwise. For one visible ordered pair , the contribution is .
Now sum that over all visible ordered pairs. Since visibility is symmetric, every collects one coefficient instead of staying tangled with other choices.
That coefficient is . Once you know , person should get a gift exactly when .
The maximum is . Compute each circular field-of-view sum with a duplicated/tripled prefix sum in per test case.
The trap is thinking this is a complicated subset problem on a circular graph. It looks like the choices should interact. They do not. The algebra nukes the interaction completely.
Let
For one ordered visible relation , person 's contribution caused by person is:
Check it directly:
So the total happiness is
where is the field of view of person .
Split the sum:
The visibility relation is symmetric: if is in 's field of view, then is in 's field of view. Therefore, in the second double sum, we can collect terms by instead:
Define
Then
This is the whole problem. There is no DP, no sorting, no spooky global constraint. Each is independent now. To maximize , choose:
Thus the answer is
This is always an integer. Every visible unordered pair contributes opposite differences into the coefficient sum, so ; the positive and negative parts balance.
Now we only need to compute every field-of-view sum quickly.
Use 1-indexing and build prefix sums over three copies of the circular array. For original person , place them at position
in the middle copy. Then their counter-clockwise side is
and their clockwise side is
So:
Then compute and add .
The condition guarantees the clockwise and counter-clockwise people are distinct, so this matches the statement cleanly.
Complexity per test case is time and memory. Use long long, because values can reach about , which is way past int.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n, d;
cin >> n >> d;
vector<ll> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vector<ll> pref(3 * n + 1, 0);
for (int i = 1; i <= 3 * n; i++) {
pref[i] = pref[i - 1] + a[(i - 1) % n + 1];
}
ll sumAbs = 0;
for (int i = 1; i <= n; i++) {
int p = n + i;
ll left = pref[p - 1] - pref[p - d - 1];
ll right = pref[p + d] - pref[p];
ll c = 2LL * d * a[i] - left - right;
sumAbs += c >= 0 ? c : -c;
}
cout << sumAbs / 2 << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n, d;
cin >> n >> d;
vector<ll> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vector<ll> pref(3 * n + 1, 0);
for (int i = 1; i <= 3 * n; i++) {
pref[i] = pref[i - 1] + a[(i - 1) % n + 1];
}
ll sumAbs = 0;
for (int i = 1; i <= n; i++) {
int p = n + i;
ll left = pref[p - 1] - pref[p - d - 1];
ll right = pref[p + d] - pref[p];
ll c = 2LL * d * a[i] - left - right;
sumAbs += c >= 0 ? c : -c;
}
cout << sumAbs / 2 << '\n';
}
return 0;
}