結果

問題 No.77 レンガのピラミッド
ユーザー lam6er
提出日時 2025-03-20 20:43:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 51 ms / 5,000 ms
コード長 1,183 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 82,776 KB
実行使用メモリ 64,392 KB
最終ジャッジ日時 2025-03-20 20:43:55
合計ジャッジ時間 2,014 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

n = int(input())
a = list(map(int, input().split()))
s = sum(a)
min_cost = float('inf')

max_m = int(math.isqrt(s))  # Maximum possible m such that m^2 <= s

for m in range(1, max_m + 1):
    k = 2 * m - 1
    e_pyramid = 0
    d_pyramid = 0

    for j in range(1, k + 1):
        distance = abs(j - m)
        b_j = m - distance
        if j <= n:
            available = a[j - 1]
        else:
            available = 0

        # Calculate excess and deficit
        excess = available - b_j
        if excess > 0:
            e_pyramid += excess
        deficit = b_j - available
        if deficit > 0:
            d_pyramid += deficit

    # Calculate sum of non-pyramid columns (j > k)
    sum_non = 0
    if k < n:
        for j in range(k + 1, n + 1):
            sum_non += a[j - 1]

    total_available_surplus = e_pyramid + sum_non

    # Check if surplus can cover the deficit
    if total_available_surplus >= d_pyramid:
        current_cost = e_pyramid + sum_non
        if current_cost < min_cost:
            min_cost = current_cost

# If no valid m found (unlikely given the constraints), default to 0
print(min_cost if min_cost != float('inf') else 0)
0