#include <bits/stdc++.h>
namespace IO {
char buf[1 << 20 | 1], *f1 = buf, *f2 = buf, pbuf[1 << 24 | 1], *f3 = pbuf,
                       sta[100];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
template <class T>
inline void write(T x) {
  if (!x) return (void)(*f3++ = 48, *f3++ = ' ');
  if (x < 0) *f3++ = '-', x = ~x + 1;
  char *top = sta;
  while (x) *top++ = x % 10 + 48, x /= 10;
  while (top != sta) *f3++ = *--top;
  *f3++ = ' ';
}
inline void put(std::string s) {
  int len = s.length();
  for (int i = 0; i < len; i++) *f3++ = s[i];
  *f3++ = ' ';
}
inline void endline() { *f3++ = '\n'; }
template <class T>
inline T abs(T a) {
  return a < 0 ? -a : a;
}
template <class T>
inline T max(T a, T b) {
  return a > b ? a : b;
}
template <class T>
inline T min(T a, T b) {
  return a < b ? a : b;
}
template <class T>
inline void swap(T &a, T &b) {
  T t = a;
  a = b;
  b = t;
}
}  // namespace IO
using namespace IO;
const int N = 1e5 + 5;
int n, m, a[N], cnt[N], L, R, em;
long long f[25][N], ans;
void get(int val, int is) { ans += is * (1ll * cnt[val] * (cnt[val] - 1) / 2); }
long long calc(int l, int r) {
  while (L < l) get(a[L], -1), cnt[a[L]]--, get(a[L], 1), L++;
  while (L > l) L--, get(a[L], -1), cnt[a[L]]++, get(a[L], 1);
  while (R < r) R++, get(a[R], -1), cnt[a[R]]++, get(a[R], 1);
  while (R > r) get(a[R], -1), cnt[a[R]]--, get(a[R], 1), R--;
  return ans;
}
void solve(int x, int y, int l, int r) {
  if (x > y || l > r) return;
  int mid = (l + r) >> 1, pos = 0;
  long long res = 1e18;
  for (int i = x; i <= y; i++) {
    long long tmp = calc(i + 1, mid);
    if (f[em - 1][i] + tmp < res) res = f[em - 1][i] + tmp, pos = i;
  }
  f[em][mid] = res;
  solve(x, pos, l, mid - 1);
  solve(pos, y, mid + 1, r);
}
int main() {
  n = read();
  m = read();
  memset(f, 0x3f, sizeof(f));
  f[0][0] = 0;
  for (int i = 1; i <= n; i++) a[i] = read();
  cnt[a[1]]++;
  L = R = 1;
  for (int i = 1; i <= m; i++) em = i, solve(0, n - 1, 1, n);
  std::cout << f[m][n] << std::endl;
  return 0;
}
