結果

問題 No.1912 Get together 2
ユーザー lam6er
提出日時 2025-03-31 17:25:25
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,728 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 97,168 KB
最終ジャッジ日時 2025-03-31 17:26:27
合計ジャッジ時間 11,233 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 4 WA * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())
v_list = list(map(int, input().split()))
s_list = [input().strip() for _ in range(n)]

# Create a list of slimes sorted by value in descending order, along with their compatibility strings
sorted_slimes = sorted(zip(v_list, s_list), key=lambda x: -x[0])

# Initialize presum for each box (1-based)
presum = [0] * (m + 1)  # indexes 1..m
for v, s in zip(v_list, s_list):
    for j in range(m):
        if s[j] == 'o':
            box = j + 1
            presum[box] += v

sum_boxes = [0] * (m + 1)  # sum_boxes[1..m] are the current sums

for v, s in sorted_slimes:
    # Determine available boxes for this slime
    available = []
    for j in range(m):
        if s[j] == 'o':
            available.append(j + 1)  # box number is 1-based
    
    if not available:
        continue  # This should not happen as per problem statement
    
    # Find the maximum current sum among available boxes
    max_sum = -1
    candidates = []
    for box in available:
        current_sum = sum_boxes[box]
        if current_sum > max_sum:
            max_sum = current_sum
            candidates = [box]
        elif current_sum == max_sum:
            candidates.append(box)
    
    # Among candidates, select the one with the highest presum
    selected = -1
    max_p = -1
    for box in candidates:
        if presum[box] > max_p:
            max_p = presum[box]
            selected = box
    
    # Update the selected box's sum
    sum_boxes[selected] += v
    
    # Update the presum for all available boxes (subtract v from their presum)
    for box in available:
        presum[box] -= v

# Calculate the total sum of squares
total = 0
for s in sum_boxes[1:]:
    total += s * s

print(total)
0