Python “最短”挑戰(12.23)

Description

現有n項工作,你希望僱傭一些員工完成它們。你有m個員工可以僱傭,一個能力值爲x的員工可以完成一個難度不超過x的工作,且需要支付x元工資。如何僱傭員工才能完成所有工作,並且支付的工資爲多少?需要注意一個員工只能完成一項工作,且不能被僱傭兩次。

Input

三行。第一行爲正整數n和m,第2行爲n項工作的難度,第3行爲m個員工的能力值,所有數據用空格分隔。

Output

最少工資。如果無解輸出None。
其餘要求同首題

Sample Input

2 3
5 4
7 8 4

Sample Output

11

Reference code

[n,m]=list(map(int,input().split(' ')))
if n>m:
    print(None)
else:
    x=sorted(list(map(int,input().split(' '))))
    y=sorted(list(map(int,input().split(' '))))
    ans,buf=0,0
    for i in range(n):
        if x[n-1]>y[m-1]:
            print(None)
            break
        for j in range(buf,m):
            if x[i]<=y[j]:
                ans+=y[j]
                y[j]=0
                buf=j+1
                break
    print(ans)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章