#include using namespace std; using ll = long long; // ===== XOR Basis (20-bit) ===== struct XorBasis { static const int B = 20; int v[B]; vector< vector > idxs[B]; // store list of indices forming the vector (instead of bitset to stay flexible) XorBasis(){ memset(v,0,sizeof(v)); } void add(int x, const vector& cells){ vector c = cells; for(int b=B-1;b>=0;--b){ if(((x>>b)&1)==0) continue; if(v[b]==0){ v[b]=x; idxs[b]= {c}; return; } x ^= v[b]; // xor index lists (symmetric difference) vector nc; nc.reserve(c.size()+idxs[b][0].size()); // because both lists are small (<=100) we can brute force auto &v1 = c; auto &v2 = idxs[b][0]; sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); size_t i=0,j=0; while(i cancel ++i; ++j; } } c.swap(nc); } } // build target -> {built_value, indices} pair> build(int target) const{ int t=target, made=0; vector sel; for(int b=B-1;b>=0;--b){ if(((t>>b)&1)==0) continue; if(v[b]==0) continue; t ^= v[b]; made ^= v[b]; // xor lists vector nc; nc.reserve(sel.size() + idxs[b][0].size()); auto v1 = sel; auto v2 = idxs[b][0]; sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); size_t i=0,j=0; while(i& A, int s){ long long res=0; for(int x:A){ int y=x^s; res += (y>x?y:x);} return res; } inline void move_to(int &r,int &c,int tr,int tc, vector& ops){ while(rtr){ ops.push_back('U'); --r; } while(ctc){ ops.push_back('L'); --c; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,T; if(!(cin>>N>>T)) return 0; vector> A2(N, vector(N)); for(int i=0;i>A2[i][j]; const int NN=N*N; vector Aflat(NN); for(int i=0;i{idx}); } // 1) best single s (exact exhaustive) int s1=0; long long best1=-1; for(int s=0;s<(1<<20);++s){ long long sc=score_single(Aflat,s); if(sc>best1){best1=sc;s1=s;} } auto [s1_real, cset1] = B1.build(s1); if(s1_real!=s1){ long long sc=score_single(Aflat,s1_real); if(sc>=best1){best1=sc;s1=s1_real;} } if(s1_real!=s1){ auto tmp=B1.build(s1); cset1=tmp.second; } // base per cell after stage1 best of {A, A^s1} vector baseVal(NN); long long baseSum=0; for(int i=0;ia?b:a); baseSum+=baseVal[i]; } // 2) Find s2 by exhaustive to maximize 4-way value int s2=-1; long long gainBest=0; for(int s=0;s<(1<<20);++s){ long long sum=0; for(int i=0;im)m=v1; if(v2>m)m=v2; sum+=m; } long long g=sum-baseSum; if(g>gainBest){gainBest=g; s2=s;} } bool use2 = (gainBest>0); vector choice(NN,0); // 0 none,1 stage1W,2 stage2W,3 both if(!use2){ for(int i=0;ia?1:0); } }else{ // map s2 to achievable after stage1 W changes, so we need to rebuild basis later // First decide ideal masks with target s2 for(int i=0;ibest){best=v1;m=1;} if(v2>best){best=v2;m=2;} if(v3>best){best=v3;m=3;} choice[i]=m; } } // === Simulate stage1 W to know board for second basis === vector Aafter = Aflat; if(use2){ for(int i=0;is2 vector csetDelta; int deltaReal=0; int deltaTarget=0; if(use2){ deltaTarget = s1 ^ s2; XorBasis B2; for(int i=0;i{idx}); } auto [dReal, csetD] = B2.build(deltaTarget); deltaReal=dReal; csetDelta=csetD; int s2_real = s1 ^ deltaReal; if(s2_real!=s2){ // recompute gain and masks with s2_real to ensure non-decreasing long long sum=0; vector newChoice(NN); for(int i=0;ibest){best=v1;m=1;} if(v2>best){best=v2;m=2;} if(v3>best){best=v3;m=3;} newChoice[i]=m; sum+=best; } if(sum<=baseSum){ use2=false; choice.assign(NN,0); for(int i=0;ia?1:0);} } else{ choice.swap(newChoice); gainBest=sum-baseSum; s2=s2_real; } } } // ===== Build operation sequence ===== vector ops; int r=0,c=0; vector> snake; snake.reserve(NN); for(int i=0;i=0;j--) snake.emplace_back(i,j); } auto output_C = [&](const vector& cells){ // we'll simply visit along snake and C when needed vector mark(NN,0); for(int id:cells) mark[id]=1; if(!snake.empty()){ move_to(r,c,snake[0].first,snake[0].second,ops); for(size_t k=0;k0) move_to(r,c,rr,cc,ops); int id=rr*N+cc; if(choice[id]&1) ops.push_back('W'); } } if(use2){ // Phase C: move delta C's output_C(csetDelta); // Phase D: stage2 writes if(!snake.empty()){ for(size_t k=0;k0) move_to(r,c,rr,cc,ops); int id=rr*N+cc; if(choice[id]&2) ops.push_back('W'); } } } if((int)ops.size()>T) ops.resize(T); for(char ch:ops) cout<