10 5 1000000000
1 1 1 2 2 1 2 2 2 2
 5 3 3
1 2 2 1 1
 2 4 1
1 1
 1 1000000000 1000000000
100000
 5 3 2
1 1 2 1 1
 5 2 4
1 2 3 2 1
 1 10 10
1

10000000000
 0
 2
 0
 7
 0
 0

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

vector<pair<int, int>> packs;
int n;
ll k, m;

vector<pair<int, int>> get_center(const vector<pair<int, int>>& v) {
    int i;
    for (i = 0; i < ((int)v.size() + 1) / 2; ++i) {
        if (v[i].first != v[v.size() - i - 1].first || (ll)v[i].second + v[v.size() - i - 1].second != k)
            break;
    }
    vector<pair<int, int>> res;
    for (int j = i; j < (int)v.size() - i; ++j)
        res.push_back(v[j]);
    return res;
}

bool solve() {
    cin >> n >> k >> m;
    int last_city = 0;
    int last_count = 0;
    for (int i = 0; i < n; ++i) {
        int c;
        scanf("%d", &c);
        if (c == last_city) {
            ++last_count;
            if (last_count == k) {
                if (packs.empty()) {
                    last_count = 0;
                    last_city = 0;
                } else {
                    last_city = packs.back().first;
                    last_count = packs.back().second;
                    packs.pop_back();
                }
            }
        } else {
            if (last_city)
                packs.push_back({last_city, last_count});
            last_city = c;
            last_count = 1;
        }
    }
    if (last_city)
        packs.push_back({last_city, last_count});
    if (packs.empty()) {
        cout << 0 << endl;
        return 0;
    }
    auto closure = [](ll acc, auto p) {
        return acc + p.second;
    };
    if (m == 1) {
        cout << accumulate(packs.begin(), packs.end(), 0ll, closure) << endl;
        return 0;
    }
    auto center = get_center(packs);
    if (center.empty())
        cout << 0 << endl;
    else if (center.size() == 1) {
        cout << accumulate(packs.begin(), packs.end(), 0ll, closure) -
            center[0].second +
            center[0].second * m % k << endl;
    } else if (center.front().first == center.back().first) {
        cout << accumulate(packs.begin(), packs.end(), 0ll, closure) -
            accumulate(center.begin(), center.end(), 0ll, closure) +
            accumulate(center.begin() + 1, center.end() - 1, 0ll, closure) * m +
            center.front().second + center.back().second + (center.front().second + center.back().second) % k * (m - 1) << endl;
    } else {
        cout << accumulate(packs.begin(), packs.end(), 0ll, closure) +
            accumulate(center.begin(), center.end(), 0ll, closure) * (m - 1) << endl;
    }

    return 0;
}

int main() {
#ifdef LOCALHOST
    freopen("input", "r", stdin);
#endif
    while (solve());
    
    return 0;
}
