#P13008. 【MX-X13-T3】「KDOI-12」只有失去光明,才能逃脱黑暗。

    ID: 11696 远端评测题 2000ms 512MiB 尝试: 1 已通过: 1 难度: 6 上传者: 标签>动态规划 DP贪心O2优化位运算梦熊比赛

【MX-X13-T3】「KDOI-12」只有失去光明,才能逃脱黑暗。

Description

Given a non-negative integer xx, you need to transform it into yy through a series of operations with the minimum cost. Each operation is defined as follows:

  • Choose an integer 0ik0 \leq i \leq k, and pay a cost of aia_i to either add or subtract 2i2^i from xx.

Note: You do not need to ensure that xx remains non-negative during the operations.

Input Format

For each test case, output a single non-negative integer—the minimum cost required to transform xx into yy.

Output Format

For each test case, output a single non-negative integer—the minimum cost required to transform xx into yy.

5
2 4 1
2 5
2 5 2
2 5 2
3 9 2
1 2 3
4 23 3
1 5 2 4
1 114 5
1 4 1 9 19 8
4
4
5
11
29

Hint

Sample Explanation

For the second test case in the sample input, the following two operations transform xx into yy with the minimum cost:

  1. Choose i=2i=2, perform xx+22x \gets x + 2^2, resulting in x=6x=6 with a cost of 22.
  2. Choose i=0i=0, perform xx20x \gets x - 2^0, resulting in x=5x=5 with an additional cost of 22, totaling 44.

Data Range

This problem uses bundled testing.

Subtask Points TT \leq x,y<x, y < kk \leq aia_i
11 66 10310^3 232^3 33 109\leq 10^9
22 1515 2102^{10} 1010
33 2121 2×1052 \times 10^5 2302^{30} 11
44 2727 3030 2\leq 2
55 2020 10410^4 109\leq 10^9
66 1111 2×1052 \times 10^5

For all test cases:

  • 1T2×1051 \leq T \leq 2 \times 10^5,
  • 0x,y<2300 \leq x, y < 2^{30},
  • 1k301 \leq k \leq 30,
  • 1ai1091 \leq a_i \leq 10^9.

Hint

Please use fast input methods for reading data.


Translated by DeepSeek V3.