# 【每日打卡】Day15：最佳加法表达式（大数运算）JAVA实现（部分超时）

### 输出

2
123456
1
123456
4
12345

### 样例输出

102
579
15

### 解析

基本思路是取排列，选择放置加号的位置，然后进行运算，这样计算可以通过一部分测试点，但是超时，等待后续优化!

import java.util.*;
import java.math.BigInteger;
public class Main {
static int n,m;
static int ans[] = new int[55];
static int vis[] = new int[55];
static int cc = 0;
static String mm = "100000000000000000000000000000000000000000000000000";
static BigInteger mv[] = new BigInteger[15];

static BigInteger mi(int p){
BigInteger res = new BigInteger("1");
for(int i = 0; i < p; i ++)
res = res.multiply(new BigInteger("10"));
return res;
}

static BigInteger re(int a[], int p, int q){
BigInteger ans = new BigInteger("0");
int s = q - p;
for(int i = 0; i <= s; i ++)
return ans;
}

static void dfs(int ix, int[] a, int n, int m, BigInteger mval[]){
if(m >= n){
mval[cc] = new BigInteger("0");
return;
}
if(ix == m){
if(ans[1] == 1 || ans[m] == n)
return ;
BigInteger sum = re(a,0,ans[1]-2);
for(int i = 1; i < m; i ++)
if(sum.compareTo(mval[cc]) < 0)
mval[cc] = sum;
return;
}
for(int i =ans[ix]+1; i < n+1; i ++){
if(vis[i] == 0){
vis[i] = 1;
ans[ix+1] = i;
dfs(ix+1,a,n,m,mval);
vis[i] = 0;
}
}
}

public static void main(String []args){
Scanner sc = new Scanner(System.in);

for(int i = 0; i < 15; i ++)
mv[i] = new BigInteger(mm);
while(cc < 15){

m = sc.nextInt();
String str = sc.next();
n = str.length();
int a[] = new int[n];
for(int i = 0; i < n; i ++)
a[i] = str.charAt(i) - '0';
dfs(0,a,n+1,m,mv);
System.out.println(mv[cc].toString());
cc++;
}
sc.close();
}

}