CodeForces - 1609A Divide and Multiply

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1609A Divide and Multiply相关的知识,希望对你有一定的参考价值。

A. Divide and Multiply
time limit per test1 second
memory limit per test256 megabytes

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

Pick any two items from array ai and aj, where ai must be a multiple of 2
ai=ai/2
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 t (1≤t≤104). Description of the test cases follows.

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

The second line contains n integers a1,a2,…,an (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=2 and j=1. After performing a sequence of operations a2=4/2=2 and a1=6⋅2=12, making the array look as: [12, 2, 2].
2.Pick i=2 and j=1. After performing a sequence of operations a2=2/2=1 and a1=12⋅2=24, making the array look as: [24, 1, 2].
3.Pick i=3 and j=1. After performing a sequence of operations a3=2/2=1 and a1=24⋅2=48, making the array look as: [48, 1, 1].
The final answer 48+1+1=50.

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

问题链接CodeForces - 1609A Divide and Multiply
问题简述:(略)
问题分析:(略)
AC的C++语言程序如下:

/* CodeForces - 1609A Divide and Multiply */

#include <bits/stdc++.h>

using namespace std;

int main()

    int t;
    scanf("%d", &t);
    while (t--) 
        int n, a, maxa = 0, cnt = 0;
        long long ans = 0;

        scanf("%d", &n);
        for (int i = 1; i <= n; i++) 
            scanf("%d", &a);
            while (a % 2 == 0) cnt++, a /= 2;
            maxa = max(maxa, a);
            ans += a;
        
        ans += maxa * ((1LL << cnt) - 1);

        printf("%lld\\n", ans);
    

    return 0;

以上是关于CodeForces - 1609A Divide and Multiply的主要内容,如果未能解决你的问题,请参考以下文章

codeforces 792C. Divide by Three

Educational Codeforces Round 18 C. Divide by Three DP

Divide by three, multiply by two CodeForces - 977D (思维排序)

CodeForces 792C - Divide by Three [ 分类讨论 ]

Journey CodeForces - 1336F[data structures divide and conquer graphs trees]

Codeforces 792C. Divide by Three 贪心+分类讨论