結果

問題 No.2028 Even Choice
ユーザー ikomaikoma
提出日時 2022-08-05 23:12:01
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,604 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 124,152 KB
最終ジャッジ日時 2023-10-14 01:00:37
合計ジャッジ時間 10,063 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 736 ms
121,652 KB
testcase_02 AC 646 ms
121,788 KB
testcase_03 AC 121 ms
102,932 KB
testcase_04 AC 193 ms
124,152 KB
testcase_05 AC 188 ms
85,200 KB
testcase_06 AC 599 ms
116,640 KB
testcase_07 AC 234 ms
88,032 KB
testcase_08 AC 388 ms
98,728 KB
testcase_09 AC 518 ms
97,472 KB
testcase_10 AC 244 ms
87,932 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 630 ms
102,780 KB
testcase_14 AC 208 ms
88,056 KB
testcase_15 AC 71 ms
71,544 KB
testcase_16 AC 72 ms
71,456 KB
testcase_17 AC 73 ms
71,404 KB
testcase_18 AC 73 ms
71,544 KB
testcase_19 AC 75 ms
71,076 KB
testcase_20 AC 75 ms
71,412 KB
testcase_21 AC 74 ms
71,220 KB
testcase_22 AC 73 ms
71,392 KB
testcase_23 AC 71 ms
71,412 KB
testcase_24 AC 73 ms
71,360 KB
testcase_25 AC 71 ms
71,228 KB
testcase_26 AC 73 ms
71,144 KB
testcase_27 AC 72 ms
71,452 KB
testcase_28 AC 406 ms
96,952 KB
testcase_29 AC 280 ms
96,972 KB
testcase_30 AC 205 ms
88,268 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:
    base = ans
    # oddの先頭を取り除く
    b,i = odd[1]
    Ao = [A[j] for j in range(1,i,2)]
    mx = max(Ao)
    ans += mx - b
    # oddの先頭を生かす
    Ao = [A[j] for j in range(1,odd[0][1],2)]
    omin = min([o for o,i in odd])
    ans = max(ans, base - omin + max(Ao))
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