fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. int main() {
  9. int n;
  10. cin >> n;
  11.  
  12. vector<int> A(n);
  13. for (int i = 0; i < n; i++) {
  14. cin >> A[i];
  15. }
  16.  
  17. vector<long long> prefix_min(n, 0), prefix_max(n, 0);
  18.  
  19. prefix_min[0] = A[0];
  20. prefix_max[0] = A[0];
  21. for (int i = 1; i < n; i++) {
  22. prefix_min[i] = min(prefix_min[i - 1], (long long)A[i]);
  23. prefix_max[i] = max(prefix_max[i - 1], (long long)A[i]);
  24. }
  25.  
  26. long long result = 0;
  27. for (int i = 0; i < n; i++) {
  28. for (int j = i; j < n; j++) {
  29. long long f_lr = prefix_min[j] * prefix_max[j];
  30. result = (result + f_lr) % MOD;
  31. }
  32. }
  33.  
  34. cout << result << endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 5308KB
stdin
4
1 4 3 1
stdout
37