#include #include #include #include #include #include #include #include #include using namespace std; using int64 = long long; int main() { int n; cin >> n; vector> a(n); for (int i=0; i> p; for (int j=0; j> ai; a[i].push_back(ai); } reverse(a[i].begin(), a[i].end()); } bool f = true; while(f) { f = false; for (auto& ai : a) { if (ai.empty()) continue; f = true; cout << ai.back() << " "; ai.pop_back(); } } }