結果

問題 No.1522 Unfairness
ユーザー ygd.ygd.
提出日時 2021-05-29 15:30:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,192 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 217,984 KB
最終ジャッジ日時 2024-04-25 11:57:43
合計ジャッジ時間 5,209 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,224 KB
testcase_01 AC 35 ms
51,968 KB
testcase_02 AC 35 ms
52,224 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 367 ms
198,656 KB
testcase_21 AC 36 ms
52,224 KB
testcase_22 AC 34 ms
52,352 KB
testcase_23 WA -
testcase_24 AC 36 ms
52,352 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 36 ms
52,096 KB
testcase_28 AC 37 ms
52,224 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys 
input = sys.stdin.buffer.readline
def main():
    N,M = map(int,input().split()); INF = float("inf")
    A = list(map(int,input().split()))
    A.sort(reverse=True)

    dp = [[INF]*(N+1) for _ in range(N+1)]
    dp[0][0] = 0
    for i in range(1,N+1):
        for j in range(1,N+1):
            if j%2 == 1: #奇数個
                dp[i][j] = min(dp[i-1][j], dp[i-1][j-1] + A[i-1])
            else:
                dp[i][j] = min(dp[i-1][j], dp[i-1][j-1] - A[i-1])
    #print(dp)

    ans = 0
    for i in reversed(range(N+1)):
        if i%2 == 0 and dp[N][i] <= M:
            dif = dp[N][i]
            num = i
            break
    else:
        print(0);exit()
    #print(dif,num)
    #最後の和と個数(=2k)
    now = N
    while now > 0:
        #now番目を選ばない
        for i in range(now+1):
            if dp[i][num] == dif:
                if num%2 == 0:
                    num -= 1
                    dif += A[i-1]
                else:
                    ans += A[i-1]
                    num -= 1
                    dif -= A[i-1]
                now = i
                break
    print(ans)
        







if __name__ == '__main__':
    main()
0