This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define ft first
#define sd second
using namespace std;
typedef long long ll;
const int N = 5010;
const int oo = int(2e9);
const ll OO = 1e18;
const int md = int(1e9) + 7;
short fen[N][N], net[N], tp[N], n, m, k, pre[N][N], mem[N][N];
void upd(int id, int ps){
    for (; ps < m; ps = (ps | (ps + 1)))
        fen[id][ps]++;
}
int sum(int id, int ps){
    int res = 0;
    for (; ps >= 0; ps = (ps & (ps + 1)) - 1)
        res += fen[id][ps];
    return res;
}
short get(short id, short vl){
    if (pre[id][vl] == vl)
        return vl;
    else return (pre[id][vl] = get(id, pre[id][vl]));
}
int get(int ps, int vl){
    if (mem[ps][vl] > 0) return mem[ps][vl] - 1;
    if (vl == m - 1) {
        mem[ps][vl] = 1;
        return 0;
    }
    int res = 0;
    int rt = min(vl + k, m - 1);
    while (1){
        int nw = get(net[ps], pre[net[ps]][rt]);
        if (nw <= vl) break;
        mem[net[ps]][nw] = get(net[ps], nw) + 1;
        if (mem[net[ps]][nw] == 2)
            upd(net[ps], nw);
        pre[net[ps]][nw] = get(net[ps], pre[net[ps]][nw - 1]);
    }
    if (tp[net[ps]] == tp[ps]){
        if (sum(net[ps], rt) - sum(net[ps], vl) > 0)
            res = 1;
    } else {
        if (sum(net[ps], rt) - sum(net[ps], vl) < rt - vl)
            res = 1;
    }
    mem[ps][vl] = res + 1;
    return res;
}
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL
    cin >> n >> m >> k;
    k = min(m, k);
    for (int i = 0; i < n; i++) {
        cin >> tp[i];
        net[i] = (i + 1) % n;
        for (int j = 0; j <= m; j++)
            pre[i][j] = j;
    }
    for (int i = 0; i < n; i++){
        int ans = 0;
        ans = get(i, 0);
        cout << (tp[i] ^ ans ^ 1) << " ";
    }
    return 0;
}
Compilation message (stderr)
vode.cpp: In function 'int get(int, int)':
vode.cpp:48:43: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
         mem[net[ps]][nw] = get(net[ps], nw) + 1;
                                           ^
vode.cpp:31:5: note: candidate 1: int get(int, int)
 int get(int ps, int vl){
     ^~~
vode.cpp:25:7: note: candidate 2: short int get(short int, short int)
 short get(short id, short vl){
       ^~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |