P2080 Enhance feelings

topic background

The relationship between Xiaoming and Xiaohong developed slowly.

Topic description

They each have a favorability score for each other. The intimacy of two people is defined as the sum of their favorability values.

If their intimacy reaches V, they will come together. Their future life will depend on the absolute value of the difference between the two's favorability value, the smaller the value, the happier their life will be.

Now, their favorability value for each other is 0, Xiaoming has N things to do, each of which can increase his favorability towards Xiaohong Ai points, and increase Xiaohong favorability Bi points for him. (may be negative)

Xiao Ming can choose some things to do, please help Xiao Ming find out how to make their lives happier (you can find the minimum absolute value of the difference between the two people's favorability values ​​under the premise of being together).

Input and output format

Input format:

 

Line 1, two positive integers N, V.

After N lines, each line is an integer Ai, Bi separated by two spaces.

 

Output format:

 

One line, a non-negative integer, represents the minimum absolute value of the difference between the two people's favorability values ​​under the premise that the two are together. If the two cannot be together anyway, output -1.

 

Input and output example

Input Example #1:  Copy
4 15
5 6
-1 8
7 2
1 0
Output Sample #1:  Copy
3

illustrate

For 20% of the data, N<=10.

For all data, N<=30, |Ai|, |Bi|<=100. Data is weak

 

 

#include<bits/stdc++.h>

using namespace std;

int n,v,a[66],b[66],ans=1010101;
bool vis [66], k;
void dfs(int d,int x,int y){
    if(k==1) return;
    if(d>n+1) return;
    if(x+y>=v){
        int q=abs(x-y);
        if(ans>q) ans=q;
        if(ans==0){
            k=1;
            return;
        }
    }
    dfs(d+1,x+a[d],y+b[d]);
    dfs(d+1,x,y);
}
intmain()
{
    ios::sync_with_stdio(0);
    cin>>n>>v;
    for(int i=1;i<=n;i++)
        cin>>a[i]>>b[i];
    dfs(1,0,0);
    if(ans==1010101) cout<<-1;
    else cout<<years;
    return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324912211&siteId=291194637