Advertisement
aquiem

Untitled

Apr 20th, 2024
485
0
19 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. ll mod = 1e9 + 7;
  6.  
  7. int main()
  8. {
  9. #ifndef ONLINE_JUDGE
  10.     freopen("input.txt", "r", stdin);
  11.     freopen("output.txt", "w", stdout);
  12. #endif
  13.     ios_base::sync_with_stdio(false);
  14.     cin.tie(NULL);
  15.  
  16.     ll t;
  17.     cin >> t;
  18.     while (t--)
  19.     {
  20.         ll n, m;
  21.         cin >> n >> m;
  22.  
  23.         map<ll, vector<ll>> coins;
  24.         for (int i = 0; i < m; i++)
  25.         {
  26.             ll d, v;
  27.             cin >> d >> v;
  28.             coins[d].push_back(v);
  29.         }
  30.  
  31.         ll effi = n;
  32.         ll ans = 0;
  33.         priority_queue<ll> currCoins;
  34.  
  35.         for (int i = 0; i < n; i++)
  36.         {
  37.             for (auto i : coins[i + 1])
  38.             {
  39.                 currCoins.push(i);
  40.             }
  41.  
  42.             if (!currCoins.empty())
  43.             {
  44.                 ll val = currCoins.top();
  45.                 currCoins.pop();
  46.                 ans += effi * val;
  47.             }
  48.  
  49.             effi--;
  50.         }
  51.  
  52.         cout << ans << "\n";
  53.     }
  54.     return 0;
  55. }
  56.  
  57. //--------------------------------------------------------------------------------------
  58.  
  59. #include <bits/stdc++.h>
  60. using namespace std;
  61. using ll = long long;
  62.  
  63. ll mod = 1e9 + 7;
  64.  
  65. int main()
  66. {
  67. #ifndef ONLINE_JUDGE
  68.     freopen("input.txt", "r", stdin);
  69.     freopen("output.txt", "w", stdout);
  70. #endif
  71.     ios_base::sync_with_stdio(false);
  72.     cin.tie(NULL);
  73.  
  74.     ll t;
  75.     cin >> t;
  76.     while (t--)
  77.     {
  78.         ll n, m;
  79.         cin >> n >> m;
  80.  
  81.         map<ll, vector<ll>> coins;
  82.         for (int i = 0; i < m; i++)
  83.         {
  84.             ll d, v;
  85.             cin >> d >> v;
  86.             coins[d].push_back(v);
  87.         }
  88.  
  89.         vector<pair<ll, vector<ll>>> coinsArray;
  90.         for (auto i : coins)
  91.         {
  92.             coinsArray.push_back(i);
  93.         }
  94.  
  95.         ll effi = n;
  96.         ll ans = 0;
  97.         priority_queue<ll> currCoins;
  98.         for (int i = 0; i < coinsArray.size(); i++)
  99.         {
  100.             for (auto j : coinsArray[i].second)
  101.             {
  102.                 currCoins.push(j);
  103.             }
  104.  
  105.             if (i != coinsArray.size() - 1)
  106.             {
  107.                 ll len = coinsArray[i + 1].first - coinsArray[i].first;
  108.                 ll reduceNum = 0;
  109.                 while (len && !currCoins.empty())
  110.                 {
  111.                     ll curr = currCoins.top();
  112.                     currCoins.pop();
  113.  
  114.                     ans += effi * curr;
  115.                     effi--;
  116.                     reduceNum++;
  117.                     len--;
  118.                 }
  119.  
  120.                 effi -= coinsArray[i + 1].first - coinsArray[i].first - reduceNum;
  121.             }
  122.         }
  123.  
  124.         ll leftLen = n - coinsArray[coinsArray.size() - 1].first + 1;
  125.         while (leftLen && !currCoins.empty())
  126.         {
  127.             ll curr = currCoins.top();
  128.             currCoins.pop();
  129.  
  130.             ans += curr * effi;
  131.             effi--;
  132.             leftLen--;
  133.         }
  134.  
  135.         cout << ans << '\n';
  136.     }
  137.     return 0;
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement