#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const long double eps = 1e-6;
const int mod = 1e9 + 7;
const int maxn = 20;
const int N = 1e5 + 100;
const int M = 3e5 + 10;
const long long mm = (1LL << 32);
template <class T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= c == '-';
  for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
  x = f ? -x : x;
}
template <class T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  T y = 1;
  int len = 1;
  for (; y <= x / 10; y *= 10) ++len;
  for (; len; --len, x %= y, y /= 10) putchar(x / y + 48);
}
long long qpow(long long a, long long b, long long mod) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % mod;
    b >>= 1;
    a = (a * a) % mod;
  }
  return ans;
}
int n, k, tax[N], ql = 1, qr;
long long a[N], f[N][21], sum;
inline void ins(int x) {
  sum += tax[x];
  ++tax[x];
}
inline void del(int x) {
  --tax[x];
  sum -= tax[x];
}
inline long long w(int l, int r) {
  while (ql > l) ins(a[--ql]);
  while (ql < l) del(a[ql++]);
  while (qr > r) del(a[qr--]);
  while (qr < r) ins(a[++qr]);
  return sum;
}
inline void calc(int l, int r, int dl, int dr, int now) {
  if (l > r) return;
  int mid = l + r >> 1, dmid = dl;
  for (int i = min(mid, dr); i >= dl; --i) {
    long long tmp = f[i - 1][now - 1] + w(i, mid);
    if (f[mid][now] >= tmp) f[mid][now] = tmp, dmid = i;
  }
  calc(l, mid - 1, dl, dmid, now);
  calc(mid + 1, r, dmid, dr, now);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  memset(f, 0x3f, sizeof f);
  f[0][0] = 0;
  for (int i = 1; i <= k; i++) calc(1, n, 1, n, i);
  printf("%lld\n", f[n][k]);
  return 0;
}
