#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int l, r;
long long cnt[N];
long long now;
int a[N];
long long calc(int L, int R) {
  while (r < R) {
    ++r;
    now -= cnt[a[r]] * (cnt[a[r]] - 1) / 2;
    cnt[a[r]]++;
    now += cnt[a[r]] * (cnt[a[r]] - 1) / 2;
  }
  while (r > R) {
    now -= cnt[a[r]] * (cnt[a[r]] - 1) / 2;
    cnt[a[r]]--;
    now += cnt[a[r]] * (cnt[a[r]] - 1) / 2;
    r--;
  }
  while (l > L) {
    --l;
    now -= cnt[a[l]] * (cnt[a[l]] - 1) / 2;
    cnt[a[l]]++;
    now += cnt[a[l]] * (cnt[a[l]] - 1) / 2;
  }
  while (l < L) {
    now -= cnt[a[l]] * (cnt[a[l]] - 1) / 2;
    cnt[a[l]]--;
    now += cnt[a[l]] * (cnt[a[l]] - 1) / 2;
    l++;
  }
  return now;
}
int k;
long long dp[N][22];
void solve(int l, int r, int L, int R) {
  if (r < l || R < L) return;
  int m = (l + r) >> 1;
  int p;
  for (int i = L; i <= min(R, m - 1); i++) {
    if (dp[m][k] > dp[i][k - 1] + calc(i + 1, m)) {
      dp[m][k] = dp[i][k - 1] + calc(i + 1, m);
      p = i;
    }
  }
  solve(l, m - 1, L, p);
  solve(m + 1, r, p, R);
}
int main() {
  int n, kk;
  cin >> n >> kk;
  now = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  l = 1, r = 0;
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 1; i <= kk; i++) {
    k = i;
    solve(1, n, 0, n - 1);
  }
  cout << dp[n][kk];
  return 0;
}
