Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. int n, k, x, m, ind, cur;
  7. vector<pair<int,int>> negative, positive;
  8. scanf("%d%d",&n,&k);
  9. for (int i = 0; i < n; i++) {
  10. scanf("%d%d",&x,&m);
  11. if (x < 0) {
  12. negative.push_back({-x,m});
  13. } else {
  14. positive.push_back({x,m});
  15. }
  16. }
  17. long long cnt = 0;
  18. sort(negative.begin(),negative.end());
  19. ind = negative.size() - 1;
  20. cur = 0;
  21. while(ind >= 0) {
  22. if (cur == 0) {
  23. cur = k;
  24. cnt += floor(negative[ind].second / cur) * 2 * negative[ind].first;
  25. negative[ind].second %= cur;
  26. if (negative[ind].second == 0) ind--;
  27. if (ind == -1) break;
  28. cnt += 2 * negative[ind].first;
  29. }
  30. if (cur >= negative[ind].second) {
  31. cur -= negative[ind].second;
  32. negative[ind].second = 0;
  33. ind--;
  34. } else {
  35. negative[ind].second -= cur;
  36. cur = 0;
  37. }
  38. }
  39. sort(positive.begin(),positive.end());
  40. ind = positive.size() - 1;
  41. cur = 0;
  42. while(ind >= 0) {
  43. if (cur == 0) {
  44. cur = k;
  45. cnt += floor(positive[ind].second / cur) * 2 * positive[ind].first;
  46. positive[ind].second %= cur;
  47. if (positive[ind].second == 0) ind--;
  48. if (ind == -1) break;
  49. cnt += 2 * positive[ind].first;
  50. }
  51. if (cur >= positive[ind].second) {
  52. cur -= positive[ind].second;
  53. positive[ind].second = 0;
  54. ind--;
  55. } else {
  56. positive[ind].second -= cur;
  57. cur = 0;
  58. }
  59. }
  60. printf("%lld\n",cnt);
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement