fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<ll, ll> PLL;
  9.  
  10. vector<PLL> liczniki;
  11. int n, m;
  12.  
  13.  
  14. bool comp(PLL a, PLL b) {
  15.  
  16. if(a.first > b.first) {
  17. return true;
  18. }
  19. if(a.first == b. first) {
  20. if (a.second > b.second) {
  21. return true;
  22. }
  23. return false;
  24. }
  25. return false;
  26. }
  27.  
  28. int main() {
  29. ios_base::sync_with_stdio(0);
  30. cin.tie(0);
  31. cout.tie(0);
  32.  
  33. multiset<ll> akt_m;
  34. vector<ll> ost_m;
  35.  
  36. cin >> n >> m;
  37. for(int i = 0; i < n; i++) {
  38. int c;
  39. cin >> c;
  40. liczniki.push_back(make_pair(c, 0LL));
  41. }
  42. for(int i = 0; i < n; i++) {
  43. cin >> liczniki[i].second;
  44. }
  45.  
  46. sort(liczniki.begin(), liczniki.end(), comp);
  47.  
  48. /*cout << endl;
  49.   for(int i = 0; i < n; i++) {
  50.   cout << liczniki[i].first << " ";
  51.   }
  52.   cout<<endl;
  53.   for(int i = 0; i < n; i++) {
  54.   cout << liczniki[i].second << " ";
  55.   }
  56.   cout<<endl;*/
  57.  
  58.  
  59. for(int i = 0; i < n; i++) {
  60. ost_m.push_back(liczniki[i].second);
  61. }
  62.  
  63. for(int i = 0; i < m; i++) {
  64.  
  65. for(int j = 0; j < n; j++) {
  66. ll a;
  67. cin >> a;
  68. akt_m.insert(a);
  69. }
  70.  
  71. for(int j = 0; j < n; j++) {
  72. auto it = akt_m.lower_bound(ost_m[j]);
  73. if(it == akt_m.end()) {
  74. cout << "NIE";
  75. return 0;
  76. }
  77. ost_m[j] = *it;
  78. akt_m.erase(it);
  79. }
  80. /*for(int j = 0; j < n; j++) {
  81.   cout << ost_m[j] << " ";
  82.   }
  83.   cout << endl;*/
  84. }
  85.  
  86. ll wynik = 0LL;
  87. for(int i = 0; i < n; i++) {
  88. wynik += (ost_m[i] - liczniki[i].second) * liczniki[i].first;
  89. }
  90. cout << wynik;
  91.  
  92.  
  93.  
  94. return 0;
  95. }
  96.  
Success #stdin #stdout 0.01s 5304KB
stdin
4 2
3 1 4 3
3 2 4 7
5 10 3 7
4 6 10 9
stdout
25