fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int n; cin>>n;
  6. // assuming array's length is at least 7
  7. vector<int> a(n); for(int &x:a) cin>>x;
  8. vector<pair<int,int>> P[2];
  9. P[0].resize(n,{0,INT_MAX}); // products on the left
  10. P[1].resize(n,{0,INT_MAX}); // products on the right
  11. // we'll start filling from P[2] because
  12. // first two elements of the array cannot be multiplied.
  13. int mx = a[0],mn = a[0]; // max_element(0,0) and min_element(0,0)
  14. for(int i=2;i<n;i++) {
  15. P[0][i] = P[0][i-1];
  16. P[0][i].first = max(P[0][i].first,a[i]*mx);
  17. P[0][i].second = min(P[0][i].second,a[i]*mn);
  18. mx = max(mx,a[i-1]); mn = min(mn,a[i-1]);
  19. }
  20. // we'll start from P[n-3] because
  21. // last two elements of the arrays cannot be multiplied.
  22. mx = a[n-1],mn = a[n-1]; // max_element(n-1,n-1) and min_element(n-1,n-1)
  23. for(int i=n-3;i>=0;i--){
  24. P[1][i] = P[1][i+1];
  25. P[1][i].first = max(P[1][i].first,a[i]*mx);
  26. P[1][i].second = min(P[1][i].second,a[i]*mn);
  27. mx = max(mx,a[i+1]); mn = min(mn,a[i+1]);
  28. }
  29. int ans = 0;
  30. for(int i=2;i<n-3;i++){
  31. ans = max(ans,max(P[0][i].first-P[0][i+1].second,P[1][i+1].first-P[1][i].second));
  32. }
  33. cout<<ans;
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5348KB
stdin
9
2 1 3 4 8 6 7 9 2
stdout
66