結果

問題 No.2028 Even Choice
ユーザー ikomaikoma
提出日時 2022-08-05 22:59:40
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,362 bytes
コンパイル時間 523 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 123,984 KB
最終ジャッジ日時 2023-10-14 00:37:53
合計ジャッジ時間 10,841 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 745 ms
121,372 KB
testcase_02 AC 667 ms
121,432 KB
testcase_03 AC 126 ms
102,524 KB
testcase_04 AC 198 ms
123,984 KB
testcase_05 AC 198 ms
84,964 KB
testcase_06 AC 608 ms
116,652 KB
testcase_07 AC 232 ms
87,544 KB
testcase_08 AC 392 ms
99,032 KB
testcase_09 AC 515 ms
97,400 KB
testcase_10 AC 242 ms
87,944 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 635 ms
102,420 KB
testcase_14 AC 210 ms
87,684 KB
testcase_15 AC 68 ms
71,096 KB
testcase_16 RE -
testcase_17 AC 72 ms
71,224 KB
testcase_18 AC 72 ms
71,420 KB
testcase_19 AC 73 ms
71,440 KB
testcase_20 AC 73 ms
71,044 KB
testcase_21 AC 72 ms
70,884 KB
testcase_22 AC 72 ms
71,076 KB
testcase_23 AC 71 ms
71,220 KB
testcase_24 AC 72 ms
71,140 KB
testcase_25 AC 70 ms
71,220 KB
testcase_26 AC 71 ms
71,260 KB
testcase_27 AC 73 ms
71,104 KB
testcase_28 AC 396 ms
97,416 KB
testcase_29 AC 277 ms
96,828 KB
testcase_30 AC 205 ms
87,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,K=map(int,input().split())
A=list(map(int,input().split()))

if K==N-1:
    print(sum(A[1:]))
    exit()

Ai = [(a,i) for i,a in enumerate(A)]

B = sorted(Ai[1:], reverse=True)
odd = []
even = []
ans = 0
for b,i in B[:K]:
    if i%2==0:
        odd.append((b,i))
    else:
        even.append((b,i))
    ans += b
odd.sort(key=lambda x:x[1])
even.sort(key=lambda x:x[1])
if K==1:
    ans = max([A[i] for i in range(1,N,2)])
elif len(even)==0:
    b,i = odd[1]
    Ao = [A[j] for j in range(1,i,2)]
    mx = max(Ao)
    ans += mx - b
elif len(odd)>0:
    if len(odd)==1:
        # oddを捨てる
        B = sorted([A[i] for i in range(1,N,2)], reverse=True)
        ans = sum(B[:K])
        # oddを生かす
        emin = min([e for e,i in even])
        B = [A[i] for i in range(1,odd[0][1],2)]
        ans += max(B) - emin

    if odd[0][1]<even[0][1]:
        emin = min([e for e,i in even])
        base = ans
        # oddの先頭を捨てる → oddの2番目より前のevenの最大値を追加
        i = odd[1][1]
        ans = base + max([A[i] for i in range(1, i, 2)]) - odd[0][0]
        # oddの先頭を生かす → oddの1番目より前のevenの最大値を追加し、evenの最小値を引く
        ans = max(ans, base + max([A[i] for i in range(1, odd[0][1], 2)]) - emin)
print(ans)
0