結果
問題 | No.1029 JJOOII 3 |
ユーザー |
![]() |
提出日時 | 2020-04-17 22:18:34 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 469 ms / 2,000 ms |
コード長 | 1,401 bytes |
コンパイル時間 | 339 ms |
コンパイル使用メモリ | 82,440 KB |
実行使用メモリ | 97,636 KB |
最終ジャッジ日時 | 2024-10-03 13:43:10 |
合計ジャッジ時間 | 10,369 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
import sysinput = sys.stdin.readlineN,K=map(int,input().split())S=[input().split() for i in range(N)]DP=[1<<63]*(3*K+2+1000)DP[0]=0T=["J"]*K+["O"]*K+["I"]*KJ=[]O=[]I=[]for s,M in S:M=int(M)J.append((s.count("J"),M))O.append((s.count("O"),M))I.append((s.count("I"),M))from fractions import Fractiondef compare(x,y):if x==0:return 0if y!=0:return Fraction(x,y)else:return 1<<63MAXJ=max(J,key=lambda x:compare(x[0],x[1]))MAXO=max(O,key=lambda x:compare(x[0],x[1]))MAXI=max(I,key=lambda x:compare(x[0],x[1]))if MAXJ[0]==0 or MAXO[0]==0 or MAXI[0]==0:print(-1)sys.exit()for i in range(3*K):#NEXT=T[i:i+80]if i<K-500:if DP[i]==1<<63:continueelse:DP[i+MAXJ[0]]=DP[i]+MAXJ[1]elif K+100<i<K*2-500:if DP[i]==1<<63:continueelse:DP[i+MAXO[0]]=DP[i]+MAXO[1]elif K*2+100<i<K*3-500:if DP[i]==1<<63:continueelse:DP[i+MAXI[0]]=DP[i]+MAXI[1]else:for j in range(N):ind=0M=int(S[j][1])for s in S[j][0]:if i+ind<3*K and s==T[i+ind]:ind+=1DP[i+ind]=min(DP[i+ind],DP[i]+M)if 1<<63==DP[K*3]:print(-1)else:print(DP[K*3])