#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long INF = 1ll << 60;
const double Inf = 1e20;
const double eps = 1e-9;
void gmax(int &a, int b) { a = (a > b ? a : b); }
void gmin(int &a, int b) { a = (a < b ? a : b); }
const int maxn = 100050;
long long f[maxn][21], Cur;
int n, k, a[maxn], c[maxn];
struct state {
  int l, r, al, ar;
} h;
queue<state> Q;
int l, r, pos;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  l = r = 1;
  c[a[1]]++;
  for (int i = 1; i <= n; ++i) {
    while (r < i) {
      r++;
      c[a[r]]++;
      Cur += c[a[r]] - 1;
    }
    f[i][1] = Cur;
  }
  for (int j = 2; j <= k; ++j) {
    Q.push((struct state){1, n, 0, n});
    while (!Q.empty()) {
      h = Q.front();
      Q.pop();
      int mid = (h.l + h.r) >> 1;
      while (r < mid) {
        r++;
        c[a[r]]++;
        if (c[a[r]]) Cur += (c[a[r]] - 1);
      }
      while (r > mid) {
        Cur = Cur - c[a[r]] + 1;
        c[a[r]]--;
        --r;
      }
      f[mid][j] = 1ll << 61;
      for (int i = h.al; i <= min(mid, h.ar); ++i) {
        while (l < i + 1) {
          Cur = Cur - c[a[l]] + 1;
          c[a[l]]--;
          ++l;
        }
        while (l > i + 1) {
          --l;
          c[a[l]]++;
          Cur += c[a[l]] - 1;
        }
        if (f[mid][j] > f[i][j - 1] + Cur) {
          pos = i;
          f[mid][j] = f[i][j - 1] + Cur;
        }
      }
      if (h.l <= mid - 1) Q.push((struct state){h.l, mid - 1, h.al, pos});
      if (mid + 1 <= h.r) Q.push((struct state){mid + 1, h.r, pos, h.ar});
    }
  }
  printf("%lld\n", f[n][k]);
  return 0;
}
