fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define M 10005
  4.  
  5. struct mt
  6. {
  7. int id, soluong, gia;
  8. };
  9.  
  10. mt a[M];
  11. int m, n;
  12. long long s;
  13.  
  14. bool cmp(mt x, mt y)
  15. {
  16. return x.gia < y.gia;
  17. }
  18.  
  19. bool cmp1(mt x, mt y)
  20. {
  21. return x.id < y.id;
  22. }
  23.  
  24. int main()
  25. {
  26. cin >> m >> n;
  27. for(int i = 0; i < n; i++)
  28. {
  29. a[i].id = i + 1;
  30. cin >> a[i].soluong >> a[i].gia;
  31. }
  32. sort(a, a + n, cmp);
  33. for(int i = 0; i < n; i++)
  34. {
  35. if(m > 0)
  36. {
  37. if(a[i].soluong >= m)
  38. {
  39. a[i].soluong = m;
  40. m = 0;
  41. }
  42. else
  43. {
  44. m -= a[i].soluong;
  45. }
  46. s += a[i].gia * a[i].soluong;
  47. }
  48. else a[i].soluong = 0;
  49. }
  50. cout << s << '\n';
  51. sort(a, a + n, cmp1);
  52. for(int i = 0; i < n; i++)
  53. cout << a[i].soluong << '\n';
  54. }
  55.  
Success #stdin #stdout 0.01s 5280KB
stdin
22  5
3   30
5   10
6   8
10  5
2   20
stdout
168
0
5
6
10
1