# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
831443 | qin | Boxes with souvenirs (IOI15_boxes) | C++11 | 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct st{
ll w; int i;
st(){}
st(ll W, int I) : w(W), i(I) {}
};
int main(){
int n, k, l; scanf("%d%d%d", &n, &k, &l);
vector<int> t(n), d1(n+1), d2(n+1);
for(int i = 0; i < n; ++i) scanf("%d", &t[i]);
d1[0] = t[0], d2[n] = l-t[n-1];
for(int i = 1; i < n; ++i) d1[i] = t[i]-t[i-1];
for(int i = n-1; i; --i) d2[i] = d1[i] + d2[i+1];
for(int i = 1; i < n; ++i) d1[i] += d1[i-1];
for(int i = 0; i < n; ++i) d2[i] = d2[i+1];
vector<ll> dp1(n), dp2(n);
deque<st> dq; dq.emplace_back(0, -1);
for(int i = 0; i < n; ++i){
if(!dq.empty() && i-dq.front().i > k) dq.pop_front();
dp1[i] = dq.front().w+ll(d1[i]<<1);
while(!dq.empty() && dq.back().w >= dp1[i]) dq.pop_back();
dq.emplace_back(dp1[i], i);
} while(!dq.empty()) dq.pop_back();
dq.emplace_back(0, n);
for(int i = n-1; ~i; --i){
if(!dq.empty() && dq.front().i-i > k) dq.pop_front();
dp2[i] = dq.front().w+ll(d2[i]<<1);
while(!dq.empty() && dq.back().w >= dp2[i]) dq.pop_back();
dq.emplace_back(dp2[i], i);
} ll wynik = min(dp1[n-1], dp2[0]);
for(int i = 0; i < n-1; ++i) wynik = min(wynik, dp1[i]+dp2[i+1]);
printf("%lld\n", wynik);
return 0;
}