結果

問題 No.693 square1001 and Permutation 2
ユーザー gew1fw
提出日時 2025-06-12 16:57:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 747 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 82,776 KB
実行使用メモリ 54,052 KB
最終ジャッジ日時 2025-06-12 16:57:16
合計ジャッジ時間 1,303 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 5 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
a = list(map(int, input().split()))

count = [0] * (n + 1)
for num in a:
    count[num] += 1

duplicates = []
for k in range(1, n + 1):
    if count[k] > 1:
        duplicates.extend([k] * (count[k] - 1))

missings = []
for m in range(1, n + 1):
    if count[m] == 0:
        missings.append(m)

# Check if the counts are equal
if len(duplicates) != len(missings):
    print(-1)
else:
    # Sort duplicates in descending order and missings in ascending order
    duplicates.sort(reverse=True)
    missings.sort()

    # Pair the largest duplicate with the smallest missing
    total_cost = 0
    for i in range(len(duplicates)):
        k = duplicates[i]
        m = missings[i]
        total_cost += (m - k)
    print(total_cost)
0