Problem2019--最大差

2019: 最大差

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 128 MB

Description

     现在给你n个数字xi,请你分别从中选m个数字组成两个m位数,要求这两个数的差最大,并输出这个差。2<=n<=100,1<=m<=17,0<=xi<=9,m<=n。   注意2位数以上前面不能有0.

Input

第一行,两个数,n和m。 
第二行,n个数字。 

Output

一个整数

Sample Input Copy

4 3
1 1 2 3

Sample Output Copy

209

HINT

321-112=209
n个数字中保证至少1个非0。两个m位数所选的数字不一定要全部相同。
n,m=map(int,input().split())
sl=input().split()

sl.sort()

b=sl[:-m-1:-1]

b=''.join(b)

if sl[0]=='0' and m>1:
    for i in range(len(sl)):
        if sl[i]!='0':
            sl[0],sl[i]=sl[i],sl[0]
            break

a=''.join(sl)
a=a[:m]
print(int(b)-int(a))


Source/Category