fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. void solve() {
  6. int n;
  7. cin >> n;
  8. vector<ll> a(n);
  9. ll sum = 0, mx = 0;
  10. for (int i = 0; i < n; i++) {
  11. cin >> a[i];
  12. sum += a[i];
  13. mx = max(mx, a[i]);
  14. }
  15. if (n == 1) {
  16. cout << 0 << "\n";
  17. } else if (n == 2) {
  18. cout << max(a[0], a[1]) << "\n";
  19. } else {
  20. cout << sum - mx + mx * (n - 1) << "\n";
  21. }
  22. }
  23.  
  24. int main() {
  25. ios_base::sync_with_stdio(false);
  26. cin.tie(NULL);
  27. int t;
  28. cin >> t;
  29. while (t--) solve();
  30. return 0;
  31. }
Success #stdin #stdout 0s 5316KB
stdin
3
4
1 1 3 2
2
0 2
7
1 1 4 5 1 4 1
stdout
13
2
42