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
|
大专栏 Stickspan class="meta">#include <iostream> #include <algorithm> #include <cstring>
using namespace std;
int a[105]; int vis[105]; int n = 0; int len = 0; int cnt = 0; int ans;
bool (int s, int now, int last) { if(s > cnt) {return true;} if(now == len) {return dfs(s + 1, 0, 1);} int fail = 0; for(int i = last; i <= ans; i ++) { if(!vis[i] && now + a[i] <= len && fail != a[i]) { vis[i] = 1; if(dfs(s, now + a[i], i + 1)) {return true;} fail = a[i]; vis[i] = 0; if(now == 0 || now + a[i] == len) {return false;} } } return false; }
int main(int argc, char const *argv[]) { scanf("%d", &n); int tot = 0; int maxm = 0; for(int i = 1; i <= n; i ++) { int x; scanf("%d", &x); if(x <= 50) { a[++ans] = x; tot += a[ans]; maxm = max(maxm, a[ans]); } } sort(a + 1, a + ans + 1); reverse(a + 1, a + ans + 1); for(len = maxm; len <= tot; len ++) { if(tot % len) continue; cnt = tot / len; memset(vis, 0, sizeof(vis)); if(dfs(1, 0, 1)) {break;} } printf("%d", len); return 0; }
|