题目 › usaco-1565

Purchasing Milk

USACO 2026 Second · 铜组 · 2026-01 · L1 基础 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

On National Milk Day, Farmer John is offering exclusive prices on buckets of milk! He has $N$ ($1 \leq N \leq 10^5$) deals numbered from $1$ to $N$. For the $i$'th deal, he is offering $2^{i-1}$ buckets of milk for $a_i$ ($1 \leq a_i \leq 10^9, a_i < a_{i+1}$) moonies. The same deal may be taken any non-negative integer number of times.

You are thinking about $Q$ ($1 \leq Q \leq 10^4$) independent queries. For each query, you have an integer $x$ ($1 \leq x \leq 10^9$) in mind and wonder what is the minimum cost to purchase at least $x$ buckets of milk.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains two integers $N$ and $Q$.

The following line contains $a_1, a_2, \ldots, a_N$.

Each of the following $Q$ lines contains an integer $x$, representing a query.

OUTPUT FORMAT (print output to the terminal / stdout):

For each query, output the minimum cost on a new line.

Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

SAMPLE INPUT:

2 4
10 15
1
2
6
7

SAMPLE OUTPUT:

10
15
45
55
In the example above, Farmer John is offering 2 deals: 1 bucket of milk for 10 moonies and 2 buckets of milk for 15 moonies.

The cheapest cost to buy 1 bucket is just the cost of the 1 bucket deal and the cheapest cost to buy 2 buckets is just the cost of the 2 bucket deal.

To get 6 buckets, the cheapest way is to purchase 3 of the 2 bucket deal for a total of 45 moonies.

To get 7 buckets, the cheapest way is to purchase 3 of the 2 bucket deal and 1 of the 1 bucket deal for a total of 55 moonies.

SAMPLE INPUT:

4 10
10 25 30 70
1
2
3
4
5
6
7
8
15
101

SAMPLE OUTPUT:

10
20
30
30
40
50
60
60
120
760
In this example, Farmer John is offering a total of 4 deals for 1, 2, 4, and 8 buckets. For each of the 10 queries, the corresponding output indicates the minimum cost to purchase at least that amount of milk. Sometimes, it is cheaper to purchase more than the specified amount.

SCORING:

  • Inputs 3-4: $N \leq 2$
  • Inputs 5-8: $N \leq 10$
  • Inputs 9-16: No additional constraints.

Problem credits: Chongtian Ma

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 13.8k平均输出 token(含思考) ↓
  • 26.5输出速度 tokens/s ↑
  • 525 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 2Pylint 问题 ↓
  • 9.39Pylint 得分 ↑
  • 6圈复杂度 ↓
  • 68.3可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 20.9k平均输出 token(含思考) ↓
  • 191.7输出速度 tokens/s ↑
  • 110 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 5Pylint 问题 ↓
  • 8.44Pylint 得分 ↑
  • 9圈复杂度 ↓
  • 47.3可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 25.8k平均输出 token(含思考) ↓
  • 140.3输出速度 tokens/s ↑
  • 185 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 4Pylint 问题 ↓
  • 8.75Pylint 得分 ↑
  • 10圈复杂度 ↓
  • 72.7可维护性指数 ↑

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 53 行
import sys


def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    if not data:
        return

    n, q = data[0], data[1]
    prices = data[2:2 + n]
    queries = data[2 + n:2 + n + q]

    # Deal 31 already supplies 2^30 > 10^9 buckets. Every later deal
    # costs more, so none of them can improve an answer.
    limit = min(n, 31)
    deals = []  # (buckets supplied, cost), retaining only useful deals
    exact_cost = 0

    for i in range(limit):
        cost = prices[i]
        if i == 0:
            deals.append((1, cost))
            exact_cost = cost
            continue

        previous_option = 2 * exact_cost
        if cost < previous_option:
            deals.append((1 << i, cost))
            exact_cost = cost
        else:
            exact_cost = previous_option

    def calculate(j, amount):
        if j == 0:
            return amount * deals[0][1]

        size, cost = deals[j]
        whole, remainder = divmod(amount, size)

        # Either buy one extra large deal, or buy `whole` large deals
        # and optimally cover the remainder with smaller deals.
        return min(
            (whole + 1) * cost,
            whole * cost + calculate(j - 1, remainder)
        )

    last = len(deals) - 1
    answers = [str(calculate(last, x)) for x in queries]
    sys.stdout.write("\n".join(answers))


if __name__ == "__main__":
    main()