結果

問題 No.2028 Even Choice
ユーザー ikomaikoma
提出日時 2022-08-05 23:07:23
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,399 bytes
コンパイル時間 1,033 ms
コンパイル使用メモリ 86,992 KB
実行使用メモリ 124,124 KB
最終ジャッジ日時 2023-10-14 00:53:24
合計ジャッジ時間 11,597 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 773 ms
121,804 KB
testcase_02 AC 687 ms
121,816 KB
testcase_03 AC 124 ms
102,868 KB
testcase_04 AC 200 ms
124,124 KB
testcase_05 AC 198 ms
85,248 KB
testcase_06 AC 608 ms
116,980 KB
testcase_07 AC 232 ms
88,076 KB
testcase_08 AC 400 ms
99,200 KB
testcase_09 AC 528 ms
97,632 KB
testcase_10 AC 246 ms
88,104 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 635 ms
103,016 KB
testcase_14 AC 212 ms
87,916 KB
testcase_15 AC 71 ms
71,564 KB
testcase_16 AC 72 ms
71,412 KB
testcase_17 AC 72 ms
71,216 KB
testcase_18 AC 71 ms
71,348 KB
testcase_19 AC 76 ms
71,288 KB
testcase_20 AC 74 ms
71,364 KB
testcase_21 AC 74 ms
71,624 KB
testcase_22 AC 73 ms
71,500 KB
testcase_23 AC 72 ms
71,332 KB
testcase_24 AC 76 ms
71,512 KB
testcase_25 AC 73 ms
71,572 KB
testcase_26 AC 73 ms
71,376 KB
testcase_27 AC 75 ms
71,552 KB
testcase_28 AC 413 ms
97,496 KB
testcase_29 AC 278 ms
96,924 KB
testcase_30 AC 207 ms
88,012 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:
        base = ans
        # 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(ans, base + max(B) - emin)

    elif 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