#include <bits/stdc++.h>
using namespace std;
int cnt[100005], L, R, a[100005];
long long ans;
void modify(int x, int v) {
  ans -= (1ll * cnt[x] * (cnt[x] - 1)) / 2;
  cnt[x] += v;
  ans += (1ll * cnt[x] * (cnt[x] - 1)) / 2;
}
long long calc(int l, int r) {
  while (L < l) modify(a[L++], -1);
  while (L > l) modify(a[--L], 1);
  while (R < r) modify(a[++R], 1);
  while (R > r) modify(a[R--], -1);
  return ans;
}
long long dp[22][100005];
int cur;
void solve(int lb, int rb, int l, int r) {
  if (lb > rb || l > r) return;
  int mid = l + r >> 1;
  int d = 0;
  long long res = 1e18;
  for (int i = lb; i <= rb; ++i) {
    long long tmp = calc(i + 1, mid);
    if (res > dp[cur - 1][i] + tmp) res = dp[cur - 1][i] + tmp, d = i;
  }
  dp[cur][mid] = res;
  solve(lb, d, l, mid - 1), solve(d, rb, mid + 1, r);
}
int main() {
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  cnt[a[1]]++, L = R = 1;
  for (cur = 1; cur <= m; ++cur) solve(0, n - 1, 1, n);
  printf("%lld", dp[m][n]);
  return 0;
}
