fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. const int MAXN = 1e6;
  5. ll a[MAXN];
  6. int main(){
  7. ios::sync_with_stdio(false);
  8. cin.tie(nullptr);
  9. freopen("BONUS.INP" , "r" , stdin);
  10. freopen("BONUS.OUT" , "w" , stdout);
  11. ll n;
  12. cin >> n;
  13. for( int i = 1 ; i<= n ; i++ ) cin >> a[i];
  14. ll kq = 0;
  15. for(int i = 1; i <= n; i++) {
  16. ll mx = a[i];
  17. ll mn = a[i];
  18. for(int j = i; j <= n; j++) {
  19. mx = max(mx, a[j]);
  20. mn = min(mn, a[j]);
  21. kq += (mx - mn);
  22. }
  23. }
  24. cout << kq;
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0.01s 5280KB
stdin
3
1 2 3
stdout
4