Universal Cup Judging System

Universal Cup

時間限制: 2.0 s 記憶體限制: 256 MB 總分: 100 可 Hack ✓
统计

Description

You are playing an adventure game. As a mage, you need to use magic to deal with several enemies in front of you. Each enemy has a level, which is an integer between $1$ and $n$. An enemy of level $i$ has $2^{i-1}$ health points.

You can use two kinds of spells:

  • Soul Link: consumes $m$ mana and applies the "Soul Link" effect to one enemy. This effect lasts until that enemy dies.
  • Attack: consumes $1$ mana and deals $1$ damage to all enemies with the "Soul Link" effect, i.e., decreases their health by $1$. Once an enemy's health drops to $0$, that enemy dies, and you gain $m$ mana as a reward.

Among the enemies in front of you, there are $a_i$ enemies of level $i$. You may use the two spells any number of times, but at any time your mana cannot be less than $0$. What is the minimum initial mana you need in order to defeat all enemies?

Input

The first line contains an integer $T$ ($1 \le T \le 500$), denoting the number of test cases.

For each test case, the first line contains two integers $n, m$ ($1 \le n \le 30, 1 \le m \le 10^{18}$), representing the maximum enemy level and the mana cost of Soul Link, respectively.

The second line contains $n$ positive integers. The $i$-th integer $a_i$ denotes the number of enemies of level $i$.

For each test case, it is guaranteed that $0 \le a_i \le 10^9$ and $\sum_{i=1}^n a_i > 0$.

Output

For each test case, output one line containing an integer, denoting the minimum initial mana required to defeat all enemies.

Examples

Input 1

2
5 7
5 2 4 1 2
6 4
1 1 4 5 1 4

Output 1

34
48

Note

Explanation for the first test case:

Initially, there are 14 enemies, with health values 1, 1, 1, 1, 1, 2, 2, 4, 4, 4, 4, 8, 16, 16.

With 34 mana initially, first apply Soul Link to the enemies with health 1, 1, 1, 1, leaving 6 mana.

Attack once, and the enemies' health becomes 0, 0, 0, 0, 1, 2, 2, 4, 4, 4, 4, 8, 16, 16. Four enemies die, leaving 33 mana.

Apply Soul Link to the enemies with health 1, 2, 2, 4, leaving 5 mana.

Attack once, and the enemies' health becomes 0, 1, 1, 3, 4, 4, 4, 8, 16, 16. One enemy dies, leaving 11 mana.

Apply Soul Link to the enemy with health 4, leaving 4 mana.

Attack once, and the enemies' health becomes 0, 0, 2, 3, 4, 4, 8, 16, 16. Two enemies die, leaving 17 mana.

Apply Soul Link to the enemies with health 4, 16, leaving 3 mana.

Attack twice, and the enemies' health becomes 0, 1, 2, 4, 8, 14, 16. One enemy dies, leaving 8 mana.

Apply Soul Link to the enemy with health 8, leaving 1 mana.

Attack once, and the enemies' health becomes 0, 1, 4, 7, 13, 16. One enemy dies, leaving 7 mana.

Attack once, and the enemies' health becomes 0, 4, 6, 12, 16. One enemy dies, leaving 13 mana.

Apply Soul Link to the enemy with health 4, leaving 6 mana.

Attack four times, and the enemies' health becomes 0, 2, 8, 16. One enemy dies, leaving 9 mana.

Apply Soul Link to the enemy with health 16, leaving 2 mana.

Attack twice, and the enemies' health becomes 0, 6, 14. One enemy dies, leaving 7 mana.

Attack six times, and the enemies' health becomes 0, 8. One enemy dies, leaving 8 mana.

Attack eight times, and all enemies die, leaving 0 mana.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.