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 <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

#define APPEND(head,add)                    \
do {                                        \
  if (head) {                               \
      (add)->prev = (head)->prev;           \
      (add)->next = NULL;                   \
      (head)->prev->next = (add);           \
      (head)->prev = (add);                 \
  } else {                                  \
      (add)->prev = (add);                  \
      (add)->next = NULL;                   \
      (head)=(add);                         \
  }                                         \
} while (0)

#define DELETE(head,del)                    \
do {                                        \
  if ((del)->prev == (del)) {               \
      (head)=NULL;                          \
  } else if ((del)==(head)) {               \
      (del)->next->prev = (del)->prev;      \
      (head) = (del)->next;                 \
  } else {                                  \
      (del)->prev->next = (del)->next;      \
      if ((del)->next) {                    \
          (del)->next->prev = (del)->prev;  \
      } else {                              \
          (head)->prev = (del)->prev;       \
      }                                     \
  }                                         \
  free(del);                                \
} while (0)

#define FOREACH(head,start,el,tmp)     \
  for((el)=(start);                         \
      (el) && (tmp = (el)->next, 1);        \
      (el) = tmp)

typedef struct node_s {
    struct node_s *next, *prev;
    int v;
    int c;
    struct node_s *pc;
} node_t;

node_t *
remove_sequence(node_t *h, node_t *p, int cnt)
{
    node_t *i, *t;
    FOREACH(h, p, i, t) {
        if (!cnt)
            break;
        cnt--;
        DELETE(h, i);
    }

    FOREACH(h, h, i, t) {
        if (!cnt)
            break;
        cnt--;
        DELETE(h, i);
    }
    return h;
}

int64_t
reduce(int64_t n, int64_t k, int64_t m, node_t *h) 
{
    int64_t r = 0;
    node_t *i = h, // current node
           *t = h->next, // tmp for next node
           *p = h; // first node with current v
    int seq = 0;
    int changed = 1;
    while (n > k && changed) {
        changed = 0;
        // check k-sequences
        FOREACH(h, t, i, t) {
            if (i->v == p->v) {
                i->c = ++seq;
                i->pc = p;
            } else {
                seq = 0; // i->c = 0 and i->pc = i at initialize 
                p = i;
            }
            if (seq == k-1) // found k-sequence
                break;
        }

        if (seq == k-1) {
            // remove k-sequence
            h = remove_sequence(h, p, k);
            n -= k;
            // update p, seq and prob. t for next interation
            if (t) {
                p = t->prev->pc;
                seq = p->c;
            } 
          //else if (t == h) {
          //    // if we removed head, set t to second node
          //    t = h->next;
          //    p = h;
          //    seq = 0;
          //}
            changed = 1;
        }
    }
    changed = 1;
    while (n > k && changed) {
        changed = 0;
        // check sequences with overlap
        // check if m > 1
        if (m > 1 && n > k) {
            // p - one of last elements
            p = h->prev->pc;
            seq = p->c;
            FOREACH(h, h, i, t) {
                if (i->v == p->v)
                    seq++;
                else
                    break;
                if (seq == k-1)
                    break;
            }
            if (seq == k-1) {
                // overlapping from first and last: [a_1], ..., a_n, a_1, ..., ...., ..., [a_n]
                h = remove_sequence(h, p, k);
                r += k;
                n -= k;
                changed = 1;
            }
        }
    } 
    if (k >= n) {
        int cnt = 0;
        FOREACH(h, h, i, t) {
            if (i->v == h->v)
                cnt++;
            else
                break;
        }
        if (cnt == n) {
            n = n * m % k;
            m = 1;
        }
    }
    return n*m + r*(n != 0);
}

node_t *
new(int v) {
    node_t *e = malloc(sizeof *e);
    e->v = v;
    e->c = 0;
    e->pc = e;
    return e;
}

int
main()
{    
    int64_t n, k, m;
    node_t *h = NULL;

    scanf("%"SCNd64"%"SCNd64"%"SCNd64, &n, &k, &m);
    for (int64_t i = 0; i < n; ++i) {
        int v;
        scanf("%d", &v);
        node_t *e = new(v);
        APPEND(h, e);
    }

    printf("%"PRId64"\n", reduce(n, k, m, h));
    return 0;
}

