package work;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Sticks {
static int N, total;
static int sti[];
static int len;
static boolean[] isV;
public static void main(String args[]) throws FileNotFoundException {
/* Scanner sc=new Scanner(System.in); */
Scanner sc = new Scanner(new File("src/1011.txt"));
while (true) {
int total = 0;
N = sc.nextInt();
if (N == 0) {
break;
}
sti = new int[N];
for (int i = 0; i < N; i++) {
sti[i] = sc.nextInt();
total += sti[i];
}
if (N == 1) {
System.out.println(sti[0]);
}
if (N == 2) {
if (sti[0] == sti[1]) {
System.out.println(sti[0]);
}
if (sti[0] != sti[1]) {
System.out.println(sti[0] + sti[1]);
}
}
if (N >= 3) {
Qsort(0, N - 1, sti);
System.out.println(Fuc());
}
}
}
private static int Fuc() {
// TODO Auto-generated method stub
for (len = sti[0]; len < total; len++) {
if (total / len != 0) {
continue;
}
isV = new boolean[len];
if (dfs(0, 0, 0))
break;
}
return len;
}
private static boolean dfs(int n, int p, int num) {//每条边完成长度,当前边位置,完成边数
// TODO Auto-generated method stub
if(len*num==total)return true;
for(int i=p;i<N;i++){
if(isV[i]){continue;}
if(n+sti[i]==len){
isV[i]=true;
if(dfs(0,i+1,num++)){return true;}
else{isV[i]=false;return false;}
}
if(n+sti[i]<len){
isV[i]=true;
if(dfs(n+sti[i],i+1,num)){return true;}
else{isV[i]=false;return false;}
}
if(sti[i]==0){return false;}
}
return false;
}
private static void Qsort(int l, int r, int[] ss) {
// TODO Auto-generated method stub
if (l < r) {
int key = ss[r];
int i = l;
int j = r;
while (i < j) {
while (i < j && ss[i] > key)
i++;
ss[j] = ss[i];
while (i < j && ss[j] <= key)
j--;
ss[i] = ss[j];
}
ss[j] = key;
Qsort(l, j - 1, ss);
Qsort(j + 1, r, ss);
}
}
}
package work;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Sticks {
static int N, total;
static int sti[];
static int len;
static boolean[] isV;
public static void main(String args[]) throws FileNotFoundException {
/* Scanner sc=new Scanner(System.in); */
Scanner sc = new Scanner(new File("src/1011.txt"));
while (true) {
int total = 0;
N = sc.nextInt();
if (N == 0) {
break;
}
sti = new int[N];
for (int i = 0; i < N; i++) {
sti[i] = sc.nextInt();
total += sti[i];
}
if (N == 1) {
System.out.println(sti[0]);
}
if (N == 2) {
if (sti[0] == sti[1]) {
System.out.println(sti[0]);
}
if (sti[0] != sti[1]) {
System.out.println(sti[0] + sti[1]);
}
}
if (N >= 3) {
Qsort(0, N - 1, sti);
System.out.println(Fuc());
}
}
}
private static int Fuc() {
// TODO Auto-generated method stub
for (len = sti[0]; len < total; len++) {
if (total / len != 0) {
continue;
}
isV = new boolean[len];
if (dfs(0, 0, 0))
break;
}
return len;
}
private static boolean dfs(int n, int p, int num) {//每条边完成长度,当前边位置,完成边数
// TODO Auto-generated method stub
if(len*num==total)return true;
for(int i=p;i<N;i++){
if(isV[i]){continue;}
if(n+sti[i]==len){
isV[i]=true;
if(dfs(0,i+1,num++)){return true;}
else{isV[i]=false;return false;}
}
if(n+sti[i]<len){
isV[i]=true;
if(dfs(n+sti[i],i+1,num)){return true;}
else{isV[i]=false;return false;}
}
if(sti[i]==0){return false;}
}
return false;
}
private static void Qsort(int l, int r, int[] ss) {
// TODO Auto-generated method stub
if (l < r) {
int key = ss[r];
int i = l;
int j = r;
while (i < j) {
while (i < j && ss[i] > key)
i++;
ss[j] = ss[i];
while (i < j && ss[j] <= key)
j--;
ss[i] = ss[j];
}
ss[j] = key;
Qsort(l, j - 1, ss);
Qsort(j + 1, r, ss);
}
}
}
、、input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
2
1 1
2
1 3
0
、、output
4
1
4
、、output