#include using namespace std; using ll = long long; // ===== XOR Basis (20-bit) ===== struct XorBasis { static const int B = 20; int v[B]; vector mask[B]; 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]){ v[b]=x; mask[b]=c; return; } x ^= v[b]; // symmetric difference of two sorted vectors vector a = mask[b], res; res.reserve(c.size()+a.size()); sort(c.begin(),c.end()); sort(a.begin(),a.end()); size_t i=0,j=0; while(i> 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]) continue; t ^= v[b]; made ^= v[b]; vector a = mask[b], res; res.reserve(sel.size()+a.size()); sort(sel.begin(),sel.end()); sort(a.begin(),a.end()); size_t i=0,j=0; while(i& A, int s){ ll sum=0; for(int x:A){ int y=x^s; sum += (y>x?y:x);} return sum; } static 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; // N=10, T=1000 vector> A2(N, vector(N)); for(int i=0;i>A2[i][j]; const int NN=N*N; // flatten board vector board(NN); for(int i=0;i> snake; snake.reserve(NN); for(int i=0;i=0;j--) snake.emplace_back(i,j); } vector ops; ops.reserve(2000); int r=0,c=0; // current pos auto do_C = [&](const vector& need){ vector mark(NN,0); for(int id:need) mark[id]=1; if(snake.empty()) return; move_to(r,c,snake[0].first,snake[0].second,ops); for(size_t k=0;k& take, int s){ if(snake.empty()) return; for(size_t k=0;k0) move_to(r,c,rr,cc,ops); int id=rr*N+cc; if(take[id]){ ops.push_back('W'); board[id]^=s; } } }; // current s held by the cursor int curS = 0; // time/ops guard auto start = chrono::steady_clock::now(); const double TL = 1.85; // seconds while(true){ // stop if we cannot afford another full pass if((int)ops.size() + 2*NN + 200 >= T) break; double t = chrono::duration(chrono::steady_clock::now()-start).count(); if(t > TL) break; // base sum ll baseSum=0; for(int v:board) baseSum+=v; // ---- choose best s for current board (full search) ---- int targetS=-1; ll bestGain=0; // gain over baseSum for(int s=0;s<(1<<20);++s){ ll tot=0; for(int x:board){ int y=x^s; tot += (y>x?y:x); } ll g = tot - baseSum; if(g>bestGain){ bestGain=g; targetS=s; } } if(bestGain<=0) break; // no improvement left // ---- realize diff on current board ---- int diff = curS ^ targetS; // build basis from current board values (before W) XorBasis B; for(int i=0;i{idx}); } auto [diffReal, needC] = B.build(diff); int newS = curS ^ diffReal; // recompute take mask with realizable newS and check gain vector take(NN,0); ll afterSum=0; for(int id=0; ida){ take[id]=1; afterSum+=b; } else afterSum+=a; } if(afterSum <= baseSum) break; // no actual gain // emit ops do_C(needC); curS = newS; do_W(take, curS); } if((int)ops.size()>T) ops.resize(T); for(char ch: ops) cout<