# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
968599 | alex_2008 | Boxes with souvenirs (IOI15_boxes) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#include "boxes.h"
#include <iostream>
#include <vector>
#include <algorithm>
typedef long long ll;
using namespace std;
ll delivery(int n, int k, int l, vector <int> pos) {
vector <int> a, b;
vector <ll> dp_a, dp_b;
for (int i = 0; i < n; i++) {
if (pos[i] <= (l / 2)) a.push_back(pos[i]);
else b.push_back(pos[i]);
}
reverse(b.begin(), b.end());
for (auto &it : b) {
it = l - it;
}
dp_a.resize((int)a.size());
dp_b.resize((int)b.size());
for (int i = 0; i < (int)dp_a.size(); i++) {
if (i < k) {
dp_a[i] = 2 * a[i];
}
else {
dp_a[i] = dp_a[i - k] + 2 * a[i];
}
}
for (int i = 0; i < (int)dp_b.size(); i++) {
if (i < k) {
dp_b[i] = 2 * b[i];
}
else {
dp_b[i] = dp_b[i - k] + 2 * b[i];
}
}
int ind1 = 0, ind2 = 0;
ll ans = l * 1ll * ((n + k - 1) / k);
ll cur = 0;
for (int i = 1; i <= n; i++) {
if (ind1 == (int)dp_a.size()) {
if (ind2) cur -= dp_b[ind2 - 1];
cur += dp_b[ind2];
ind2++;
}
else if (ind2 == (int)dp_b.size()) {
if (ind1) cur -= dp_a[ind1 - 1];
cur += dp_a[ind1];
ind1++;
}
else {
ll diff1 = dp_a[ind1];
if (ind1) diff1 -= dp_a[ind1 - 1];
ll diff2 = dp_b[ind2];
if (ind2) diff2 -= dp_b[ind2 - 1];
if (diff1 <= diff2) {
cur += diff1;
ind1++;
}
else {
cur += diff2;
ind2++;
}
}
ans = min(ans, ((n - i + k - 1) / k * 1ll * l) + cur);
}
return ans;
}
int main() {
int n, k, l;
cin >> n >> k >> l;
vector <int> v(n);
for (auto &it : v) {
cin >> it;
}
cout << delivery(n, k, l, v) << "\n";
}