MAX=1000
def takeSecond(elem):return elem[1]
def Arrange(n,work):work.sort(key&#61;takeSecond)print("排序后&#xff1a;")for i in range(1,n&#43;1):print(work[i])deadline&#61;work[n][1]punish&#61;[[MAX]*(deadline&#43;1) for i in range(n&#43;1)]for j in range(deadline&#43;1):if work[1][0]<&#61; j:punish[1][j] &#61; 0else:punish[1][j] &#61; work[1][2]if punish[1][deadline]&#61;&#61;0:count&#61;1for i in range(2,n&#43;1):for j in range(deadline&#43;1):punish[i][j] &#61; punish[i-1][j] &#43; work[i][2]time&#61;min(j,work[i][1])if(time >&#61; work[i][0] and punish[i][j]>punish[i-1][time-work[i][0]]):punish[i][j] &#61; punish[i-1][time-work[i][0]]if j&#61;&#61;deadline:count&#43;&#61;1print("任务安排表&#xff1a;")for i in range(1,n&#43;1):print(punish[i])return punish[n][deadline],count
file_readpath &#61; &#39;input.txt&#39;
with open(file_readpath) as file:txt &#61; file.read()
txt&#61;txt.split(&#39;\n&#39;)
n&#61;eval(txt[0])
work&#61;[[0,0,0]]
for t in txt[1:]:t&#61;t.split()work.append([eval(t[0]),eval(t[1]),eval(t[2])])
punish,count&#61;Arrange(n,work)
print("最小惩罚为",punish)
print("执行文件个数为",count)
file_writepath &#61; &#39;output.txt&#39;
file&#61;open(file_writepath,"w")
file.write(str(punish)&#43;&#39;\n&#39;&#43;str(count))
file.close()
运行结果&#xff1a;