fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. /*
  6.  Optimization over priority-queue algorithm :-
  7.  
  8. If(largest>=(tot_sum)/2){answer = largest;}
  9. else{
  10. -> answer = tot_sum/2; (conditions applied)
  11. }
  12.  
  13.  */
  14. int n ;
  15. cin>>n;
  16.  
  17. vector<int>a(n);
  18. for(int i = 0 ;i < n ;i++){
  19. cin>>a[i];
  20. }
  21. int ans = 0 ;
  22. int x = *max_element(a.begin(),a.end());
  23. int y = accumulate(a.begin(),a.end(),0);
  24.  
  25. if(x>=y/2){
  26. ans = x;
  27. }else{
  28. if(y%2==0){
  29. ans = y/2;
  30.  
  31. }else{
  32. ans = y/2+1;
  33. }
  34. }
  35.  
  36. cout<<ans<<endl;
  37. return 0;
  38. }
Success #stdin #stdout 0s 5320KB
stdin
4
1 2 4 6
stdout
6