#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class c>
  typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }
  template <class c, class b>
  debug &operator<<(pair<b, c> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  template <class c>
  debug &operator<<(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
};
const long double eps = 1e-9;
const int N = 1e5 + 5;
const long long inf = 1e18;
auto rnd = bind(uniform_int_distribution<int>(1, 5), mt19937(time(0)));
bool umin(long long &a, long long b) {
  if (a > b) {
    a = b;
    return 1;
  } else {
    return 0;
  }
}
bool umax(int &a, int b) {
  if (a > b) {
    a = b;
    return 1;
  } else {
    return 0;
  }
}
int n, k;
int L = 0, R = -1;
int cnt[N];
int a[N];
long long res = 0;
long long get(int l, int r) {
  while (L < l) {
    cnt[a[L]]--;
    res -= cnt[a[L]];
    L++;
  }
  while (L > l) {
    res += cnt[a[L - 1]];
    cnt[a[L - 1]]++;
    L--;
  }
  while (R > r) {
    cnt[a[R]]--;
    res -= cnt[a[R]];
    R--;
  }
  while (R < r) {
    res += cnt[a[R + 1]];
    cnt[a[R + 1]]++;
    R++;
  }
  return res;
}
long long dp[N][21];
void calc(int j, int l, int r, int x, int y) {
  if (l > r) return;
  int mid = (l + r) >> 1;
  int pos = x;
  dp[mid][j] = inf;
  for (int opt = x; opt <= min(mid - 1, y); opt++) {
    if (dp[mid][j] > get(opt + 1, mid) + dp[opt][j - 1]) {
      pos = opt;
      dp[mid][j] = get(opt + 1, mid) + dp[opt][j - 1];
    }
  }
  if (l == r) return;
  calc(j, l, mid - 1, x, pos);
  calc(j, mid + 1, r, pos, y);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) dp[i][j] = inf;
  }
  for (int i = 0; i < n; i++) dp[i][1] = get(0, i);
  for (int j = 2; j <= k; j++) {
    calc(j, 0, n - 1, 0, n - 1);
  }
  cout << dp[n - 1][k];
  return 0;
}
