FLY backpack

Title Description

FLY travel again, this time he put the backpack k only, the same, there are n items he wants to take out the weight of each item as a [i] (and can not be separated). Since FLY is a mild End (qiang) United States (po) main (zheng) justice (huan) by (zhe) [false], he hopes the weight of each backpack will be able to equal. So you need to help each FLY calculate the maximum weight of the backpack can bring.

· (In fact, this sentence is only useful: he wants to backpack weight of each are equal so you need to be able to help each FLY calculate the maximum weight of the backpack can bring other words useless, qwq..)

Entry

The first two row number n, k, representative of the number of items and the number of the backpack.
The second row of n numbers represent the weight of each item.
n <= 10, each not greater than the weight of the backpack 20. The scope of the DFS hehehe qwq]

Export

A number, i.e., each dispensing bags are obtained the maximum weight.

Sample input

10 4
3 5 3 7 4 3 5 2 8 1

Sample Output

10

prompt

Sample Explanation:
10. 4
. 3. 5. 4. 3. 7. 8 2. 3. 1. 5

10 minutes 4 backpack items, as each of the weight of the backpack, may be assigned a maximum weight of 10
backpack 1: 37
backpack 2: 334
backpack 3: 55
backpack 4: 28

Thinking

To see to know is the search, not as a backpack Oh, this question because he said he hoped each backpack should be equal, not equal if one of them can not be elected. This will dry backpack. . .

Search can actually be a positive solution is search. Stop to enumerate, each time, then either selected or not selected.

Option can also be divided into a number of situations: not currently selected, and although the election, but still want to use that kind of backpack. After the operation also marks a number. Then lose weight of the backpack. In the continuing search.

Given the key code:

if(fg == 0) {
        if(t == 1) {
            flag = 1;
            return;
        }
        dfs(1 , gu , t-1);
        return;
    }

Without election: When no markers have to do, but also to put this capacity to lose. Continue the search.

Given the key code:

if(!f[i]) {
        f[i] = 1;
        dfs(i+1,fg-a[i],t);
        f[i] = 0;
    }

Then you can continue the search.

dfs(i+1,fg,t);

Search key components:

gu as an answer, fg is the weight of the backpack, t is the number.

void dfs(int i,int fg,int t) {
    if(fg < 0 || flag || i>n+1)return;
    if(fg == 0) {
        if(t == 1) {
            flag = 1;
            return;
        }
        dfs(1 , gu , t-1);
        return;
    }
    if(!f[i]) {
        f[i] = 1;
        dfs(i+1,fg-a[i],t);
        f[i] = 0;
    }
    dfs(i+1,fg,t);
}

Tip: Search is the only style of play.

Guess you like

Origin www.cnblogs.com/wangshengjun/p/11220635.html