fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. main()
  5. {
  6. int n;
  7. cin>>n;
  8. int a[n+1];
  9. int sum=0,ans=0;
  10.  
  11. for(int i=0;i<n;i++)
  12. {
  13. cin>>a[i];
  14. }
  15. sort(a,a+n);
  16. int target=n-1,low=0,high=n-1,flag=1;
  17. while(low<=high)
  18. {
  19. sum+=a[low];
  20. //cout<<sum<<endl;
  21. if(sum>a[high])
  22. {
  23. flag=1;
  24. sum=0;
  25. ans+=2*a[high];
  26. high--;
  27.  
  28. }
  29. else
  30. {
  31. flag=0;
  32. low++;
  33. }
  34. }
  35. if(flag==0)ans+=2*a[high];
  36. cout<<ans<<endl;
  37.  
  38.  
  39. }
Success #stdin #stdout 0s 4408KB
stdin
Standard input is empty
stdout
-7344