-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16198.cpp
52 lines (45 loc) · 1.09 KB
/
16198.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
//
// Created by LG on 2021-10-09.
//
#include <iostream>
#include <vector>
using namespace std;
int n;
int sum, ans=0;
void energy(vector<int> weight, vector<bool> &check, int num){
if(num==n-2){
ans = max(ans, sum);
return;
}
for(int i=1; i<n-1; i++){
if(!check[i]) {
check[i] = true;
int left, right;
for(int j=i-1; j>=0; j--){
if(!check[j]) {
left = j;
break;
}
}
for(int j=i+1; j<n; j++){
if(!check[j]) {
right = j;
break;
}
}
sum += weight[left] * weight[right];
energy(weight, check, num+1);
check[i] = false;
sum -= weight[left] * weight[right];
}
}
}
int main(){
cin >> n;
vector<int> weight(n);
vector<bool> check(n, false);
for(int i=0; i<n; i++)
cin >> weight[i];
energy(weight, check, 0);
cout << ans;
}