Skip to content

Instantly share code, notes, and snippets.

@Vicfred
Last active August 29, 2015 13:55
Show Gist options
  • Save Vicfred/8726169 to your computer and use it in GitHub Desktop.
Save Vicfred/8726169 to your computer and use it in GitHub Desktop.
624 - CD
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=565
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <cmath>
#include <cassert>
#include <climits>
using namespace std;
#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define lowBit(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
#define modulo(S, N) ((S) & (N - 1))
#define isPowerOfTwo(S) (!(S & (S - 1)))
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S) ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S) ((S) | (S - 1))
#define s(T) scanf("%d", &T)
#define sl(T) scanf("%lld", &T);
#define fill(a, val) memset(a,val, sizeof(a))
#define all(x) x.begin(), x.end()
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define REP(i,n) FOR(i,0,n)
#define PB push_back
#define MP make_pair
#define mod 1000000007
typedef long long int ll;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> ii;
typedef vector<ii> vii;
int main(int argc, char *argv[]) {
int n, m, suma, max, tracks;
while(scanf("%d %d", &n, &m) != EOF) {
int cd[m+2];
for(int i = 0; i < m; i++) {
scanf("%d", &cd[i]);
}
max = 0;
for(int mask = 0; mask < (1 << m); mask++) {
suma = 0;
for(int j = 0; j < 21; j++) {
if(isOn(mask,j) > 0) {
suma += cd[j];
}
}
if(suma > max && suma <= n) {
max = suma;
tracks = mask;
}
}
for(int i = 0; i < 21; i++) {
if(isOn(tracks,i)) {
printf("%d ", cd[i]);
}
}
printf("sum:%d\n", max);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment