import itertools
import random

n, k = map(int, input().split())
a = list(map(int, input().split()))
best = 0
it = 0
for el in itertools.product(range(k), repeat=n):
    it += 1
    if it > 5 * 10 ** 6:
        break
    if it % 5 == 0 or it % 7 == 0 or it % 9 == 0 or it % 11 == 0:
        continue
    ans = 0
    b = [[] for _ in range(k)]
    cnt0 = k
    for i in range(n):
        if len(b[el[i]]) == 0:
            cnt0 -= 1
        b[el[i]].append(a[i])
    if cnt0 != 0:
        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(best)
