Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int n, a[15];
- vector<int>seen;
- int sum(int ind){
- int store = 1, f = 0;
- for(int i = ind+1; i < n; i++){
- if(seen[i] == 0){
- store = store * a[i];
- f = 1;
- break;
- }
- }
- for(int i = ind-1; i>=0; i--){
- if(seen[i] == 0){
- store = store * a[i];
- f=1;
- break;
- }
- }
- if(f==1){
- return store;
- }
- else{
- return a[ind];
- }
- }
- int Brust(int ind){
- if(ind == n){
- return 0;
- }
- int ans = INT_MIN; // This have to declare here
- for(int i = 0; i < n; i++){
- if(seen[i] == 0){
- seen[i] = 1;
- ans = max(ans, sum(i)+Brust(ind+1));
- seen[i] = 0;
- }
- }
- return ans;
- }
- int main(){
- cin >> n;
- seen.assign(n, 0);
- for(int i = 0; i < n; i++){
- cin >> a[i];
- }
- cout << Brust(0) << endl;
- return 0;
- }
- /*4
- 1 2 3 4
- 20*/
Advertisement
Add Comment
Please, Sign In to add comment