fork download
  1. #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
  2. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx,mmx,tune=native")
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #define pb push_back
  6. #define ll long long
  7. signed main() {
  8. ios_base::sync_with_stdio(0);
  9. cin.tie(0);
  10. cout.tie(0);
  11. int t;
  12. cin >> t;
  13. while(t--){
  14. int n, s;
  15. cin >> n >> s;
  16. int a[n];
  17. for(int i = 0; i < n; i++){
  18. cin >> a[i];
  19. }
  20. ll sum = 0;
  21. for(int i = 1; i < n-1; i++){
  22. int x = a[i];
  23. int y = s * 2;
  24. sum += min(x, y) * a[i-1] + abs(x-y) * a[n-1];
  25. }
  26. cout << sum << endl;
  27.  
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5460KB
stdin
10
5 0
2 0 1 3 4
5 1
5 3 4 3 5
7 2
7 6 5 4 3 2 1
5 1
1 2 3 4 5
5 2
1 2 3 4 5
4 0
0 1 1 1
5 5
4 3 5 6 4
4 1
0 2 1 0
3 99999
200000 200000 200000
6 8139
7976 129785 12984 78561 173685 15480
stdout
16
44
96
27
35
2
121
2
1345294336
4219044662