2438: 【普及-】【P2036】PERKET

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:1 解决:1

题目描述

Perket 是一种流行的美食。为了做好 Perket,厨师必须谨慎选择食材,以在保持传统风味的同时尽可能获得最全面的味道。你有 lns="http://www.w3.org/1998/Math/MathML"> 种可支配的配料。对于每一种配料,我们知道它们各自的酸度 lns="http://www.w3.org/1998/Math/MathML"> 和苦度 lns="http://www.w3.org/1998/Math/MathML">。当我们添加配料时,总的酸度为每一种配料的酸度总乘积;总的苦度为每一种配料的苦度的总和。

众所周知,美食应该做到口感适中,所以我们希望选取配料,以使得酸度和苦度的绝对差最小。

另外,我们必须添加至少一种配料,因为没有任何食物以水为配料的。

输入

第一行一个整数 lns="http://www.w3.org/1998/Math/MathML">,表示可供选用的食材种类数。

接下来 lns="http://www.w3.org/1998/Math/MathML"> 行,每行 lns="http://www.w3.org/1998/Math/MathML">2 个整数 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML">,表示第 lns="http://www.w3.org/1998/Math/MathML"> 种食材的酸度和苦度。

输出

一行一个整数,表示可能的总酸度和总苦度的最小绝对差。

样例输入 复制

1
3 10

样例输出 复制

7

提示

数据规模与约定

对于 lns="http://www.w3.org/1998/Math/MathML">100% 的数据,有 lns="http://www.w3.org/1998/Math/MathML">110,且将所有可用食材全部使用产生的总酸度和总苦度小于 lns="http://www.w3.org/1998/Math/MathML">1×109,酸度和苦度不同时为 lns="http://www.w3.org/1998/Math/MathML">1 和 lns="http://www.w3.org/1998/Math/MathML">0

#include<bits/stdc++.h>
using namespace std;
int n,s[15],b[15];
int sums=1,sumb=0,ans=99999999; 
void search(){
	int U=1<<n;
	for(int S=1;S<U;S++) {
		sums=1,sumb=0;
		for(int i=0;i<n;i++) {
			if(S&(1<<i)) {
				sums*=s[i];
				sumb+=b[i];
			}
		}
		ans=min(ans,abs(sums-sumb));
	}
}

int main(){
    scanf("%d",&n);//输入
    for(int i = 0; i < n; i++)
        scanf("%d%d",&s[i],&b[i]);//循环读入
    search();//调用函数
    printf("%d\n",ans);//输出答案
    return 0;//结束程序
}