Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <bits/stdc++.h>
- using namespace std;
- const long long MOD = 1e9 + 7;
- const int INF = 2e9;
- int n, k;
- string to_binary(int x) {
- string res = "";
- while(x > 0) {
- res += (x % 2) + '0';
- x /= 2;
- }
- while((int) res.length() < k) {
- res += "0";
- }
- reverse(res.begin(), res.end());
- return res;
- }
- int main() {
- cin >> n >> k;
- vector<int> v(k);
- for(int i = 0; i < k; i++) {
- cin >> v[i];
- }
- int res = INF;
- for(int bitmask = 0; bitmask < (1 << k); bitmask++) {
- string s = to_binary(bitmask);
- int cnt = 0;
- for(int i = 0; i < (int) s.length(); i++) {
- if(s[i] == '1') {
- cnt++;
- }
- }
- if(cnt == n - 1) {
- int sum = 0;
- int max_sum = 0;
- for(int i = 0; i < (int) s.length(); i++) {
- if(s[i] == '0') {
- sum += v[i];
- }
- else {
- sum = v[i];
- }
- max_sum = max(max_sum, sum);
- }
- res = min(res, max_sum);
- }
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment