_takumi

num26

Oct 18th, 2020 (edited)
248
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main() {
  8.     int s, n, cnt = 0, now = 0, max = 0, sum = 0;
  9.     ifstream inFile("C:\\Users\\Максим\\Downloads\\26-8.txt");
  10.     inFile >> s >> n;
  11.     vector<int> v;
  12.     while (inFile >> now) {
  13.         v.push_back(now);
  14.     }
  15.     sort(begin(v), end(v));
  16.     for (int i = 0; i < v.size(); i++) {
  17.         if (sum + v[i] <= s) {
  18.             sum += v[i];
  19.             max = v[i];
  20.             cnt++;
  21.         }
  22.         else {
  23.             if (sum - max + v[i] <= s) {
  24.                 sum -= max;
  25.                 sum += v[i];
  26.                 max = v[i];
  27.             }
  28.         }
  29.     }
  30.     cout << cnt << " " << max;
  31.     return 0;
  32. }
RAW Paste Data