fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int N, A, B;
  9. cin >> N >> A >> B;
  10. struct F{ int P, C, X; };
  11. vector<F> a(N);
  12. for(int i = 0; i < N; i++)
  13. cin >> a[i].P >> a[i].C >> a[i].X;
  14.  
  15. // 1) PHẢI sort theo X tăng dần
  16. sort(a.begin(), a.end(), [](auto &l, auto &r){
  17. return l.X < r.X;
  18. });
  19.  
  20. // dp[t]: trạng thái gộp:
  21. // 0 ≤ t ≤ A : đã tiêu t mooney, cones chưa dùng => (j=t, k=0)
  22. // A < t ≤ A+B : đã xài hết mooney, tiêu k = t−A cones => (j=A, k=t−A)
  23. vector<int> dp(A+B+1, -1), ndp;
  24. dp[0] = 0;
  25.  
  26. for(auto &f : a){
  27. ndp = dp;
  28. int P = f.P, C = f.C, X = f.X;
  29. int maxConesForCow = C * X;
  30.  
  31. for(int t = 0; t <= A + B; t++){
  32. if (dp[t] < 0) continue;
  33.  
  34. // 1) không mời: đã được copy sang ndp
  35.  
  36. // 2) mời:
  37. if (t <= A) {
  38. // PHASE MOONEY: cones toàn bộ B vẫn có sẵn
  39. int useCones = min(B, maxConesForCow);
  40. int discount = useCones / X;
  41. int needM = C - discount;
  42. // needM >= 0, chắc chắn t+needM ≤ A + C ≤ A+B
  43. int nt = t + needM;
  44. ndp[nt] = max(ndp[nt], dp[t] + P);
  45. }
  46. else {
  47. // PHASE CONES: j=A, cones đã xài = t−A, còn lại rem = B-(t−A)
  48. int usedSoFar = t - A;
  49. int remCones = B - usedSoFar;
  50. // chỉ cho phép nếu có thể dùng đủ để discount toàn bộ
  51. if (remCones >= maxConesForCow) {
  52. // dùng đúng maxConesForCow cones ⇒ discount = C, needM = 0
  53. int nt = t + maxConesForCow;
  54. ndp[nt] = max(ndp[nt], dp[t] + P);
  55. }
  56. }
  57. }
  58. dp.swap(ndp);
  59. }
  60.  
  61. int ans = 0;
  62. for(int t = 0; t <= A + B; t++)
  63. ans = max(ans, dp[t]);
  64. cout << ans << "\n";
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0s 5320KB
stdin
3 10 8
5 5 4
6 7 3
10 6 3
stdout
21