#include <bits/stdc++.h>
using namespace std;
int n, k, lnow = 1, rnow;
int fr[100005], a[100005];
long long cnow;
long long dp[2][100005];
void add(int val, int amo) {
  if (amo > 0) {
    cnow += fr[val];
    fr[val]++;
  } else {
    fr[val]--;
    cnow -= fr[val];
  }
}
void bring(int x, int y) {
  while (lnow > x) {
    lnow--;
    add(a[lnow], 1);
  }
  while (lnow < x) {
    add(a[lnow], -1);
    lnow++;
  }
  while (rnow < y) {
    rnow++;
    add(a[rnow], 1);
  }
  while (rnow > y) {
    add(a[rnow], -1);
    rnow--;
  }
}
void solve(int bas, int son, int L, int R) {
  int tut = -1;
  if (L == -1 || R == -1 || bas > son) {
    return;
  }
  dp[1][((bas + son) / 2)] = 10000000000000009;
  bring(min(R + 1, ((bas + son) / 2)), ((bas + son) / 2));
  for (int i = min(((bas + son) / 2) - 1, R); i >= L; i--) {
    if (dp[1][((bas + son) / 2)] > dp[0][i] + cnow) {
      dp[1][((bas + son) / 2)] = dp[0][i] + cnow;
      tut = i;
    }
    lnow--;
    add(a[i], 1);
  }
  solve(bas, ((bas + son) / 2) - 1, L, tut);
  solve(((bas + son) / 2) + 1, son, tut, R);
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i <= n; i++) {
    dp[0][i] = dp[1][i] = 10000000000000009;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= k; i++) {
    solve(1, n, 0, n - 1);
    for (int j = 1; j <= n; j++) {
      swap(dp[0][j], dp[1][j]);
    }
  }
  printf("%lld", dp[0][n]);
}
