# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
158443 | 2019-10-17T05:47:51 Z | davitmarg | Boxes with souvenirs (IOI15_boxes) | C++17 | 0 ms | 0 KB |
/*DavitMarg*/ #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #include <map> #include <set> #include <queue> #include <iomanip> #include <bitset> #include <stack> #include <cassert> #include <iterator> #include <fstream> #define mod 1000000000ll #define LL long long #define LD long double #define MP make_pair #define PB push_back #define all(v) v.begin(), v.end() using namespace std; int n; LL k, m; deque<int> pos; LL ans; LL delivery(int N, int K, int L, vector<int> P) { n = N; k = K; m = L; for (int i = 0; i < N; i++) if (P[i]) pos.PB(P[i]); while (!pos.empty()) { if (pos.size() >= 2 * k) { if (pos[k - 1] < (m - pos[pos.size() - k])) { ans += 2ll * pos[k - 1]; for (int i = 0; i < k; i++) pos.pop_front(); } else { ans += 2ll * (m - pos[pos.size() - k]); for (int i = 0; i < k; i++) pos.pop_back(); } } else { LL add1 = 0, add2 = 0; add1 += min(2ll * pos[k - 1], m); if (k < pos.size()) add1 += min(2ll * (m - pos[k]), m); add2 += min(2ll * (m - pos[pos.size() - k]), m); if (pos.size() - k - 1 >= 0) add2 += min(2ll * pos[pos.size() - k - 1], m); //cout << add1 << " : " << add2 << endl; ans += min(add1, add2); break; } } return ans; } #ifdef death int main() { int N, K, L; vector<int> pos; cin >> N >> K >> L; for (int i = 0; i < N; i++) { pos.PB(0); cin >> pos.back(); } cout << delivery(N, K, L, pos) << endl; return 0; } #endif /* 3 2 8 1 2 5 */