結果
| 問題 |
No.2799 Cut and Eat
|
| コンテスト | |
| ユーザー |
Kude
|
| 提出日時 | 2024-06-29 02:51:53 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 56 ms / 2,000 ms |
| コード長 | 1,137 bytes |
| コンパイル時間 | 158 ms |
| コンパイル使用メモリ | 82,432 KB |
| 実行使用メモリ | 61,952 KB |
| 最終ジャッジ日時 | 2024-06-29 02:51:57 |
| 合計ジャッジ時間 | 3,015 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 |
ソースコード
from functools import cache
@cache
def f(p):
ans = -sum(p)
if not p:
return 0
n = len(p)
for i in range(n):
x = p[i]
if x <= k:
ans = max(ans, x - f(tuple(sorted(p[:i] + p[i+1:]))))
else:
for s in range(1, x):
ans = max(ans, -f(tuple(sorted(p[:i] + p[i+1:] + (s, x - s)))))
return ans
from random import *
# k = int(input())
# k=3
def solve(k, inp):
d1 = []
d2 = []
for x in inp:
if x <= k:
d1.append(x)
else:
d2.append(x)
d1.sort(reverse=True)
res = sum(d1[0::2]) - sum(d1[1::2])
turn = len(d1) % 2
if sum(d2) % 2:
if (turn + len(d2)) % 2 == 0:
res += 1
else:
res -= 1
return res
for _ in range(0):
k = randrange(1, 6)
inp = tuple(sorted(randrange(1, 10) for _ in range(randrange(1, 5))))
f.cache_clear()
res = solve(k, inp)
# print(k, inp, f(inp), res)
assert f(inp) == res
# print(f(inp))
n, k = map(int, input().split())
a = tuple(sorted(map(int, input().split())))
print((solve(k, a) + sum(a)) // 2)
Kude