import itertools
import random

n, k = map(int, input().split())
a = list(map(int, input().split()))

best = 0
for _ in range(10 ** 6):
    el = [random.randint(0, k-1) for _ in range(n)]
    #print(el)
    ans = 0
    b = [[] for _ in range(k)]
    for i in range(n):
        b[el[i]].append(a[i])
    if any([len(b[bi]) == 0 for bi in range(k)]):
        continue
    for bi in range(k):
        mx = max(b[bi])
        ind = 0
        for i, bai in enumerate(b[bi]):
            if bai == mx:
                ind = i
        ans += mx + ind + 1
    if ans > best:
        best = ans
        #print(el)
    #print(el, ans)
print(best)