#include "paint.h"
#include <bits/stdc++.h>
using namespace std;
string solve_puzzle(string s, vector<int> c) {
int K =c.size(), N =s.length();
vector<int> maxl(N,0),maxr(N,0);
for(int i =0; i < N; i++) {
if(s[i] == '_') continue;
if(i == 0) maxl[i] =1;
else maxl[i] =maxl[i-1]+1;}
for(int i =N-1; i >= 0; i--) {
if(s[i] == '_') continue;
if(i == N-1) maxr[i] =1;
else maxr[i] =maxr[i+1]+1;}
vector< vector<int> > lft(K+1,vector<int>(N+1,0)),rt(K+1,vector<int>(N+1,0));
lft[0][0] =rt[0][N] =1;
for(int i =0; i <= K; i++) for(int j =0; j < N; j++) {
if(s[j] != 'X' && lft[i][j]) lft[i][j+1] =1;
if(i == K || maxl[j] < c[i]) continue;
if(j == N-1) {
if(lft[i][j+1-c[i]]) lft[i+1][j+1] =1;
}
else {
if(s[j+1] == 'X') continue;
if(lft[i][j+1-c[i]]) lft[i+1][j+2] =1;}
}
for(int i =0; i <= K; i++) for(int j =N-1; j >= 0; j--) {
if(s[j] != 'X' && rt[i][j+1]) rt[i][j] =1;
if(i == K || maxr[j] < c[K-1-i]) continue;
if(j == 0) {
if(rt[i][j+c[K-1-i]]) rt[i+1][j] =1;
}
else {
if(s[j-1] == 'X') continue;
if(rt[i][j+c[K-1-i]]) rt[i+1][j-1] =1;}
}
string ans =s;
vector<int> isw(N,0),isb(N,0);
// can be white?
for(int i =1; i < N-1; i++) if(s[i] == '.')
for(int j =0; j <= K; j++) if(lft[j][i+1] && rt[K-j][i]) isw[i] =1;
if(rt[K][1] || (N > 1 && maxr[1] >= c[0] && rt[K-1][c[0]+1])) isw[0] =1;
if(lft[K][N-1] || (N > 1 && maxl[N-2] >= c[K-1] && lft[K-1][N-1-c[K-1]])) isw[N-1] =1;
// can be black?
vector<int> maxbr(N,0);
for(int i =0; i < N; i++) for(int j =0; j < K; j++) if(maxr[i] >= c[j])
if(lft[j][i] && rt[K-j-1][i+c[j]]) maxbr[i] =max(maxbr[i],c[j]);
for(int i =0; i < N-1; i++) if(maxbr[i] > 0) maxbr[i+1] =max(maxbr[i+1],maxbr[i]-1);
for(int i =0; i < N; i++) if(s[i] == '.' && maxbr[i] > 0) isb[i] =1;
for(int i =0; i < N; i++) if(s[i] == '.') {
if(isw[i] == 0) ans[i] ='X';
else if(isb[i] == 0) ans[i] ='_';
if(ans[i] == '.') ans[i] ='?';}
return ans;}