Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cmath>
- using namespace std;
- void next(int a[], int n){
- int position = n;
- while(position >=1 && a[position] == 1) position--;
- a[position] = 1;
- for(int i = position + 1; i <= n; i++) a[i]=0;
- }
- main(){
- int n, s[100005], b[100005], s1[100005]={0}, s2[100005]={0};
- int xmin;
- cin>>n;
- int mu = pow(2,n);
- for(int i = 1; i <= n; i++){
- cin>>s[i]>>b[i];
- }
- xmin = abs(s[1]-b[1]);
- for(int i = 1; i < mu; i++){
- next(s1, n);
- next(s2, n);
- long long mul = 1, sum = 0;
- for(int i = 1; i <= n; i++){
- if(s1[i] == 1){
- mul *= s[i];
- sum += b[i];
- }
- int x;
- if(mul > sum) x = mul - sum;
- else x = sum - mul;
- if(x < xmin) xmin = x;
- }
- }
- cout<<xmin;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement