#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long f[22][111111], a[111111], b[111111];
long long ans, now, L, R;
void update(long long x, long long y) { ans += y * b[x] * (b[x] - 1) / 2; }
long long calc(long long l, long long r) {
  while (L < l) {
    update(a[L], -1);
    b[a[L]]--;
    update(a[L], 1);
    L++;
  }
  while (L > l) {
    L--;
    update(a[L], -1);
    b[a[L]]++;
    update(a[L], 1);
  }
  while (R < r) {
    R++;
    update(a[R], -1);
    b[a[R]]++;
    update(a[R], 1);
  }
  while (R > r) {
    update(a[R], -1);
    b[a[R]]--;
    update(a[R], 1);
    R--;
  }
  return ans;
}
void solve(long long x, long long y, long long l, long long r) {
  if (x > y || l > r) return;
  long long mid = l + r >> 1;
  long long mini = 0, minx = 1e18;
  for (long long i = x; i <= y; ++i) {
    long long tmp = calc(i + 1, mid);
    if (minx > f[now - 1][i] + tmp) {
      minx = f[now - 1][i] + tmp;
      mini = i;
    }
  }
  f[now][mid] = minx;
  solve(x, mini, l, mid - 1);
  solve(mini, y, mid + 1, r);
}
signed main() {
  memset(f, 0x3f, sizeof(f));
  f[0][0] = 0;
  long long n = read(), m = read();
  for (long long i = 1; i <= n; ++i) a[i] = read();
  b[a[1]]++;
  L = R = 1;
  for (now = 1; now <= m; ++now) solve(0, n - 1, 1, n);
  cout << f[m][n] << endl;
  fclose(stdin);
  fclose(stdout);
  return 0;
}
