#include using namespace std; struct Cell { int x, y; }; struct Transform { int R, F; }; // reflect (y-axis) then rotate R*90° CW struct Oriented { vector pts; // normalized: minx=miny=0 int w, h; int offx, offy; // = -minx, -miny (to undo normalization for output) Transform tf; }; struct Piece { int id; vector base; vector variants; int area; }; static inline uint64_t pack_xy(int x,int y){ return (uint64_t(uint32_t(x))<<32) | uint32_t(y); } static inline pair apply_transform(int x,int y,int F,int R){ if (F) x = -x; switch (R & 3){ case 0: return { x, y}; case 1: return { y, -x}; // 90 CW case 2: return {-x, -y}; // 180 default:return {-y, x}; // 270 CW } } static Oriented make_oriented(const vector& base,int F,int R){ vector t; t.reserve(base.size()); int minx=INT_MAX,miny=INT_MAX,maxx=INT_MIN,maxy=INT_MIN; for (auto &c: base){ auto [tx,ty]=apply_transform(c.x,c.y,F,R); minx=min(minx,tx); miny=min(miny,ty); maxx=max(maxx,tx); maxy=max(maxy,ty); t.push_back({tx,ty}); } // normalization for (auto &c: t){ c.x -= minx; c.y -= miny; } int W = maxx-minx+1, H = maxy-miny+1; sort(t.begin(), t.end(), [](const Cell&a,const Cell&b){ if (a.y!=b.y) return a.y cand; cand.reserve(8); for (int F=0;F<=1;++F) for (int R=0;R<4;++R) cand.push_back(make_oriented(P.base,F,R)); // dedup vector uniq; for (auto &o: cand){ bool dup=false; for (auto &u: uniq){ if (u.w==o.w && u.h==o.h && u.pts.size()==o.pts.size()){ bool same=true; for (size_t i=0;i place; }; struct World { int W, Hlimit; // square side S; W==Hlimit==S vector height; unordered_set occ; int maxH=0; World(int S=1){ clear(S); } void clear(int S){ W=S; Hlimit=S; height.assign(W,0); occ.clear(); maxH=0; } inline int shelfY(int x,int w) const { int y=0; for (int i=0;i W) return false; if (Y + o.h > Hlimit) return false; // enforce square height cap for (auto &c: o.pts){ int gx=X+c.x, gy=Y+c.y; if (occ.find(pack_xy(gx,gy))!=occ.end()) return false; } return true; } inline void do_place(const Oriented& o,int X,int Y){ for (auto &c: o.pts){ int gx=X+c.x, gy=Y+c.y; occ.insert(pack_xy(gx,gy)); height[gx]=max(height[gx], gy+1); if (height[gx]>maxH) maxH=height[gx]; } } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; if(!(cin>>n)) return 0; vector P(n); long long totalCells=0; for (int i=0;i>k; P[i].base.resize(k); for (int j=0;j>x>>y; P[i].base[j]={x,y}; } P[i].area=k; totalCells+=k; build_variants(P[i]); } // Place larger / less flexible first vector order(n); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int a,int b){ if (P[a].area!=P[b].area) return P[a].area>P[b].area; return P[a].variants.size() sides = { S0, max(S0, (int)ceil(1.05*S0)), max(S0, (int)ceil(1.15*S0)), max(S0, (int)ceil(0.95*S0)) }; sort(sides.begin(), sides.end()); sides.erase(unique(sides.begin(), sides.end()), sides.end()); auto better = [](const PackedSolution& A, const PackedSolution& B){ if (B.W==0) return true; long long aA=1LL*A.W*A.H, aB=1LL*B.W*B.H; if (aA!=aB) return aAoptional{ World world(S); world.occ.reserve((size_t)totalCells*2 + 1024); vector place(n); for (int idx=0; idx vord(piece.variants.size()); iota(vord.begin(), vord.end(), 0); sort(vord.begin(), vord.end(), [&](int a,int b){ const auto&A=piece.variants[a], &B=piece.variants[b]; if (A.h!=B.h) return A.hB.w; return A.pts.size()>B.pts.size(); }); int bestVi=-1, bestX=-1, bestY=INT_MAX; for (int vi: vord){ const auto &o = piece.variants[vi]; if (o.w > world.W || o.h > world.Hlimit) continue; // x scan (try both coarse step and right align) int step = max(1, o.w/2); for (int x=0; x + o.w <= world.W; x += step){ int y = world.shelfY(x, o.w); if (y + o.h > world.Hlimit) continue; // square cap if (y > bestY) continue; if (!world.can_place(o, x, y)) continue; bestY=y; bestX=x; bestVi=vi; } int xr = world.W - o.w; if (xr>=0){ int y = world.shelfY(xr, o.w); if (y + o.h <= world.Hlimit && y <= bestY && world.can_place(o, xr, y)){ bestY=y; bestX=xr; bestVi=vi; } } } if (bestVi<0) return {}; // fail for this S const auto &o = piece.variants[bestVi]; world.do_place(o, bestX, bestY); place[i] = {bestX, bestY, o.tf.R, o.tf.F, o.w, o.h, bestVi}; } // success with this S PackedSolution cand; cand.W = S; cand.H = S; // enforce square cand.place.resize(n); for (int i=0;i pl(n); // Use each piece's first variant for (int i=0;i