Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- int n, k, x, m, ind, cur;
- vector<pair<int,int>> negative, positive;
- scanf("%d%d",&n,&k);
- for (int i = 0; i < n; i++) {
- scanf("%d%d",&x,&m);
- if (x < 0) {
- negative.push_back({-x,m});
- } else {
- positive.push_back({x,m});
- }
- }
- long long cnt = 0;
- sort(negative.begin(),negative.end());
- ind = negative.size() - 1;
- cur = 0;
- while(ind >= 0) {
- if (cur == 0) {
- cur = k;
- cnt += floor(negative[ind].second / cur) * 2 * negative[ind].first;
- negative[ind].second %= cur;
- if (negative[ind].second == 0) ind--;
- if (ind == -1) break;
- cnt += 2 * negative[ind].first;
- }
- if (cur >= negative[ind].second) {
- cur -= negative[ind].second;
- negative[ind].second = 0;
- ind--;
- } else {
- negative[ind].second -= cur;
- cur = 0;
- }
- }
- sort(positive.begin(),positive.end());
- ind = positive.size() - 1;
- cur = 0;
- while(ind >= 0) {
- if (cur == 0) {
- cur = k;
- cnt += floor(positive[ind].second / cur) * 2 * positive[ind].first;
- positive[ind].second %= cur;
- if (positive[ind].second == 0) ind--;
- if (ind == -1) break;
- cnt += 2 * positive[ind].first;
- }
- if (cur >= positive[ind].second) {
- cur -= positive[ind].second;
- positive[ind].second = 0;
- ind--;
- } else {
- positive[ind].second -= cur;
- cur = 0;
- }
- }
- printf("%lld\n",cnt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement