Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- vector <int> a;
- vector <int> b;
- vector <int> c;
- string s;
- int getvec(int ind) {
- int i = ind;
- string num;
- for (;; i++) {
- if (s[i] == ']') {
- a.push_back(stoi(num));
- break;
- }
- else if (s[i] == ',') {
- a.push_back(stoi(num));
- num = "";
- }
- else if (s[i] >= '0' && s[i] <= '9')
- num.push_back(s[i]);
- }
- return i + 2;
- }
- int getvec1(int ind) {
- int i = ind;
- string num;
- for (;; i++) {
- if (s[i] == ']') {
- b.push_back(stoi(num) * -1);
- break;
- }
- else if (s[i] == ',') {
- b.push_back(stoi(num) * -1);
- num = "";
- }
- else if (s[i] >= '0' && s[i] <= '9')
- num.push_back(s[i]);
- }
- return i + 2;
- }
- int getvec2(int ind) {
- int i = ind;
- string num;
- for (;; i++) {
- if (s[i] == ']') {
- c.push_back(stoi(num));
- break;
- }
- else if (s[i] == ',') {
- c.push_back(stoi(num));
- num = "";
- }
- else if (s[i] >= '0' && s[i] <= '9')
- num.push_back(s[i]);
- }
- return i + 2;
- }
- int main() {
- //string s;
- getline(cin, s);
- int i1, i2, i3;
- i1 = getvec(1);
- i2 = getvec1(i1);
- i3 = getvec2(i2);
- for (int i = 0; i < b.size(); i++)
- a.push_back(b[i]);
- vector <bool> curr(4001, false);
- vector <bool> tmp;
- curr[2000] = true;
- tmp = curr;
- for (int i = 0; i < a.size(); i++) {
- for (int j = 0; j < 4001; j++)
- if (tmp[j] == true)
- curr[j + a[i]] = true;
- tmp = curr;
- }
- int ans = 0;
- int cc = 0;
- for (int i = 1; i <= (int)pow(2, c.size()) - 1; i++) {
- int sum = 0, cm = i, j = 0;
- int count = 0;
- while (cm > 0) {
- if (cm % 2 == 1) {
- sum += c[j];
- count++;
- }
- cm /= 2;
- j++;
- }
- if (curr[sum + 2000]) {
- //cout << sum << " "<< i<< endl;
- if (cc < count) {
- cc = count;
- ans = sum;
- }
- else if (cc == count)
- ans = min(ans, sum);
- }
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement