結果

問題 No.1861 Required Number
ユーザー ygd.ygd.
提出日時 2022-03-04 22:39:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,781 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 237,344 KB
最終ジャッジ日時 2023-09-26 03:46:03
合計ジャッジ時間 17,759 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,132 KB
testcase_01 AC 74 ms
75,612 KB
testcase_02 AC 71 ms
71,360 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 77 ms
75,200 KB
testcase_08 WA -
testcase_09 AC 78 ms
75,520 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 82 ms
75,460 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 77 ms
75,532 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 83 ms
75,264 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 71 ms
71,132 KB
04_evil_1.txt MLE -
04_evil_2.txt MLE -
04_evil_3.txt MLE -
04_evil_4.txt MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline
input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]

def solve(A,K):
    N = len(A)
    dp = [[0]*(K+1) for _ in range(N+1)]
    dp[0][0] = 1 
    #配るDP
    for i in range(N):
        a = A[i]
        for j in range(K+1):
            #使わない場合
            dp[i+1][j] = max(dp[i+1][j], dp[i][j])
            #使う場合
            if j+a <= K:
                dp[i+1][j+a] = max(dp[i+1][j+a], dp[i][j])
    used = []
    if dp[N][K] == 0:
        return used
    v = K
    for i in reversed(range(N)):
        a = A[i]
        if v-a >= 0 and dp[i][v-a] == 1:
            used.append(i)
            v -= a

    return used

def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    mx = [-1]*(1+K)
    mn = [-1]*(1+K)
    A.sort()
    for i,a in enumerate(A):
        if mn[a] == -1:
            mn[a] = i
        mx[a] = i

    X = solve(A,K)
    X.sort()
    #print(X)
    A.reverse()
    Y = solve(A,K)
    Y = sorted([N-1-y for y in Y])
    #print(X,Y)
    #print(mx)
    #print(mn)
    #print(Y)
    X = set(X)
    Y = set(Y)
    Z = X&Y
    if len(Z) == 0:
        print(-1);exit()
        
    ans = 0
    for v in range(1+K):
        if mx[v] == -1: continue
        num = mx[v] - mn[v] + 1 #値vはnum個ある
        if mx[v] in Z and mn[v] in Z:
            ans += num

    print(ans)
    

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