#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <climits>
#include <queue>
#include <algorithm>
#include <cassert>
#include <cmath>
#define INF INT_MAX
#define NIL -1
#define PR(A) cout<<endl<<#A<<" = "<<A;
using namespace std;
void rem_1(vector<int>&a,int x){
int min=INT_MAX;
int index=-1;
for(int i=0;i<a.size();i++){
if(a[i]%3==x && a[i]!=INT_MIN && a[i]<min){
min=a[i];index=i;
}//end if
}//end for
a[index]=INT_MIN;
}//end rem_1
void largest_multiple(vector<int>&a){
int count[3];
for(auto i:a){
count[i%3]++;
}//end for1
int sum=accumulate(a.begin(),a.end(),0);
if(sum%3==1){
if(count[1]>0){
rem_1(a,1);
}//end if
else if(count[2]>1){
rem_1(a,2);
rem_1(a,2);
}//end if
else
return ;
}//end if
else if(sum%3==2){
if(count[2]>0){
rem_1(a,2);
}//end if
else if(count[2]>1){
rem_1(a,1);
rem_1(a,1);
}//end else if
else
return ;
}//end else
sort(a.begin(),a.end(),greater<int>());
for(auto i:a){
if(i==INT_MIN)
return;
cout<<i;
}//end for
}//end largest_multiple
int main(){
vector<int>a{8, 1, 7, 6, 0};
largest_multiple(a);
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGNsaW1pdHM+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNhc3NlcnQ+CiNpbmNsdWRlIDxjbWF0aD4KI2RlZmluZSBJTkYgSU5UX01BWAojZGVmaW5lIE5JTCAtMQojZGVmaW5lIFBSKEEpIGNvdXQ8PGVuZGw8PCNBPDwiID0gIjw8QTsKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgcmVtXzEodmVjdG9yPGludD4mYSxpbnQgeCl7CiAgICBpbnQgbWluPUlOVF9NQVg7CiAgICBpbnQgaW5kZXg9LTE7CiAgICBmb3IoaW50IGk9MDtpPGEuc2l6ZSgpO2krKyl7CiAgICAgICAgaWYoYVtpXSUzPT14ICYmIGFbaV0hPUlOVF9NSU4gJiYgYVtpXTxtaW4pewogICAgICAgICAgICBtaW49YVtpXTtpbmRleD1pOwogICAgICAgIH0vL2VuZCBpZgogICAgfS8vZW5kIGZvcgogICAgYVtpbmRleF09SU5UX01JTjsKfS8vZW5kIHJlbV8xCnZvaWQgbGFyZ2VzdF9tdWx0aXBsZSh2ZWN0b3I8aW50PiZhKXsKICAgIGludCBjb3VudFszXTsKICAgIGZvcihhdXRvIGk6YSl7CiAgICAgICAgICAgIGNvdW50W2klM10rKzsKICAgIH0vL2VuZCBmb3IxCiAgICBpbnQgc3VtPWFjY3VtdWxhdGUoYS5iZWdpbigpLGEuZW5kKCksMCk7CiAgICBpZihzdW0lMz09MSl7CiAgICAgICAgaWYoY291bnRbMV0+MCl7CiAgICAgICAgICAgIHJlbV8xKGEsMSk7CiAgICAgICAgfS8vZW5kIGlmCiAgICAgICAgZWxzZSBpZihjb3VudFsyXT4xKXsKICAgICAgICAgICAgcmVtXzEoYSwyKTsKICAgICAgICAgICAgcmVtXzEoYSwyKTsKICAgICAgICB9Ly9lbmQgaWYKICAgICAgICBlbHNlCiAgICAgICAgcmV0dXJuIDsKICAgIH0vL2VuZCBpZgogICAgZWxzZSBpZihzdW0lMz09Mil7CiAgICAgICAgICAgIGlmKGNvdW50WzJdPjApewogICAgICAgICAgICByZW1fMShhLDIpOwogICAgICAgICAgICB9Ly9lbmQgaWYKICAgICAgICAgICAgZWxzZSBpZihjb3VudFsyXT4xKXsKICAgICAgICAgICAgcmVtXzEoYSwxKTsKICAgICAgICAgICAgcmVtXzEoYSwxKTsKICAgICAgICAgICAgfS8vZW5kIGVsc2UgaWYKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gOwogICAgfS8vZW5kIGVsc2UKICAgIHNvcnQoYS5iZWdpbigpLGEuZW5kKCksZ3JlYXRlcjxpbnQ+KCkpOwogICAgZm9yKGF1dG8gaTphKXsKICAgICAgICBpZihpPT1JTlRfTUlOKQogICAgICAgICAgICByZXR1cm47CiAgICAgICAgY291dDw8aTsKICAgIH0vL2VuZCBmb3IKfS8vZW5kIGxhcmdlc3RfbXVsdGlwbGUKaW50IG1haW4oKXsKdmVjdG9yPGludD5hezgsIDEsIDcsIDYsIDB9OwpsYXJnZXN0X211bHRpcGxlKGEpOwpyZXR1cm4gMDsKfQoKCgoKCgoK