【洛谷】P1120 小木棍 [数据加强版](题解)

P1120 小木棍 [数据加强版]


题解:

#include <cstdio>
#include <cstdlib>
#include <iostream>
int n, cnt, tot, maxn, minn, tm[70];
void dfs(int res,int sum,int target,int p)
{
    if(res == 0) {
        printf("%d",target);
        exit(0);
    }
    if(sum == target) {
        dfs(res-1,0,target,maxn);
        return;
    }
    for(int i = p; i >= minn; i--) {
        if(tm[i]&&i+sum<=target) {
            tm[i]--;
            dfs(res,sum+i,target,i);
            tm[i]++;
            if(sum==0||sum+i==target)
            {
                break;
            }
        }
    }
    return;
}
int main()
{
    scanf("%d",&n);
    minn = 70;
    int temp;
    while(n--) {
        scanf("%d",&temp);
        if(temp<=50) {
            cnt++;
            tm[temp]++;
            tot+= temp;
            maxn=maxn>temp?maxn:temp;
            minn=minn<temp?minn:temp;
        }
    }
    temp=tot*2;
    for(int i=maxn;i<=temp;i++) {
        if(tot%i==0) {
            dfs(tot/i,0,i,maxn);
        }
    }
    printf("%d",tot);
    return 0;
}

 

posted @ 2020-02-18 17:41  BorisDimitri  阅读(35)  评论(0编辑  收藏  举报