fork download
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. int main() {
  5. int N;
  6. scanf("%d", &N);
  7.  
  8. int a[N+1];
  9. for (int i = 1; i <= N; i++) {
  10. scanf("%d", &a[i]);
  11. }
  12.  
  13. int prefix_max[N+1];
  14. int suffix_min[N+1];
  15.  
  16. prefix_max[0] = INT_MIN;
  17. for (int i = 1; i <= N; i++) {
  18. prefix_max[i] = (a[i] > prefix_max[i-1]) ? a[i] : prefix_max[i-1];
  19. }
  20.  
  21. suffix_min[N+1] = INT_MAX;
  22. for (int i = N; i >= 1; i--) {
  23. suffix_min[i] = (a[i] < suffix_min[i+1]) ? a[i] : suffix_min[i+1];
  24. }
  25.  
  26. long long max_S = LLONG_MIN;
  27. for (int j = 2; j < N; j++) {
  28. long long S = 3 * a[j] + 2 * a[j-1] - suffix_min[j+1];
  29. if (S > max_S) {
  30. max_S = S;
  31. }
  32. }
  33.  
  34. printf("%lld\n", max_S);
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 5272KB
stdin
10
4 9 7 9 4 3 2 9 15 6
stdout
57