fork download
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <vector>
  5. #include <climits>
  6. #include <queue>
  7. #include <algorithm>
  8. #include <cassert>
  9. #include <cmath>
  10. #define INF INT_MAX
  11. #define NIL -1
  12. #define PR(A) cout<<endl<<#A<<" = "<<A;
  13. using namespace std;
  14.  
  15. void rem_1(vector<int>&a,int x){
  16. int min=INT_MAX;
  17. int index=-1;
  18. for(int i=0;i<a.size();i++){
  19. if(a[i]%3==x && a[i]!=INT_MIN && a[i]<min){
  20. min=a[i];index=i;
  21. }//end if
  22. }//end for
  23. a[index]=INT_MIN;
  24. }//end rem_1
  25. void largest_multiple(vector<int>&a){
  26. int count[3];
  27. for(auto i:a){
  28. count[i%3]++;
  29. }//end for1
  30. int sum=accumulate(a.begin(),a.end(),0);
  31. if(sum%3==1){
  32. if(count[1]>0){
  33. rem_1(a,1);
  34. }//end if
  35. else if(count[2]>1){
  36. rem_1(a,2);
  37. rem_1(a,2);
  38. }//end if
  39. else
  40. return ;
  41. }//end if
  42. else if(sum%3==2){
  43. if(count[2]>0){
  44. rem_1(a,2);
  45. }//end if
  46. else if(count[2]>1){
  47. rem_1(a,1);
  48. rem_1(a,1);
  49. }//end else if
  50. else
  51. return ;
  52. }//end else
  53. sort(a.begin(),a.end(),greater<int>());
  54. for(auto i:a){
  55. if(i==INT_MIN)
  56. return;
  57. cout<<i;
  58. }//end for
  59. }//end largest_multiple
  60. int main(){
  61. vector<int>a{8, 1, 7, 6, 0};
  62. largest_multiple(a);
  63. return 0;
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
8760