#include <bits/stdc++.h>   using namespace std;   const int N = 35000, K = 50;   struct seg_node {     seg_node *lc, *rc;     int l, r, f, m;     seg_node(seg_node *lc, seg_node *rc, int l, int r)         : lc(lc), rc(rc), l(l), r(r), f(), m() {}     static seg_node *build(int l, int r) {       if (l == r - 1) return new seg_node(nullptr, nullptr, l, r);       int mid = (l + r) / 2;       return new seg_node(build(l, mid), build(mid, r), l, r);     }     void add(int val) {       f += val;       m += val;     }     void push_down() {       if (f > 0) {         lc->add(f);         rc->add(f);         f = 0;       }     }     void maintain() { m = max({m, lc->m, rc->m}); }     void add(int ql, int qr, int val) {       if (ql == l && qr == r)         add(val);       else {         push_down();         int mid = (l + r) / 2;         if (qr <= mid)           lc->add(ql, qr, val);         else if (ql >= mid)           rc->add(ql, qr, val);         else {           lc->add(ql, mid, val);           rc->add(mid, qr, val);         }         maintain();       }     }     int query(int ql, int qr) {       if (ql == l && qr == r)         return m;       else {         push_down();         int mid = (l + r) / 2;         if (qr <= mid)           return lc->query(ql, qr);         else if (ql >= mid)           return rc->query(ql, qr);         else           return max(lc->query(ql, mid), rc->query(mid, qr));       }     }   } * t[K + 1];   int a[N], last[N];   int main() {     ios::sync_with_stdio(false);     int n, k;     cin >> n >> k;     int cnt = 0;     t[1] = seg_node::build(0, n);     memset(last, 0xff, sizeof last);     for (int i = 0; i < n; i++) {       cin >> a[i];       a[i]--;       if (last[a[i]] == -1) {         cnt++;         last[a[i]] = i;       }       t[1]->add(i, i + 1, cnt);     }     for (int i = 2; i <= k; i++) {       t[i] = seg_node::build(0, n);       memset(last, 0xff, sizeof last);       for (int j = i - 1; j < n; j++) {         t[i - 1]->add(last[a[j]] == -1 ? i - 2 : last[a[j]], j, 1);         t[i]->add(j, j + 1, t[i - 1]->query(i - 2, j));         last[a[j]] = j;       }     }     cout << t[k]->query(n - 1, n);   }   