fork download
  1. /*
  2.   THE DARK NIGHT RISES
  3. I LOVE YOU 72 AND WILL FOREVER DO SO
  4. Patience, Persistence, Perseverance, Passion, Purpose
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. typedef long long int ll;
  9. typedef unsigned long long int ull;
  10. typedef long double ld;
  11. const int N = 2e5 + 20 ;
  12. #define F first
  13. #define S second
  14. # define all(v) v.begin(),v.end()
  15. #define rall(v) v.rbegin(),v.rend()
  16. #define pb push_back
  17. #define mk make_pair
  18. #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  19. #define rep(i,a,b) for(ll i=a;i<b;i++)
  20. typedef pair<int, int> ii;
  21. typedef vector<int> vi;
  22. typedef vector<ii> vii;
  23. typedef vector<vi> vvi;
  24. typedef vector<vii> vvii;
  25. #define sz(x) (int)x.size()
  26. const long long int mod=1e9 + 7;
  27. const long double EPS=1e-12;
  28. #define mem( a, val ) memset(a, val, sizeof( a ) )
  29. #define deci( x ) cout<<fixed<<setprecision( x );
  30. #define bitcount( x ) __builtin_popcountll( x )
  31.  
  32. const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  33. const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
  34.  
  35. int solve()
  36. {
  37. ll n , k ;
  38. cin >> n >> k ;
  39. ll ans = 0 ,sum=0,mx=0;
  40. std::vector<ll> v(n),x(n,0);
  41. rep(i,0,n) cin >> v[i] ;
  42. sort(all(v));
  43. rep(i,0,n-1)
  44. {
  45. x[i] = max(0ll,v[i]-k);
  46. sum += max(0ll,v[i]-k);
  47. mx = max(mx , x[i]);
  48. ans += min(k,v[i]);
  49. }
  50. if(sum%2==0 && (sum-mx )>= mx) ans += v[n-1] ;
  51. else if((sum -mx)< mx){ans += (v[n-1]-(mx-(sum-mx)));}
  52. else ans += (v[n-1]-1);
  53. cout << ans << '\n';
  54. }
  55. int main()
  56. {
  57. fast;
  58. ll t;
  59. t=1;
  60. cin>>t;
  61. while(t--)
  62. {
  63. solve();
  64. }
  65.  
  66. return 0;
  67. }
  68. // Patience, Persistence, Perseverance, Passion, Purpose
  69. // I LOVE YOU 72 AND WILL FOREVER DO SO.
Runtime error #stdin #stdout #stderr 0s 4556KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc