#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
void chkmin(T1 &x, T2 y) {
  if (x > y) x = y;
}
template <typename T1, typename T2>
void chkmax(T1 &x, T2 y) {
  if (x < y) x = y;
}
namespace fastio {
char rbuf[1 << 23], *p1 = rbuf, *p2 = rbuf, wbuf[1 << 23], *p3 = wbuf;
inline char getc() {
  return p1 == p2 &&
                 (p2 = (p1 = rbuf) + fread(rbuf, 1, 1 << 23, stdin), p1 == p2)
             ? -1
             : *p1++;
}
inline void putc(char x) { (*p3++ = x); }
template <typename T>
void read(T &x) {
  x = 0;
  char c = getchar();
  T neg = 0;
  while (!isdigit(c)) neg |= !(c ^ '-'), c = getchar();
  while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  if (neg) x = (~x) + 1;
}
template <typename T>
void recursive_print(T x) {
  return (!x) ? void() : (recursive_print(x / 10), putc(x % 10 ^ 48), void());
}
template <typename T>
void print(T x) {
  (!x) && (putc('0'), 0);
  (x < 0) && (putc('-'), x = ~x + 1);
  recursive_print(x);
}
template <typename T>
void print(T x, char c) {
  (!x) && (putc('0'), 0);
  (x < 0) && (putc('-'), x = ~x + 1);
  recursive_print(x);
  putc(c);
}
void print_final() { fwrite(wbuf, 1, p3 - wbuf, stdout); }
}  // namespace fastio
const int MAXN = 1e5;
const int MAXK = 20;
int n, k, a[MAXN + 5];
long long dp[MAXN + 5][MAXK + 3];
int cl = 1, cr = 0, buc[MAXN + 5];
long long sum = 0;
void push(int x) {
  sum -= 1ll * buc[a[x]] * (buc[a[x]] - 1) >> 1;
  buc[a[x]]++;
  sum += 1ll * buc[a[x]] * (buc[a[x]] - 1) >> 1;
}
void pop(int x) {
  sum -= 1ll * buc[a[x]] * (buc[a[x]] - 1) >> 1;
  buc[a[x]]--;
  sum += 1ll * buc[a[x]] * (buc[a[x]] - 1) >> 1;
}
long long calc(int l, int r) {
  while (cr < r) push(++cr);
  while (cl > l) push(--cl);
  while (cl < l) pop(cl++);
  while (cr > r) pop(cr--);
  return sum;
}
void solve(int l, int r, int pl, int pr, int j) {
  if (l > r || pl > pr) return;
  int mid = l + r >> 1, pos = -1;
  for (int i = pl; i <= min(mid - 1, pr); i++) {
    if (dp[mid][j] > dp[i][j - 1] + calc(i + 1, mid))
      dp[mid][j] = dp[i][j - 1] + calc(i + 1, mid), pos = i;
  }
  solve(l, mid - 1, pl, pos, j);
  solve(mid + 1, r, pos, pr, j);
}
int main() {
  scanf("%d%d", &n, &k);
  memset(dp, 63, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= k; i++) solve(1, n, 0, n, i);
  printf("%lld\n", dp[n][k]);
  return 0;
}
