fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define fi first
  5. #define se second
  6. #define MOD 1000000007
  7. #define FOR(i,a,b) for (int i = (a);i <= (b);i++)
  8. #define FOD(i,a,b) for (int i = (b);i >= (a);i--)
  9. #define ALL(x) (x).begin(),(x).end()
  10. #define ii pair<ll,ll>
  11. #define iii pair<int,pair<int,int>>
  12. //const int MOD = 998244353;
  13. const int MAXN = 1e5 + 7;
  14. const ll oo = 9e18;
  15. int a[MAXN],n;
  16. vector<ll> v;
  17. bool f[19];
  18. ll ans = -oo;
  19. void check(int id,ll cur){
  20. if (!id){
  21. ans = max(ans,cur);
  22. return ;
  23. }
  24. FOR(i,1,9)if (!f[i]){
  25. f[i] = true;
  26. FOR(j,1,9)if (!f[j]){
  27. f[j] = true;
  28. FOR(k,1,9)if (!f[k]){
  29. f[k] = true;
  30. check(id - 1,cur + v[i] * v[j] * v[k]);
  31. f[k] = false;
  32. }
  33. f[j] = false;
  34. }
  35. f[i] = false;
  36. }
  37. }
  38. void sub1(int mask = n){
  39. int cnt = 0;
  40. FOR(i,1,(1<<mask) - 1)
  41. if (__builtin_popcount(i) == 9){
  42. cnt++;
  43. v.clear();v.push_back(0);
  44. FOR(j,0,mask - 1)if (i >> j & 1)
  45. v.push_back(a[j + 1]);
  46. check(3,0);
  47. }
  48. cout << ans;
  49. }
  50. void sub2(){
  51.  
  52. }
  53. int main(){
  54. ios_base::sync_with_stdio(false);
  55. cin.tie(0); cout.tie(0);
  56. cin >> n;
  57. FOR(i,1,n)cin >> a[i];
  58. if (n <= 18)sub1();
  59. else sub2();
  60. return 0^0;
  61. }
  62.  
Success #stdin #stdout 0.39s 5276KB
stdin
11
-5 -5 -4 -6 -2 -2 0 3 5 -3 -6 
stdout
255