-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSPOJ21061.cc
55 lines (50 loc) · 1.12 KB
/
SPOJ21061.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPOJ 21061: Yet Another Subset Sum Problem
// http://www.spoj.com/problems/YASSP/
//
// Solution: dynamic programming
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
int x[50+10];
long long G[1000*50+10];
int F[1000*50+10];
void doit() {
int n; scanf("%d", &n);
int M = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &x[i]);
M += x[i];
}
memset(G, 0, sizeof(G));
G[0] = 1;
for (int i = 0; i < n; ++i)
for (int m = M-x[i]; m >= 0; --m)
G[m+x[i]] |= (G[m] << 1);
/*
// check
for (int m = 0; m <= M; ++m) {
int a = G[m];
printf("%d: ", m);
for (int i = 0; i <= n; ++i)
printf("%d", !!(a & (1<<i)));
printf("\n");
}
*/
int maxm = M;
memset(F, 0, sizeof(F));
for (int m = M; m >= 1; --m) {
for (int i = 0; i <= n; ++i)
F[m] += !!(G[m] & (1ull<<i));
if (F[m] >= F[maxm]) maxm = m;
}
printf("%d %d\n", F[maxm], maxm);
}
int main() {
int ncase; scanf("%d", &ncase);
for (int icase = 0; icase < ncase; ++icase) doit();
}