#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
long long dp[25][maxn];
int a[maxn];
long long curans = 0, cnt[maxn];
int n, k;
int ql, qr;
inline long long f(long long x) { return x * (x - 1) / 2; }
inline void work(int dep, int vl, int vr, int il, int ir) {
  if (il > ir || vl > vr) return;
  int mid = (il + ir) / 2;
  int p = 0;
  long long res = 1e10;
  for (int i = vl; i <= vr && i <= mid; i++) {
    while (ql < i) {
      curans -= f(cnt[a[ql]]);
      cnt[a[ql]]--;
      curans += f(cnt[a[ql]]);
      ql++;
    }
    while (ql > i) {
      ql--;
      curans -= f(cnt[a[ql]]);
      cnt[a[ql]]++;
      curans += f(cnt[a[ql]]);
    }
    while (qr < mid) {
      qr++;
      curans -= f(cnt[a[qr]]);
      cnt[a[qr]]++;
      curans += f(cnt[a[qr]]);
    }
    while (qr > mid) {
      curans -= f(cnt[a[qr]]);
      cnt[a[qr]]--;
      curans += f(cnt[a[qr]]);
      qr--;
    }
    long long tp = curans + dp[dep - 1][i - 1];
    if (tp < res) {
      res = tp;
      p = i;
    }
  }
  dp[dep][mid] = res;
  work(dep, vl, p, il, mid - 1);
  work(dep, p, vr, mid + 1, ir);
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  cnt[a[1]]++;
  ql = qr = 1;
  curans = 0;
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 1; i <= k; i++) {
    work(i, 1, n, 1, n);
  }
  cout << dp[k][n] << endl;
}
