#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long n, m, a[N], L[N], R[N], sum[N], gs[21][N], dp[21][N];
inline long long read() {
  long long ret = 0, f = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (isdigit(c)) {
    ret = ret * 10 + c - 48;
    c = getchar();
  }
  if (f) return -ret;
  return ret;
}
void solve(long long dep, long long now, long long l, long long r, long long x,
           long long y) {
  if (l > r) return;
  long long mid = (l + r) / 2;
  while (R[now] < mid) {
    R[now]++;
    sum[now] += gs[now][a[R[now]]];
    gs[now][a[R[now]]]++;
  }
  while (L[now] < x) {
    gs[now][a[L[now]]]--;
    sum[now] -= gs[now][a[L[now]]];
    L[now]++;
  }
  while (L[now] < min(mid, y)) {
    gs[now][a[L[now]]]--;
    sum[now] -= gs[now][a[L[now]]];
    L[now]++;
  }
  long long xu = 0;
  for (long long i = min(mid, y); i >= x; i--) {
    if (dp[dep - 1][i - 1] + sum[now] < dp[dep][mid]) {
      dp[dep][mid] = dp[dep - 1][i - 1] + sum[now];
      xu = i;
    }
    if (L[now] > 1) {
      L[now]--;
      sum[now] += gs[now][a[L[now]]];
      gs[now][a[L[now]]]++;
    }
  }
  solve(dep, now + 1, l, mid - 1, x, xu);
  solve(dep, now + 1, mid + 1, r, xu, y);
}
signed main() {
  n = read();
  m = read();
  for (long long i = 1; i <= n; i++) a[i] = read();
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (long long i = 1; i <= m; i++) {
    for (long long j = 1; j <= 20; j++) {
      L[j] = 1;
      R[j] = 0;
      sum[j] = 0;
      for (long long k = 1; k <= n; k++) gs[j][k] = 0;
    }
    solve(i, 1, 1, n, 1, n);
  }
  cout << dp[m][n];
  return 0;
}
