You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
791 B

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 15;
int a[N], b[N], n;
int ans = INF;
/**
* @param step 第几步
* @param x 目前的酸度值
* @param y 目前的苦度值
*/
void dfs(int step, int x, int y) {
if (step == n) {
//清水不行~
if (x == 0 && y == 0) return;
//更新ans
ans = min(abs(x - y), ans);
return;
}
//选择
dfs(step + 1, (x == 0 ? 1 : x) * a[step + 1], y + b[step + 1]);
//放弃
dfs(step + 1, x, y);
}
int main() {
//n种配料
cin >> n;
//分别读入酸度和苦度
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
dfs(0, 0, 0);
//输出
printf("%d\n", ans);
return 0;
}