fork(1) download
  1. #include <cstdio>
  2.  
  3. #include <algorithm>
  4.  
  5. constexpr int INT_MIN() {
  6. return (1 << ((sizeof(int) << 3) - 1));
  7. }
  8.  
  9. inline int accumulate(int *current, int next) {
  10. int ret = (*current) * next;
  11. if (next == 0) {
  12. *current = 1;
  13. } else {
  14. *current = ret;
  15. }
  16. return ret;
  17. }
  18.  
  19. template<size_t N_>
  20. int calculate(int (&a)[N_]) {
  21. int m = INT_MIN(), litr = 1, ritr = 1;
  22. for (size_t i = 0; i < N_; ++i) {
  23. m = std::max({
  24. m, accumulate(&litr, a[i]), accumulate(&ritr, a[N_ - i - 1])
  25. });
  26. }
  27. return m;
  28. }
  29.  
  30. int main() {
  31. int a[] = {
  32. 2, -7, 0, 2, 3, 8, -6, 5
  33. };
  34. printf("%d\n", calculate(a));
  35. int b[] = {
  36. -2, 0, 3, 5, -7
  37. };
  38. printf("%d\n", calculate(b));
  39. int c[] = {
  40. -2, 10, 10, -2
  41. };
  42. printf("%d\n", calculate(c));
  43. return 0;
  44. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
48
15
400