vector <int> getOrdering( vector <int> height, vector <int> bloom, vector <int> wilt )
{
int n = height.size();
vector < vector <int> > mat (n, vector <int> (n, 0));
vector <int> deg (n, 0);
vector <int> mark (n, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if ( height[i] < height[j] &&
( (bloom[i] >= bloom[j] && bloom[i] <= wilt[j]) || (bloom[j] >= bloom[i] && bloom[j] <= wilt[i]) )
)
{
mat[i][j] = 1;
deg[j]++;
}
vector <int> res;
for (int i = 0; i < n; i++)
{
int maxi = -1;
int ind = -1;
for (int j = 0; j < n; j++)
if (!deg[j] && !mark[j] && height[j] > maxi)
{
maxi = height[j];
ind = j;
}
mark[ind] = 1;
res.push_back (maxi);
for (int j = 0; j < n; j++)
if (mat[ind][j])
deg[j]--;
}
return res;
}
ICAgdmVjdG9yIDxpbnQ+IGdldE9yZGVyaW5nKCB2ZWN0b3IgPGludD4gaGVpZ2h0LCB2ZWN0b3IgPGludD4gYmxvb20sIHZlY3RvciA8aW50PiB3aWx0ICkgCiAgIHsKCSAgIGludCBuID0gaGVpZ2h0LnNpemUoKTsKCSAgIHZlY3RvciA8IHZlY3RvciA8aW50PiA+IG1hdCAobiwgdmVjdG9yIDxpbnQ+IChuLCAwKSk7CgkgICB2ZWN0b3IgPGludD4gZGVnIChuLCAwKTsKCSAgIHZlY3RvciA8aW50PiBtYXJrIChuLCAwKTsKCSAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQoJCSAgIGZvciAoaW50IGogPSAwOyBqIDwgbjsgaisrKQoJCQkgICBpZiAoIGhlaWdodFtpXSA8IGhlaWdodFtqXSAmJiAKCQkJCSAgICggKGJsb29tW2ldID49IGJsb29tW2pdICYmIGJsb29tW2ldIDw9IHdpbHRbal0pIHx8IChibG9vbVtqXSA+PSBibG9vbVtpXSAmJiBibG9vbVtqXSA8PSB3aWx0W2ldKSApIAoJCQkgICAgICAgKQoJCQkgICB7CgkJCQkgICBtYXRbaV1bal0gPSAxOwoJCQkJ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