DFS组合数学向问题
https://ac.nowcoder.com/acm/contest/11220/C
#include<bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define x first
#define y second
const int inf = 0x3f3f3f3f;
// #define int long long
const int N = 10;
int a[N];
double ans=-1;
int n;
double calc(int x, int y, int z){
if(x+y<=z || y+z<=x || z+x<=y) return -1;
double p = 0.5*(x+y+z);
double ans = sqrt(p*(p-x)*(p-y)*(p-z));
return ans;
}
void dfs(int u, int x, int y, int z){
if(u==n) {
ans = max(ans, calc(x,y,z));
return ;
}
dfs(u+1, x+a[u], y, z);
dfs(u+1, x, y+a[u], z);
dfs(u+1, x, y, z+a[u]);
dfs(u+1, x, y, z);
}
void sol() {
cin >> n;
for (int i=0; i<n; i++) cin >> a[i];
dfs(0,0,0,0);
if(ans<0) {
cout << -1;
return;
}
printf("%.1lf", ans);
}
int main(){
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
int T_T=1;
// cin >> T_T;
while (T_T--) sol();
return 0;
}