fork download
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. using namespace std;
  5. int main()
  6. {
  7. int t;
  8. cin>>t;
  9. while(t--)
  10. {
  11. int n,k;
  12. cin>>n>>k;
  13. int a[n];
  14. for(int i=0;i<n;i++)
  15. {
  16. cin>>a[i];
  17. }
  18. sort(a,a+n,greater<int>());
  19. for(int i=0;i<n-1;i++)
  20. {
  21. if(a[i+1]>k && a[i]>k)
  22. {
  23. a[i]-=1;
  24. a[i]-=1;
  25. i-=1;
  26. }
  27. else
  28. break;
  29. }
  30. long long int s=0;
  31. for(int i=0;i<n;i++)
  32. {
  33. s+=a[i];
  34. }
  35. cout<<s<<endl;
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0s 4520KB
stdin
3
2 1
1 2
2 1
2 2
3 1
2 3 2
stdout
3
2
5