#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int tl = 1, tr = 0;
long long ans = 0;
int buc[N], col[N];
long long dp[2][N];
int n, k;
long long calc(int l, int r) {
  while (tl > l) {
    int& T = buc[col[--tl]];
    ans += (T++);
  }
  while (tr < r) {
    int& T = buc[col[++tr]];
    ans += (T++);
  }
  while (tl < l) {
    int& T = buc[col[tl++]];
    ans -= (--T);
  }
  while (tr > r) {
    int& T = buc[col[tr--]];
    ans -= (--T);
  }
  return ans;
}
void solve(long long* f, long long* g, int lb, int rb, int l, int r) {
  if (l > r || lb > rb) return;
  int mid = (l + r) >> 1, d;
  for (int i = lb; i <= rb; i++) {
    long long res = g[i] + calc(i + 1, mid);
    if (res < f[mid]) f[mid] = res, d = i;
  }
  solve(f, g, lb, d, l, mid - 1);
  solve(f, g, d, rb, mid + 1, r);
}
int main() {
  memset(dp, 0x3f, sizeof(dp));
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &col[i]);
  dp[0][0] = 0;
  for (int i = 1; i <= k; i++) {
    memset(dp[i & 1], 0x3f, sizeof(dp[i & 1]));
    solve(dp[i & 1], dp[~i & 1], 0, n - 1, 1, n);
  }
  printf("%lld\n", dp[k & 1][n]);
  return 0;
}
