日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 214B】Hometask (模拟,有坑)

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 214B】Hometask (模拟,有坑) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?

You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by?2,?3,?5?without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.

Each digit is allowed to occur in the number the same number of times it occurs in the set.

Input

A single line contains a single integer?n?(1?≤?n?≤?100000)?— the number of digits in the set. The second line contains?n?digits, the digits are separated by a single space.

Output

On a single line print the answer to the problem. If such number does not exist, then you should print -1.

Examples

Input

1 0

Output

0

Input

11 3 4 5 4 5 3 5 3 4 4 0

Output

5554443330

Input

8 3 2 5 1 5 2 2 3

Output

-1

Note

In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.

解題報告:

? ?這題好惡心,明明不難的題,卡了半小時、、因為有前導零啊要特判。合適會出現前導零呢?在輸入全為0時,或者有一個1剩下全是0這樣的,或者兩個2剩下全是0這樣的。。并且不能讀入完數據就特判剛開始寫了if(a[n]==0) {puts("0");return 0;},結果發現不對啊處理不了一個1剩下全是0這樣的情況。然后改成if(sum == 0 || sum==1 || sum==2) {?puts("0");return 0 ;}發現處理不了兩個2剩下全是0的情況。。然后在每一個輸出中都判斷是否全是0這樣、、、因為不判斷就會出現前導零、、

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; int a[MAX],sum,sum1,sum2; int n; bool bk[MAX]; int main() {cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i),sum+=a[i];sort(a+1,a+n+1);if(a[1] != 0) {puts("-1");return 0 ;}if(sum == 0 || sum==1 || sum==2) {puts("0");return 0 ;}if(sum%3 == 0) {for(int i = n; i>=1; i--) printf("%d",a[i]);return 0 ;}if(sum%3 == 1) {int cnt1=0;for(int i = 1; i<=n; i++) {if(a[i]%3 == 1 && cnt1 != 1) {bk[i]=1;cnt1++;continue;}sum1+=a[i];}if(sum1 == sum) {int cnt2 = 0;for(int i = 1; i<=n; i++) {if(a[i]%3 == 2 && cnt2 != 2) {bk[i]=1;cnt2++;continue;}sum2+=a[i];}if(cnt2 != 2) {puts("-1");return 0 ;}else {int flag = 0;for(int i = n; i>=1; i--) {if(bk[i] == 0) {printf("%d",a[i]);if(a[i] == 0 && flag == 0) return 0 ;flag=1;}}return 0 ;}}else {int flag = 0;for(int i = n; i>=1; i--) {if(bk[i] == 0) {printf("%d",a[i]);if(a[i] == 0 && flag == 0) return 0 ;flag=1;}}return 0 ;}} //if(sum%3 == 2) {int cnt2=0;for(int i = 1; i<=n; i++) {if(a[i]%3 == 2 && cnt2 != 1) {bk[i]=1;cnt2++;continue;}sum2+=a[i];}if(sum2 == sum) {int cnt1=0;for(int i = 1; i<=n; i++) {if(a[i]%3 == 1 && cnt1 != 2) {bk[i]=1;cnt1++;continue;}sum1+=a[i];}if(cnt1 != 2) {puts("-1");return 0 ;}else {int flag = 0;for(int i = n; i>=1; i--) {if(bk[i] == 0) {printf("%d",a[i]);if(a[i] == 0 && flag == 0) return 0 ;flag=1;}}return 0 ;}}else {int flag=0;for(int i = n; i>=1; i--) {if(bk[i] == 0) {printf("%d",a[i]);if(a[i] == 0 && flag == 0) return 0 ;flag=1;}}return 0 ;}}return 0 ;}/* 11 3 9 9 6 4 3 6 4 9 6 0 Output -1 Answer 999666330 */

?

總結

以上是生活随笔為你收集整理的【CodeForces - 214B】Hometask (模拟,有坑)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。