fork download
  1. // code by Deepak Verma
  2. #include <bits/stdc++.h>
  3. #define endl '\n'
  4. #define fast cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0)
  5. using namespace std;
  6. const int mod = 1000*1000*1000+7 ;
  7. #define rep(i,l,r) for(int i=l;i<r;i++)
  8. #define repi(i,l,r) for(int i=l;i<=r;i++)
  9. typedef long long ll ;
  10. typedef unsigned long long ull ;
  11. typedef pair<int,int> ii ;
  12. typedef vector<int> vi ;
  13. typedef vector<char> vc ;
  14. typedef vector<vi> vvi ;
  15. typedef vector<ii> vii ;
  16. typedef vector<char> vc ;
  17. typedef set<int> si ;
  18. typedef set<ll> sl ;
  19. typedef map<int,int> mii;
  20. typedef map<char,int> mci;
  21.  
  22. struct newd {
  23. ll di,fi,bi ;
  24. int num ;
  25. };
  26.  
  27. void solve1() {
  28. ll n,m ;
  29. cin >> n >> m ;
  30. int arrm[m+1] {0} ;
  31. repi(i,1,m) cin >> arrm[i] ; // for m flavours
  32. newd itm ;
  33. vector<newd> vt ;
  34. ll tot = 0 ;
  35. int ice[n+1] = {0} ; // for n persons // the flavour number
  36.  
  37. // for(auto it:arrm) cout << it << ' ' ;
  38. // cout << endl ;
  39. rep(i,0,n) {
  40. cin >> itm.di >> itm.fi >> itm.bi ;
  41. itm.num = i+1 ;
  42.  
  43. if(arrm[itm.di]>0) {
  44. tot += itm.fi ;
  45. arrm[itm.di]-- ;
  46. ice[i+1] = itm.di ;
  47. }
  48. else {
  49. vt.push_back(itm) ;
  50. }
  51. }
  52. // for(auto it:arrm) cout << it << ' ' ;
  53. // cout << endl ;
  54.  
  55. vi lefts ;
  56. if(!vt.empty()) {
  57. for(auto it:vt) {
  58. tot += it.bi ;
  59. }
  60. int i = 1 ;
  61. int j = 0 ;
  62. int size = vt.size() ;
  63. while(j<size) {
  64. ll lf = arrm[i] ;
  65. if(lf>0) {
  66. ll pos = vt[j].num ;
  67. ll whi = i ;
  68. ice[pos] = whi ;
  69. j++ ;
  70. }
  71. else {
  72. i++ ;
  73. }
  74.  
  75. }
  76. }
  77. cout << tot << endl ;
  78. repi(i,1,n) cout << ice[i] << ' ' ;
  79. cout << endl ;
  80. }
  81.  
  82. int main() {
  83. fast;
  84. #ifndef ONLINE_JUDGE
  85. freopen("input.txt", "r", stdin);
  86. freopen("output.txt", "w", stdout);
  87. #endif
  88.  
  89. ll t ;
  90. cin >> t ;
  91. while(t--) {
  92. solve1() ;
  93. }
  94.  
  95. }
  96.  
Success #stdin #stdout 0s 4548KB
stdin
1
5 3
1 2 3
2 6 3
2 10 7
2 50 3
1 10 5
1 7 4
stdout
33
2 2 3 1 3