#include "assistant.h"
#include <queue>
using namespace std;
#define MAXN 100005
static int in_scaffold[MAXN],first_active[MAXN],request_active[MAXN];
static bool in_que[MAXN];
static queue<int> que;
void Assist(unsigned char *A, int N, int K, int R)
{
int i,j,k,p=0;
for (i=1;i<=K;i++) in_scaffold[i] = 1;
for (i=1;i<=K;i++){
first_active[i] = A[p++];
if (!first_active[i]) in_que[i] = 1, que.push(i);
}
for (i=1;i<=N;i++) request_active[i] = A[p++];
for (i=1;i<=N;i++){
int req=GetRequest()+1;
if (request_active[i]) in_que[req] = 0;
if (!request_active[i] && !in_que[req]) in_que[req] = 1, que.push(req);
if (in_scaffold[req]) continue;
for (;;){
k = que.front(); que.pop();
if (in_que[k]) break;
}
in_que[k] = 0;
in_scaffold[req] = 1;
in_scaffold[k] = 0;
PutBack(--k);
}
}
I2luY2x1ZGUgImFzc2lzdGFudC5oIgojaW5jbHVkZSA8cXVldWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIE1BWE4gMTAwMDA1CgpzdGF0aWMgaW50IGluX3NjYWZmb2xkW01BWE5dLGZpcnN0X2FjdGl2ZVtNQVhOXSxyZXF1ZXN0X2FjdGl2ZVtNQVhOXTsKc3RhdGljIGJvb2wgaW5fcXVlW01BWE5dOwpzdGF0aWMgcXVldWU8aW50PiBxdWU7Cgp2b2lkIEFzc2lzdCh1bnNpZ25lZCBjaGFyICpBLCBpbnQgTiwgaW50IEssIGludCBSKQp7CiAgICBpbnQgaSxqLGsscD0wOwogICAgZm9yIChpPTE7aTw9SztpKyspIGluX3NjYWZmb2xkW2ldID0gMTsKICAgIGZvciAoaT0xO2k8PUs7aSsrKXsKICAgICAgICBmaXJzdF9hY3RpdmVbaV0gPSBBW3ArK107CiAgICAgICAgaWYgKCFmaXJzdF9hY3RpdmVbaV0pIGluX3F1ZVtpXSA9IDEsIHF1ZS5wdXNoKGkpOwogICAgfQogICAgZm9yIChpPTE7aTw9TjtpKyspIHJlcXVlc3RfYWN0aXZlW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