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 64 65 66 67 68 69 70 71
|
#include <stdio.h> #include <iostream> #include <vector>
using namespace std; vector<int> res;
void dfs(vector<int> v, int sum, int idx, int plus, int minus, int multi, int div){ if(idx == v.size()){ res.push_back(sum); return; } if(plus > 0){ dfs(v,sum+v[idx],idx+1, plus-1,minus,multi,div); } if(minus > 0){ dfs(v,sum-v[idx],idx+1, plus,minus-1,multi,div); } if(multi > 0){ dfs(v,sum*v[idx],idx+1, plus,minus,multi-1,div); } if(div > 0){ dfs(v,sum/v[idx],idx+1, plus,minus,multi,div-1); } } int main(void){ int n; cin >> n; vector<int> v(n); int plus=0; int minus=0; int multi=0; int div=0; for(int i=0; i<n; i++){ cin >> v[i]; } for(int i=0; i<4; i++){ int cal=0; cin >> cal; if(i == 0){ plus = cal; }else if(i == 1){ minus = cal; }else if(i == 2){ multi = cal; }else { div = cal; } } dfs(v, v[0],1,plus, minus, multi, div); auto p = minmax_element(res.begin(), res.end()); cout << *p.second << " " << *p.first; return 0; }
|