zoj3590 -3+1

-3+1

Time Limit: 2 Seconds      Memory Limit: 65536 KB

ZOJ is 10 years old! For celebrating, we are offering the easiest problem in the world to you.

Recently we received a long sequence. We can modify the sequence once by the following two steps.

  1. Choose any element in the sequence, say x(satisfying x ≥ 3), and subtract 3 from x.
  2. Choose any element in the sequence, say x, and add 1 to x.

 

Now, we want to know how many times at most the sequence can be modified.

Input

The input contains multiple test cases. For each case, the first line contains an integer n(1 ≤ n ≤ 20000). The second line contains n integers describing the sequence. All the numbers in the sequence are non-negative and not greater than 1000000.

Output

Output number of times at most the sequence can be modified, one line per case.

Sample Input

1
10
2
10 11

Sample Output

4
10
题意:给你n个数,每次操作包括一次对其中一个数进行 -3 和  一个数进行 +1 , 最多可以进行多少次操作;
分析:nn 代表 n 个数每个除 3 之和,n1 表示这 n 个数中模 3 余 1 的个数,n2 表示 mod 3 == 1 的个数;
View Code
 1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5 long long n,n1,n2,nn,i,ans,t;
6 while(cin>>n)
7 {
8 ans=0;n1=n2=nn=0;
9 for(i=0;i<n;i++)
10 {
11 cin>>t;
12 ans=ans+t/3;
13 if(t%3==1) n1++;
14 if(t%3==2) n2++;
15 }
16 nn=ans;
17 if(nn<1)
18 {
19 cout<<ans<<endl;
20 }
21 else
22 {
23 ans=ans+n2;
24 if(nn<=n1)
25 {
26 ans=ans+(nn-1);
27 cout<<ans<<endl;
28 }
29 else
30 {
31 ans=ans+n1;
32 nn=nn-n1;
33 ans=ans+(nn-1)/2;
34 cout<<ans<<endl;
35 }
36 }
37 }
38 return 0;
39 }

posted @ 2012-03-12 21:45  mtry  阅读(543)  评论(0)    收藏  举报