ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C - Divide and Multiply

2021-12-14 23:33:18  阅读:184  来源: 互联网

标签:operations cnt Divide sequence sum test Multiply array


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有