其他分享
首页 > 其他分享> > C - Divide and Multiply

C - Divide and Multiply

作者:互联网

C - Divide and Multiply

原题链接找不到了...

William has array of nn numbers a1​,a2​,…,an​ He can perform the following sequence of operations any number of times:

  1. Pick any two items from array ai and aj​, where ai must be a multiple of 2
  2. ai​=ai/2
  3. aj = aj*2

Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1 \le t \le 10^41≤t≤104). Description of the test cases follows.

The first line of each test case contains an integer nn (1 \le n \le 15)(1≤n≤15), the number of elements in William's array.

The second line contains nn integers a_1, a_2, \dots, a_na1​,a2​,…,an​ (1 \le a_i < 16)(1≤ai​<16), the contents of William's array.

Output

For each test case output the maximal sum of array elements after performing an optimal sequence of operations.

Example

Input
5
3
6 4 2
5
1 2 3 4 5
1
10
3
2 3 4
15
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
Output
50
46
10
26
35184372088846

Note

In the first example test case the optimal sequence would be:

  1. Pick i = 2i=2 and j = 1j=1. After performing a sequence of operations a_2 = \frac{4}{2} = 2a2​=24​=2 and a_1 = 6 \cdot 2 = 12a1​=6⋅2=12, making the array look as: [12, 2, 2].
  2. Pick i = 2i=2 and j = 1j=1. After performing a sequence of operations a_2 = \frac{2}{2} = 1a2​=22​=1 and a_1 = 12 \cdot 2 = 24a1​=12⋅2=24, making the array look as: [24, 1, 2].
  3. Pick i = 3i=3 and j = 1j=1. After performing a sequence of operations a_3 = \frac{2}{2} = 1a3​=22​=1 and a_1 = 24 \cdot 2 = 48a1​=24⋅2=48, making the array look as: [48, 1, 1].

The final answer 48 + 1 + 1 = 5048+1+1=50.

In the third example test case there is no way to change the sum of elements, so the answer is 1010.

题意:题目不算复杂,给一个大小为n的数组,通过上述运算使得数组总和最大,并输出最大值。

思路:先对数组内所有满足条件的元素进行运算2,即/2,记录次数cnt。将数组排序,数组的最大值 *= 2 cnt次

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll t,n,a[19],sum,cnt;

int main()
{
    cin >> t;
    while(t--)
    {
        memset(a,0,sizeof(a));
        cnt = 0,sum = 0;
        cin >> n;
        for(int i = 0;i < n;i++)
        {
            cin >> a[i];
        }
        for(int i = 0;i < n;i++)
        {
            while(a[i]>1 && !(a[i]&1)) a[i]/=2,cnt++;
        }
        sort(a,a+n);
        while(cnt--) a[n-1]*=2;
        for(int i = 0;i < n;i++)
        {
            sum += a[i];
        }
        printf("%lld\n",sum);
    }
}

标签:operations,cnt,Divide,sequence,sum,test,Multiply,array
来源: https://www.cnblogs.com/wbw1537/p/15690652.html