Josif_tepe

Untitled

Feb 18th, 2026
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. const long long MOD = 1e9 + 7;
  6. const int INF = 2e9;
  7. int n, k;
  8. string to_binary(int x) {
  9.     string res = "";
  10.     while(x > 0) {
  11.         res += (x % 2) + '0';
  12.         x /= 2;
  13.     }
  14.     while((int) res.length() < k) {
  15.         res += "0";
  16.     }
  17.     reverse(res.begin(), res.end());
  18.     return res;
  19. }
  20. int main() {
  21.     cin >> n >> k;
  22.    
  23.     vector<int> v(k);
  24.     for(int i = 0; i < k; i++) {
  25.         cin >> v[i];
  26.     }
  27.     int res = INF;
  28.     for(int bitmask = 0; bitmask < (1 << k); bitmask++) {
  29.         string s = to_binary(bitmask);
  30.        
  31.         int cnt = 0;
  32.         for(int i = 0; i < (int) s.length(); i++) {
  33.             if(s[i] == '1') {
  34.                 cnt++;
  35.             }
  36.         }
  37.        
  38.         if(cnt == n - 1) {
  39.             int sum = 0;
  40.             int max_sum = 0;
  41.             for(int i = 0; i < (int) s.length(); i++) {
  42.                 if(s[i] == '0') {
  43.                     sum += v[i];
  44.                 }
  45.                 else {
  46.                     sum = v[i];
  47.                 }
  48.                 max_sum = max(max_sum, sum);
  49.             }
  50.             res = min(res, max_sum);
  51.         }
  52.     }
  53.    
  54.     cout << res << endl;
  55.     return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment