#include <bits/stdc++.h>
using namespace std;
int n, K, nums[100005], pl = 1, pr, a[100005];
long long res;
inline void getin(int x) {
  res += nums[x];
  ++nums[x];
}
inline void getout(int x) {
  --nums[x];
  res -= nums[x];
}
inline long long calc(int l, int r) {
  while (pr < r) getin(a[++pr]);
  while (pl > l) getin(a[--pl]);
  while (pl < l) getout(a[pl++]);
  while (pr > r) getout(a[pr--]);
  return res;
}
void solve(int l1, int r1, int l2, int r2, long long *g, long long *f) {
  int mid = (l2 + r2) >> 1;
  long long now;
  int pos = -1;
  for (int i = l1; i <= min(r1, mid - 1); ++i) {
    long long tmp = g[i] + calc(i + 1, mid);
    if (tmp < now || pos == -1) now = tmp, pos = i;
  }
  f[mid] = now;
  if (l2 < mid) solve(l1, pos, l2, mid - 1, g, f);
  if (mid < r2) solve(pos, r1, mid + 1, r2, g, f);
}
long long f[100005], g[100005];
int main() {
  cin >> n >> K;
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  memset(f, 0x3f, sizeof f), f[0] = 0;
  for (int i = 1; i <= K; ++i) swap(g, f), solve(0, n, 1, n, g, f);
  cout << f[n] << endl;
  return 0;
}
