Exact Change

One day, early in the morning, you decided to buy yourself a bag of chips in the nearby store. The store has chips of nn different flavors. A bag of the ii-th flavor costs aiai burles.

The store may run out of some flavors, so you'll decide which one to buy after arriving there. But there are two major flaws in this plan:

  1. you have only coins of 11, 22 and 33 burles;
  2. since it's morning, the store will ask you to pay in exact change, i. e. if you choose the ii-th flavor, you'll have to pay exactly aiai burles.

Coins are heavy, so you'd like to take the least possible number of coins in total. That's why you are wondering: what is the minimum total number of coins you should take with you, so you can buy a bag of chips of any flavor in exact change?

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The first line of each test case contains the single integer nn (1≤n≤1001≤n≤100) — the number of flavors in the store.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the cost of one bag of each flavor.

Output

For each test case, print one integer — the minimum number of coins you need to buy one bag of any flavor you'll choose in exact change.

Example

input

Copy

4
1
1337
3
10 8 10
5
1 2 3 4 5
3
7 77 777

output

Copy

446
4
3
260

Note

In the first test case, you should, for example, take with you 445445 coins of value 33 and 11 coin of value 22. So, 1337=445⋅3+1⋅21337=445⋅3+1⋅2.

In the second test case, you should, for example, take 22 coins of value 33 and 22 coins of value 22. So you can pay either exactly 8=2⋅3+1⋅28=2⋅3+1⋅2 or 10=2⋅3+2⋅210=2⋅3+2⋅2.

In the third test case, it's enough to take 11 coin of value 33 and 22 coins of value 11.

思路:该题的难点是选 两个2 还是 一个1与一个3 当遇到该选哪种时,分情况讨论太难,情况太多而且也太难了,所以直接暴搜,因为1最多不会带超过两个,2也不会带超过两个,无意义。所以便能得出3最少max/3-2个(因为2*2=4,3要多退一个)3最多带max/3个,带多了少的也用不了这么多。所以将1的个数,2的个数,3的个数进行暴力找出最小的组合。每种组合是否合理也要判断,就是能将数组里的每个数都要能凑出来,一个凑不出来就不行,再用不超过硬币组合(每个硬币)的上限值的硬币个数来判断是否能凑出来。最后就是在合理的硬币数中找到值最小的即可。

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long ll;

int a[110],n;

bool check(int x,int y,int z)
{
    for(int i=0;i<n;i++)
    {
        int maxx=a[i]/3;
        bool ok=false;
        for(int j=max(0,maxx-2);j<=min(maxx,z);j++)
        {
            for(int l=0;l<=x;l++)
            {
                for(int r=0;r<=y;r++)
                {
                    if((j*3+l+2*r)==a[i])
                    {
                        ok=true;
                    }
                }
            }
        }
        if(ok==false) return false;
    }
    return true;
}

void solve()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    int ans=1<<30;
    int cur=a[n-1]/3;
    for(int i=0;i<=2;i++)
    {
        for(int j=0;j<=2;j++)
        {
            for(int k=max(0,cur-2);k<=cur;k++)
            {
                if(check(i,j,k))
                {
                    ans=min(ans,i+j+k);
                }
            }
        }
    }
    cout<<ans<<endl;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值