forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperket.cpp
63 lines (50 loc) · 1.12 KB
/
perket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
// Generates each combination
vector<bool> get_use(int i, int n) {
vector<bool> v;
v.resize(n, false);
int count = 0;
while(i > 0) {
if(i%2 == 1) {
v[count] = true;
}
count++;
i /= 2;
}
return v;
}
// Finds the bitterness and sourness
ll calculate(vector<pair<ll, ll>> v, vector<bool> b) {
ll sourness = 1;
ll bitterness = 0;
for(int i = 0; i < v.size(); i++) {
if(b[i]) {
sourness *= v[i].first;
bitterness += v[i].second;
}
}
return abs(bitterness - sourness);
}
int main() {
int n;
cin >> n;
// Read in data
vector<pair<ll, ll>> v;
for(int i = 0; i < n; i++) {
pair<ll, ll> p;
cin >> p.first >> p.second;
v.push_back(p);
}
// Find minimum diff
long long diff = 1000000001;
for(int i = 1; i < pow(2, n); i++) {
vector<bool> use = get_use(i, n);
diff = min(calculate(v, use), diff);
}
// Return
cout << diff << endl;
}