fork download
  1. #include <set>
  2. #include <map>
  3. #include <list>
  4. #include <cmath>
  5. #include <queue>
  6. #include <stack>
  7. #include <cstdio>
  8. #include <string>
  9. #include <vector>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include <sstream>
  13. #include <iomanip>
  14. #include <complex>
  15. #include <iostream>
  16. #include <algorithm>
  17.  
  18. #include <ctime>
  19. #include <deque>
  20. #include <bitset>
  21. #include <cctype>
  22. #include <utility>
  23. #include <cassert>
  24. using namespace std;
  25.  
  26. #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
  27. #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
  28. #define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
  29. #define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
  30.  
  31. #define DEBUG(x) { cout << #x << " = " << x << endl; }
  32. #define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
  33. #define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
  34.  
  35. const int M = 100111;
  36. int x[M], p[M], m[M];
  37.  
  38. int main() {
  39. ios :: sync_with_stdio(false); cin.tie(NULL);
  40. cout << (fixed) << setprecision(6);
  41. int test; cin >> test;
  42. while (test--) {
  43. int n; cin >> n;
  44. FOR(i,1,n) cin >> x[i] >> p[i] >> m[i];
  45.  
  46. multiset< pair<int,int> > all;
  47. long long res = 0;
  48. FORD(i,n,1) {
  49. all.insert(make_pair(-p[i], m[i]));
  50.  
  51. while (x[i] && !all.empty()) {
  52. int price = -all.begin()->first;
  53. int amount = all.begin()->second;
  54. all.erase(all.begin());
  55.  
  56. int can = min(amount, x[i]);
  57.  
  58. res += can * (long long) price;
  59. x[i] -= can;
  60. amount -= can;
  61. if (amount > 0) all.insert(make_pair(-price, amount));
  62. }
  63. DEBUG(res);
  64. }
  65. cout << res << endl;
  66. }
  67. return 0;
  68. }
  69.  
  70.  
Success #stdin #stdout 0s 4608KB
stdin
1
6
4 4 2
2 9 3
2 6 3
2 5 9
2 2 2
2 3 3
stdout
res = 6
res = 11
res = 21
res = 33
res = 51
res = 76
76