Problem p02704 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
/*
Coded by 秦惜梦
The most attractive girl in the world
.........
`;!!!!!!!!!;;;;;;;;::::::'''''''``''''''''````````````````````....`:;;;;;:'.....`:|$$$$$$$|;;;:`.
..........`''``...`''``.   ........   ...  ...   ....  ...   ...  '
........
'!!!!!!!!!;;;;;;;:::::::::''''':::''''`````````````````````......`:;;;;;!%$$%!'.....`:|$%!;;;'....
.....`....`'''`...```..    ........  ..   ...    ..   ...  ...   '
........
.:!!!!!!!!;;;;;;;:::::::::::::::'''''''`````````..```````........`:;!%$$$$$$$$$$$%!'.....`:|!'````..
.........`''``.......    ........  ...  ...   ...   ..   ..   .'
.......       `;!!!!!!;;;;;;;;::::::::::::::::'''''```````````.``````...
.`';|%$$$$$&&$$$$$$%%;'..`'':'''``..  ...``.....```.   ....    .......  ...  ...
...  ...  ...   .'
.......      .
'!!!!!!;;;;;;;:::'''''''''::::::'''```````````````````.........':'`.......`:!%%|||%$$$$$&%;'::::''``.
.```..   ....   ....   .......   ..  ...   ..   ...  ...  ..'
......
'::'''':::::::::::::'''''''''''::::''''``````````````````.........'!$$$$$$%!:`.......`:!%&&&|'``':::'''``.....```.
....   ...    ......   .......   ..   ..  ...   ..'
``...
.`.``'::::::::;;;;:::::'''''''`'''''`````````````..```````........'!$$$$$$$$$$$$$$%!;'`.....`'`..`'::''''`.....```..
....   ...   ......   ........ .... .......  ....'
````.      .````.`;!!!;;;;;;;;::'''''''``'''''`````````````````````````..
....`:!|%$$$&&&&&&&$%%$&&&&@&$|::'`.`''''''''`....````.  ....   ...   ......   .
.....```..``.....  ... '
````      .````.  `;!!;;;;;;;:::::'''''':::'''''````````````........`````....
........````........``'::::::::'``.``'''''``...````.. ....   ...  ......   .
......``.```....   ..  '
```.      .```.
.`';!!;;;;;;;:::::::::::::'''''``````````..........````````...`;$$$$&&&&&&$%%||||!|!;;::::::::'`...`'''''`....````.
.... ...   .....   .   .. .``..```....  ..   '
``.       .``.
.`'`';!;;;;;;;;:::::::::::'''''`````````...........`````````....'!|%%%%%%%%%%%%%%%%%%$&&&|:'''''''`...`'''''`....```.
.... ...   .....  .   ..  ..  ......   .   .'
`.        .`.
`''''`';!;;;;;;;::::::::::'''''''```````..........````````````....:|%%%%%%%%%%%%%%%%%%%%%%|:`''''''''`...`''''`....```..
...   ..  .....  ..  .   ..  .. ...  ..  ..' `         .
.`'''''``';!;;;;;;;:::::::''''''''''```.``........```````````````````!%%%%%%%%%%||!;:''`.........`'''''''`...`''''`...````.....
..   ....  ..  .   .   .  ..  ..  ...'
            .''''''````';!;;;;;;;:::::'''''''''''```````````````````````````.........``````...
...```':;!!:....`'''''''``..`'''`....```.  ... ..  ....      ..  .   .  ..  ..
...'
         .`''''''''``````;!;;;;;;:::::'''''''''''``````````'''''''''``....```````````````````````````':!||%|;``....`''''''``..`''``....`..
...     ...   .  .   .  .. ..  .   ... '
        .'''''''''````````:;;;;:::'''''':::::::::::::''''''''''''``..``````....```````````````````````````:!!'`'`.....`'''''``...```.
.........    ....     .  ..  .  .  ..  ...  '
        `'''''''''````';;;:::::::::::::::::::::::''''```````````..........```````````````````````````````````'`.``'``....`''''``..
.`.. ..... ...    ...        .  .. ..  .   ..   '
       .'''''''''```````';;:::::''''''''''''''''''`''```````.`````.....````````````````````````````````''``````....``''`...``''```......
........   ...     .     .  .  .   ..    '
       `''''''''````'''`````.`::::::::'''''''''''''''''``.``.```........````````````''''`````````````````''''''''`....``'``...``````......
........   ..     .  .  . .   .  ..     '
      .`'''''''```'''''````````':::::::''''''''''''''``.``.`............`````````'''''''''''''''``````````'''''''''`.....````....```...
..  ......    .                .   .     .'
      `'''''''`''''''''````````.`::::::'''''''''''''`.```..............``````````'''''''''''''''''````````''''''''''``.......```.........
.  ....     .     ........              '
     .`''''''`''''''''``````````.`:::::::'''''''''``.`..............```````'''''''''''''''''''''''''``````'''''''''''''.........``...
.....  .  ...     ..`:::::::::::::'`.        '
      .'''''`''''''''``````````````':::::''''''''``..````````````````````''''''''''''''''''':''''''```''````''''::''''''`.```..........
.....   ....    `'::::::::::::::::`.      '
     ....`''''''''''```````````````.`':::::'''''`...``````````''''''''''''''''''''''''''''''';:'''``''`::````';:''''''''''.....``.........
.....   ...   `::::::::::::::::::'`.....'
    ......`''''''''``````````````````.':::::::'`...``'''````''''''''''''''''''''''''''''''';!:;:'''':!;::'```````''''''''''`
........ ..... ...    ..  ....`':::':::::::::::'... '
    .......`'''''`````````````````````.`':::'```.`''''''````''''''''':'''::'''::''':;::!!!;:;!;;:```````::`````````'''''''''`.
............  ..   ..          .':::::::::''`   '
   ........`''`````````````````````````..`':''`:;:'':''':''''''''''''::'':;:'''''''''::'''''':;;;:``````:;'```''``````'``'''''`....
...........    ..                 `::::::'`.   '
  ..........`````````````````````````'::::''':;!!!!;:'::':!;;;:::'''';;'''::::''''':'''::'''''''::'`````':''``''````::`':'''''''''.
......                      .''`....   '
.`'`.......```````````````````````````````'':!;:::::::::':;::::::::''::''`''::::'`''''':::'':''`:;'`````':'```''`````::`````'''''':;'..................
.......  . '
`'''''```````````````````````````````````.`;!:::::::::::'''::::::::''::''``'::::'``'''''::''''''':'````````````'`````''````````````:!!;'................
..........   '
'''''`````````````````````````````````````;!;;;:':::''::''''::::;;:'':::'```'::'````''''''''''''':'```````````''````````````````````';!!!;'....
......               .......      '
''''````````````````````````````````````';!;;;:'::::':''`''':::;;;:'''':'``'':'````.```''''''''`':'````````````'`````````..``....`...`:!!!!:`.`.....
.          '
'''````````````````````````````````````':;!;;:`'::':'''``''':::;;;:''``'```'':'``....''`'''''''`''`.`````````````.````````.............';;'.`:;;;'`.
.....   ...   '
''````````````````````````````````````'':!;::'`::':::''`''''':;;;;:```````'''''```...''`'''''''`''`.``.````````````````````...............
`;;;;;;;;;:`.                ....   ....'
''''''''````````````````````````````''`:!;;:''':''::''`'':''':;;;;'```'```''''''`.`'``:'''::''''''```````````````````````````.............`:;;;;;;;;;;;'.
.         ....    ..'
:::::::''''````````````````````````''`'!!;:''''''':''`''''''':;;;:```::``'''':'''`';:';:'::::':`''`''`````````````````'``````````..........`:;;;;;;;;;:.
`'..          .....   '
|%$$%%!;;:'''````````````````````''``';!;:'''':'``'''`'':''':::;:'``:!:``':::::::`:!;';;':::::''`''::````'''''````````''`````````...........`:;;;;;;;'.
.':` `;;;;:. ..   ..... '
@@@@@@@@$|;:''`````````````````''```';!;:::::::'``````'':''::::::'`'!|:`':::;:::'';|!:;;'''''''::::;:``'::''`'````````''````````````.........`:;;;;;`
.:;'..';;;;;;;;;:` ......'
@@@@@@@@@@$|;:''`````````````:'`````;;;:';;;;::'`'````'':''::::::'`;!!;':::::::'`.';!;;;:::::';!!::;'`':::'``'`.``````''````````..........
..``';;:`     .';;` `:;;;;;;;;;:` `;;;;;;
@@@@@@@@@@@&|;:'``````````''```````;;::':;;;;:'`''```'':'::::;;;:':;|!!;:'''''::::!||!!!!;!!!!||;:!'`:::::'````..``````''`````..........
.  ..`':;`     .';;:` ';;;;;;;;;;:. ';;;;;;
@@@@@@@@@@#@$!:''`````````````````::```';;;;::``'''``'::':::;;;::;;!|||!::;;!!!;;;:'``````'':!!!!|:``':::'`````:;:`.``````.............
. ..`';'     .';;;'..:;;;;;;;;;;'..';;;;;;
@@@@@@@@@@@@$|;:'````````````````::````:;;;::'.`''```''''':::;:::;||%|||%%%%%|!:''''`...`'':!;:;|;```:::'``''`';!;'..`````..............
. ..`''.    .';;;:` `;;;;;;;;;;;' .:;;;;;;
@@@@@@@@@@@&%!;:'```````````````''````':;;;::'..''```````:::::';||%%%%%%$$&&&%!;!|$&%:..';!%%!!|!'`;::::'``''';!;;'...````..............
....``..   .';;;;'..';;;;;;;;;;;` `:;;;;;;
@@@@@@@@@#@%!!;:'````````````````````.'''::::`..````'''::!||||%%%%&&&&&&&&&&&&&$&&&%||||!|%%%%|!:';;;;!;``''`:;;;:`....`...................`....```.
.`':::` `:;;;;;;;;;;:` `;;;;;;;
@@@@@@##@$|!!;:''```````````````````````':'```..`````':!;:''''':!%$&&&&&&&&&&&&$$$|!;;!|%%%%%%|;':;''::'`'''`:!;:`........................```..````........
.':;;;;;;;;;'. ';;;;;;;
!%$$$$%%|!!!;:''``````````````````````````````````````';!:'::'``':|$&&&&&&&&&&&&$$$$$$$$%%%%%|!:```''::'''''```...........................``..`'''`........
......'::;;'..';;;;;;;
;!!!!!!!!!!;:''``````````````````````````````..````````'!%!;!;;!!%$%$&&&&&&&&&&&&$$$$$$$$$$%%||;``'''':''''``..`....
... .................`'''````.....   .....`.......  `::;;;;;
::;!!!!!;;:''````````````````````````````````...````````'!%|!|%$$$$%$&&&&&&&&&&&&&&$$$$$$$$%|||:``''''''''```..`....
..    ...     ....`'''`''```....   .'|%%;......    .... .'
``'::::'''````````````````````````````````````..`...```'`'|&$$$$$$$%$&&&&&&&&&&&&&&$$$$$$$$%%%|:`'''''''''```..`....
....`''```''```... .:%%%%|:......   .......'
 .````````````````````````````````````````````.``..```````:|&$$$$$$|%$&&&&&&&&&&&&&&&&&$$$$%%%|:'`'''''''````..`....
.``'`````''````. :%%%%%;`....... ........'
  .``````````````````````````````````````````..`..`````````:%&&&&&&$%|%$&&&&&&&&&&&&&&&&$$$%$%|:'`'''''''````.``.....
.``'`````'`'':|:.'!%%%%|'.`````.   .......'
  .```````````````````````````````````````````'`..`````````.:$&&&&&&&&&&&&&&&&&&&&&&&&&&&$$$$%|;'```''''```````.......
.``'```'``':|%%|'.'|%%%%;```````...........'
  .`````````````````````````````````````````:'`..''````````..!&&&&&&&&&&&&&&&&&&&&&&&&&&&$$&$%%;''```''````````....
.                .'''`.`'``'`;%%%;``;%%$%!'..................'
  .``````````````````````````````````````;|:.``.`''''`````...`|&&&&&&&&&&&&&&&&&&&&&&&&&&$$&&$%!:'```````````.``..
.`''```;;'''';%%%:.:%&&$|:...................:
  .```````````````````````````````````'!!```'`.`:!:''`````.
..`|&&&&&&&&&&&$%%$$$%|%$$&&&$$&&&$%;'`.``..`......`.
..`''''!;''''':|%$|'`!&&$|:...................`:
   .````````````````````````````````;|:````'::`'!;::''''`.
...`!&&&&&&&&&&&&&&$&&&&&&&&&&$&&&&$|:'```.``.........        .`.
....`'''```''''';$&&&;`:!;`......................`:
   .`````````````````````````````:|;`````'';:'`';:'''```..
......'|&&&&&&&&&&&%%%$&&&&&&&$$&&&&&%;'`'`..```........         ``
`;:`''''``'```'!$&&&%!'.....................```````:
   .``````````````````````````:|;``````'':;:'``':'''`````.
........`:%&&&&&&&&&&&&&&&&&&&$&&&&|!!!;'''`....``......`.
.;%%;`''''';|||!;:''::;;' ..``......``````````````````:
   .```````````````````````!%:````````'':;:'```'':''````..
............'|&@&&&&&&&&&&&&&&&$$|;!!!!!;:'``'`....``...````..
`:;;:'''!%;::::::::;;`. .````````'''````````````````:
   .```````````````````:%!'``````````'::::'````'''''````.
.............```;$&@&&&&&&&&&&$|;;!!!!!!!!;```::`  ...``.';;'```.
`:::'';$$|;:::::!;'`....``````''''''''````''''`````:
   .```````````````:%|'`````````````'::::'`````'''''''``.  .....
....`````````;$&&&&@&|::!!;!!!!!!!!|!'```';'..
....'!%%|!::``'':'':|$&&$|!!;:;:'`. .```````':::::''''''''''''''':
   .``````````;||:```````````````''::'::'````.`'''''''`.. .......
..`````````````';:`````';!!!!!!!||||!;'```';!:`...
..`:|%%!'''''`:!%$&&&$$$!:;;;:'``'```''::::::::''''':::::'''';
   ..``';;:'```````````````````'''::'':'```````'''::''`..  .......
..``````````````````.
'||!!!|||||||!!;'````'!|;'``````````'''`';:'`'%&&&&&&|;!;:;!!!;;::;;::::::''''::::::::::;
``...``````````````````````''''''::''''`````````':::''``. .........
....```````````...   .:!||||||%%%|||!!!!;'````'!||;'```.`'''`.`.`''`.;$&&&$:
.``'::::;!|%$$%|;:''':::::::::::;
    .````````````````````'''''''::```'````````.`:::'''```. ..........
.............    `!%|!!||||%%||||!!!!!|%%%!:````````..`'''..``''`.....':. .
..`':;;;;;;;::::;!%&%!;;;;:::::;
    .`````````````````''''''''''''''::'''````..`::''''`````.
........``............
.;%%%|!!!||%%%%%%%|!!!|%%%%%%||%%%%%%!'`'''`````....``.....       ..
.`':;;;;;;:::::;!%%!;;;;;;
    .`````````````'''''''''''```''''::'''''``..`''''''```````........````````.....:%%%$%||!|||%%%%%%%||!|%%%%%%%%%%%%|||!'`'''';%%|;`````......'`.
..      `':::::::;;;;;|%!;;;
    .``````````''''''''''```````'```''''````..`.`'''''````````....  ..```....
`|%%%%$%|||!|%%%%%%%%%|!|%%%%%%%%%%%%%%%|:`'''`;%%%%!:.:!|%$$$$|;:'`...   .``..
.`.  '::::;;;;;;!%!;
%!'..```````''''''''''``````````'```'````````.``.`'''''```````.......
.;%%%%%%%|!!!||%%%%%|!!!!!|%%%%%%%%%%%%%%%%:`''''!%%%!!!:`:!|%$$$$$$$$$%|'`.
.```'`.``.  ..     .':::|%
%%%%%%;```''''''''```````````````'``''``````.````.`''''`..'````.......`'''';|%%%%%%%%||%%%%%%%%%%%|!!%%%%%%%%%%%%%%%%%:`'''';%%||||:`:!|%$$$$$$$$$$$$$%;``.
.``''`.                '
$$$%%%%%$$|:''````````````````````''''``````.`````.`'''''`...
.`':|%$$%%%%%%%%%|!|%%%%%%%%%%|||%%$$%%%%%%%%%%%%%:`'''';%||%%%;.:||%$$$$$$$$$$$$$$&&&%:...````''`
'
$$$$$$$$$$%%%%%;`.``````````````````':'``````.`````.`'''''`......`''':!%%%%%%%%%%%%|!!%%%%|||%%|!|%%%%%%%%%%%%%%%%%%:`'''';||%%%|:`;||%$$$$$$$$$$$$&&&&&&&$|'...```'''`
'
|%%$$%%$$$$$%%%%%%%;`..`.`````````````':'````.``.```..`''````''''':|!:|%%%%%%%%%%%%|!!|%|||%||!!%%%%%%%%%%%%%%%%%%%:`'''':|%%%%;``;|%%$$$$$$$$$$&&&&&&&&&&&$$;...`````''`
'
@$|||||%%%%%%%%%%%%%%%%|:`....```````.`''``'''`````````.````''':;|%|;:|%%%%%%%%%%||!!|||||||!!|%%%%%%%%%%%%%%%%%%%:`'''';|%%%%:.'!|%$$$$$$$$$$$$$&&&&&&&&&&&&$!`..``````''`
'
||||&@&%|!|||%%%%%%%%%%%%%%|:.....```````'````.........`!%%%%%%%%|||;:|%%%%%%%%%%||!!|%|%|!!||%%%%%%%%%%%%%%%%%%%:`'''';|%%%!'`;||%$$$$$$$&&$$$$$&&&&&&@&&&&&$$!`..``````````.
'
|||||!|!|$@&%|!|||%%%%%%%%%%%%%:.
...`````''``........:|%%%%%%%%%||%;:|%%%%%%%$%%%%!;|||!!||||%%%%%%%%%%%%%%%%%%:`'''';||%|:`:||%%$$$&&&&&$$%%$$&&&&@######&&$$$!`..``..```````.
'
||||||||!|!!!!|%$%|||||%$%%%%%%%%%!`.....````'`......'|%%%%%%%%%%|%$|;%&&&&&&&&$$$$|!|!!%$$$$&&&&&&&&&$$$$$$$$%;`''':;|||;``;||%$$&&&&&&&$$%%$$&&&&&@######&$$$$%:.....``````````.
'
||%%||||||!!|!!|||!|%$%|||%%%%%%%%%%%|:.....`.`'`...'|%%%%%%%%%$$$$&|:|&&&&&&&&$$$$|;!|$$$$&&&&&&&&&&&&&&&&@&$;`''':;||;``!||%$&$$$&&&&$%%%%%$&#####@@###@&$$&$$$!`.......`````````:
   .:!|%%|||||||||||||||!|||||%%%%$%%%%%;`.......``:%&&&&&&&&&&&&$$$%;;$&&&&&&&&&&$||$$$&&@@@@@@@@@@@@@@@@@@&!'''':;!:`'!||%$&$$$$$&&&$%%|%%$@######@&&&&&$$$$$$$%;..`...`...`````':
    .....`;|%%%%%|||!|||!|||!!!||%%%%%%%%%%;`.....:%&&&&&&&&&&&&$$$&$|'!$&&&&&&&$$%$$$&&@@@@@@@@@@@@@@@@@@@@!'::':'`.'|||%$$&&$$$$$&&$%%||%$@#########@@&&$$$$&$%$!`..`...```..````:
    ..........'!|%|%|||||!||||||||||%%%%%%%%%:...'|&&&&&&&&&&&&&$$&&&$;'|&&&&&$%%$$$&&@@@@@@@@@@@@@@@@@##@@!'::::'`:|%%%$$$&&&$$$$$$$%%|%&@@##########@@@@&&&$$$$$%;..``....````.``:
    .....```......`;|%|%%||||||||||||||%%%%%%|!:`!&&&&&&&&&&&&&&$$$&&$%::%&$$%%$$&&&@@@@@@@@@@@@@@@@@@@#@&|::::::;|%%$$$$&&&&&$$$$$$%%%%&@@@###########@@@@&&&&&$$$!`..`.....`````.'
    ..................`;|%%%|%%|||!|||||||%%%|||$&&&&&&&&&&&@&&&$$$&&&$%;;%%%$$&&@@@@@@@@@@@@@@@@@##@#@&$;':::::!%%$$$$&&&&&&&$$$%%%%%|$@@@@##@#########@@@@@&&&&$$|:...`.....`````:
    ............``..```...`;|%|||%||!!|||||||||%&&&&&&&&&&@@@@&&$$$&&&&$%;;%$$&@@@@@@@@@@@@@@@@@@#@@&$!`.`:::::!%$$$&&&&&&&&$$$$$$%%%||&@@@@##@@@@########@@@@&&&&$$!`...`.....````:
    ...................```...`:|%||||||||!||||%&&&&&&&&&&&@@@@@&$%&&$$$%%%;:!$&@@@@@@@@@@@@@@#@@&&%;``;|;:::::!$$$&&&&&&&&&&$$$$$%%%%||&@@@@@@@@@@##@#####@@@@@&&&&&$:..```.....```:
    .....................``.`...`;|%|||||||||%&&&&&&&&&&&&@@@@@@$$&$$%%$$$%!::|&@@@@@####@@@&$%;'':|%|!::::::!$$&&&&&@&&&&&&$$$$$$%%|||$@@@@@@@@@@@@@@@####@@@@@&&&&&%'.```..`...``:
    ...........................``..'!%|||||!%$&&&&&&&&&&&&@@@@@&$$$%%$$@@@&$%!::|&&&&&&&&$|;::;||||%%|::::::!$$&&&&@@@&&&&&&$$$$$$%%|||$@@@@&&&&&&&@@@@@@@@@@@@@@&&&$$|'.```.......:
    ...........................```...`;||||%$&&&&&&&&&&&&@@@@@&$%%$$&@@@@&&&&$|!!;;!||;:;!|%%||%$&&$|;:::::!$$&&&@@@@@@@&&&&$$$$$$%%%%|%&@@@&&&&&&&&&&&&&&&&&@@@&&&&$$$!`.``.......:
*/

const int Inf = 0x3f3f3f3f;

namespace nf {
struct Edge {
  int to, cap, rev;
  Edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};
vector<Edge> egs[1005];
int base;
void init() {
  base = 0;
  for (int i = 0; i < 1005; i++) {
    egs[i].clear();
  }
}
void newedge(int u, int v, int cap) {
  egs[u].emplace_back(v, cap, egs[v].size());
  egs[v].emplace_back(u, 0, egs[u].size() - 1);
}
void newedge(int u, int v, int l, int r) {
  newedge(0, v, l);
  newedge(u, 1, l);
  newedge(u, v, r - l);
  base += l;
}
int dist[1005], iter[1005];
void bfs() {
  memset(dist, -1, sizeof(dist));
  dist[0] = 0;
  queue<int> q;
  q.push(0);
  while (q.size()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < egs[x].size(); i++) {
      Edge &e = egs[x][i];
      if (e.cap && !~dist[e.to]) {
        dist[e.to] = dist[x] + 1;
        q.push(e.to);
      }
    }
  }
}
int dfs(int now, int cap) {
  if (now == 1) {
    return cap;
  }
  for (int &i = iter[now]; i < egs[now].size(); i++) {
    Edge &e = egs[now][i];
    if (e.cap && dist[e.to] == dist[now] + 1) {
      int fl = dfs(e.to, min(cap, e.cap));
      if (fl) {
        e.cap -= fl;
        egs[e.to][e.rev].cap += fl;
        return fl;
      }
    }
  }
  return 0;
}
int getflow() {
  int fl = 0;
  while (1) {
    bfs();
    if (!~dist[1]) {
      break;
    }
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(0, Inf))) {
      fl += f;
    }
  }
  return fl;
}
} // namespace nf

int n;
int s[505], t[505];
unsigned long long u[505], v[505], ans[505][505];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", s + i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", t + i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%llu", u + i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%llu", v + i);
  }
  for (int b = 0; b < 64; b++) {
    nf::init();
    for (int i = 1; i <= n; i++) {
      if (s[i] == 1) {
        if ((u[i] >> b) & 1) {
          nf::newedge(2, 3 + i, 1, n);
        } else {
          nf::newedge(2, 3 + i, 0, 0);
        }
      } else {
        if ((u[i] >> b) & 1) {
          nf::newedge(2, 3 + i, n, n);
        } else {
          nf::newedge(2, 3 + i, 0, n - 1);
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      if (t[i] == 1) {
        if ((v[i] >> b) & 1) {
          nf::newedge(3 + n + i, 3, 1, n);
        } else {
          nf::newedge(3 + n + i, 3, 0, 0);
        }
      } else {
        if ((v[i] >> b) & 1) {
          nf::newedge(3 + n + i, 3, n, n);
        } else {
          nf::newedge(3 + n + i, 3, 0, n - 1);
        }
      }
    }
    nf::newedge(3, 2, Inf);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        nf::newedge(3 + i, 3 + n + j, 1);
      }
    }
    int fl = nf::getflow();
    if (fl != nf::base) {
      puts("-1");
      return 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < nf::egs[i + 3].size(); j++) {
        nf::Edge &e = nf::egs[i + 3][j];
        if (e.to > 3 + n && !e.cap) {
          ans[i][e.to - 3 - n] |= (1ull << b);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      printf("%llu%c", ans[i][j], j == n ? '\\n' : ' ');
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*
Coded by 秦惜梦
The most attractive girl in the world
.........
`;!!!!!!!!!;;;;;;;;::::::'''''''``''''''''````````````````````....`:;;;;;:'.....`:|$$$$$$$|;;;:`.
..........`''``...`''``.   ........   ...  ...   ....  ...   ...  '
........
'!!!!!!!!!;;;;;;;:::::::::''''':::''''`````````````````````......`:;;;;;!%$$%!'.....`:|$%!;;;'....
.....`....`'''`...```..    ........  ..   ...    ..   ...  ...   '
........
.:!!!!!!!!;;;;;;;:::::::::::::::'''''''`````````..```````........`:;!%$$$$$$$$$$$%!'.....`:|!'````..
.........`''``.......    ........  ...  ...   ...   ..   ..   .'
.......       `;!!!!!!;;;;;;;;::::::::::::::::'''''```````````.``````...
.`';|%$$$$$&&$$$$$$%%;'..`'':'''``..  ...``.....```.   ....    .......  ...  ...
...  ...  ...   .'
.......      .
'!!!!!!;;;;;;;:::'''''''''::::::'''```````````````````.........':'`.......`:!%%|||%$$$$$&%;'::::''``.
.```..   ....   ....   .......   ..  ...   ..   ...  ...  ..'
......
'::'''':::::::::::::'''''''''''::::''''``````````````````.........'!$$$$$$%!:`.......`:!%&&&|'``':::'''``.....```.
....   ...    ......   .......   ..   ..  ...   ..'
``...
.`.``'::::::::;;;;:::::'''''''`'''''`````````````..```````........'!$$$$$$$$$$$$$$%!;'`.....`'`..`'::''''`.....```..
....   ...   ......   ........ .... .......  ....'
````.      .````.`;!!!;;;;;;;;::'''''''``'''''`````````````````````````..
....`:!|%$$$&&&&&&&$%%$&&&&@&$|::'`.`''''''''`....````.  ....   ...   ......   .
.....```..``.....  ... '
````      .````.  `;!!;;;;;;;:::::'''''':::'''''````````````........`````....
........````........``'::::::::'``.``'''''``...````.. ....   ...  ......   .
......``.```....   ..  '
```.      .```.
.`';!!;;;;;;;:::::::::::::'''''``````````..........````````...`;$$$$&&&&&&$%%||||!|!;;::::::::'`...`'''''`....````.
.... ...   .....   .   .. .``..```....  ..   '
``.       .``.
.`'`';!;;;;;;;;:::::::::::'''''`````````...........`````````....'!|%%%%%%%%%%%%%%%%%%$&&&|:'''''''`...`'''''`....```.
.... ...   .....  .   ..  ..  ......   .   .'
`.        .`.
`''''`';!;;;;;;;::::::::::'''''''```````..........````````````....:|%%%%%%%%%%%%%%%%%%%%%%|:`''''''''`...`''''`....```..
...   ..  .....  ..  .   ..  .. ...  ..  ..' `         .
.`'''''``';!;;;;;;;:::::::''''''''''```.``........```````````````````!%%%%%%%%%%||!;:''`.........`'''''''`...`''''`...````.....
..   ....  ..  .   .   .  ..  ..  ...'
            .''''''````';!;;;;;;;:::::'''''''''''```````````````````````````.........``````...
...```':;!!:....`'''''''``..`'''`....```.  ... ..  ....      ..  .   .  ..  ..
...'
         .`''''''''``````;!;;;;;;:::::'''''''''''``````````'''''''''``....```````````````````````````':!||%|;``....`''''''``..`''``....`..
...     ...   .  .   .  .. ..  .   ... '
        .'''''''''````````:;;;;:::'''''':::::::::::::''''''''''''``..``````....```````````````````````````:!!'`'`.....`'''''``...```.
.........    ....     .  ..  .  .  ..  ...  '
        `'''''''''````';;;:::::::::::::::::::::::''''```````````..........```````````````````````````````````'`.``'``....`''''``..
.`.. ..... ...    ...        .  .. ..  .   ..   '
       .'''''''''```````';;:::::''''''''''''''''''`''```````.`````.....````````````````````````````````''``````....``''`...``''```......
........   ...     .     .  .  .   ..    '
       `''''''''````'''`````.`::::::::'''''''''''''''''``.``.```........````````````''''`````````````````''''''''`....``'``...``````......
........   ..     .  .  . .   .  ..     '
      .`'''''''```'''''````````':::::::''''''''''''''``.``.`............`````````'''''''''''''''``````````'''''''''`.....````....```...
..  ......    .                .   .     .'
      `'''''''`''''''''````````.`::::::'''''''''''''`.```..............``````````'''''''''''''''''````````''''''''''``.......```.........
.  ....     .     ........              '
     .`''''''`''''''''``````````.`:::::::'''''''''``.`..............```````'''''''''''''''''''''''''``````'''''''''''''.........``...
.....  .  ...     ..`:::::::::::::'`.        '
      .'''''`''''''''``````````````':::::''''''''``..````````````````````''''''''''''''''''':''''''```''````''''::''''''`.```..........
.....   ....    `'::::::::::::::::`.      '
     ....`''''''''''```````````````.`':::::'''''`...``````````''''''''''''''''''''''''''''''';:'''``''`::````';:''''''''''.....``.........
.....   ...   `::::::::::::::::::'`.....'
    ......`''''''''``````````````````.':::::::'`...``'''````''''''''''''''''''''''''''''''';!:;:'''':!;::'```````''''''''''`
........ ..... ...    ..  ....`':::':::::::::::'... '
    .......`'''''`````````````````````.`':::'```.`''''''````''''''''':'''::'''::''':;::!!!;:;!;;:```````::`````````'''''''''`.
............  ..   ..          .':::::::::''`   '
   ........`''`````````````````````````..`':''`:;:'':''':''''''''''''::'':;:'''''''''::'''''':;;;:``````:;'```''``````'``'''''`....
...........    ..                 `::::::'`.   '
  ..........`````````````````````````'::::''':;!!!!;:'::':!;;;:::'''';;'''::::''''':'''::'''''''::'`````':''``''````::`':'''''''''.
......                      .''`....   '
.`'`.......```````````````````````````````'':!;:::::::::':;::::::::''::''`''::::'`''''':::'':''`:;'`````':'```''`````::`````'''''':;'..................
.......  . '
`'''''```````````````````````````````````.`;!:::::::::::'''::::::::''::''``'::::'``'''''::''''''':'````````````'`````''````````````:!!;'................
..........   '
'''''`````````````````````````````````````;!;;;:':::''::''''::::;;:'':::'```'::'````''''''''''''':'```````````''````````````````````';!!!;'....
......               .......      '
''''````````````````````````````````````';!;;;:'::::':''`''':::;;;:'''':'``'':'````.```''''''''`':'````````````'`````````..``....`...`:!!!!:`.`.....
.          '
'''````````````````````````````````````':;!;;:`'::':'''``''':::;;;:''``'```'':'``....''`'''''''`''`.`````````````.````````.............';;'.`:;;;'`.
.....   ...   '
''````````````````````````````````````'':!;::'`::':::''`''''':;;;;:```````'''''```...''`'''''''`''`.``.````````````````````...............
`;;;;;;;;;:`.                ....   ....'
''''''''````````````````````````````''`:!;;:''':''::''`'':''':;;;;'```'```''''''`.`'``:'''::''''''```````````````````````````.............`:;;;;;;;;;;;'.
.         ....    ..'
:::::::''''````````````````````````''`'!!;:''''''':''`''''''':;;;:```::``'''':'''`';:';:'::::':`''`''`````````````````'``````````..........`:;;;;;;;;;:.
`'..          .....   '
|%$$%%!;;:'''````````````````````''``';!;:'''':'``'''`'':''':::;:'``:!:``':::::::`:!;';;':::::''`''::````'''''````````''`````````...........`:;;;;;;;'.
.':` `;;;;:. ..   ..... '
@@@@@@@@$|;:''`````````````````''```';!;:::::::'``````'':''::::::'`'!|:`':::;:::'';|!:;;'''''''::::;:``'::''`'````````''````````````.........`:;;;;;`
.:;'..';;;;;;;;;:` ......'
@@@@@@@@@@$|;:''`````````````:'`````;;;:';;;;::'`'````'':''::::::'`;!!;':::::::'`.';!;;;:::::';!!::;'`':::'``'`.``````''````````..........
..``';;:`     .';;` `:;;;;;;;;;:` `;;;;;;
@@@@@@@@@@@&|;:'``````````''```````;;::':;;;;:'`''```'':'::::;;;:':;|!!;:'''''::::!||!!!!;!!!!||;:!'`:::::'````..``````''`````..........
.  ..`':;`     .';;:` ';;;;;;;;;;:. ';;;;;;
@@@@@@@@@@#@$!:''`````````````````::```';;;;::``'''``'::':::;;;::;;!|||!::;;!!!;;;:'``````'':!!!!|:``':::'`````:;:`.``````.............
. ..`';'     .';;;'..:;;;;;;;;;;'..';;;;;;
@@@@@@@@@@@@$|;:'````````````````::````:;;;::'.`''```''''':::;:::;||%|||%%%%%|!:''''`...`'':!;:;|;```:::'``''`';!;'..`````..............
. ..`''.    .';;;:` `;;;;;;;;;;;' .:;;;;;;
@@@@@@@@@@@&%!;:'```````````````''````':;;;::'..''```````:::::';||%%%%%%$$&&&%!;!|$&%:..';!%%!!|!'`;::::'``''';!;;'...````..............
....``..   .';;;;'..';;;;;;;;;;;` `:;;;;;;
@@@@@@@@@#@%!!;:'````````````````````.'''::::`..````'''::!||||%%%%&&&&&&&&&&&&&$&&&%||||!|%%%%|!:';;;;!;``''`:;;;:`....`...................`....```.
.`':::` `:;;;;;;;;;;:` `;;;;;;;
@@@@@@##@$|!!;:''```````````````````````':'```..`````':!;:''''':!%$&&&&&&&&&&&&$$$|!;;!|%%%%%%|;':;''::'`'''`:!;:`........................```..````........
.':;;;;;;;;;'. ';;;;;;;
!%$$$$%%|!!!;:''``````````````````````````````````````';!:'::'``':|$&&&&&&&&&&&&$$$$$$$$%%%%%|!:```''::'''''```...........................``..`'''`........
......'::;;'..';;;;;;;
;!!!!!!!!!!;:''``````````````````````````````..````````'!%!;!;;!!%$%$&&&&&&&&&&&&$$$$$$$$$$%%||;``'''':''''``..`....
... .................`'''````.....   .....`.......  `::;;;;;
::;!!!!!;;:''````````````````````````````````...````````'!%|!|%$$$$%$&&&&&&&&&&&&&&$$$$$$$$%|||:``''''''''```..`....
..    ...     ....`'''`''```....   .'|%%;......    .... .'
``'::::'''````````````````````````````````````..`...```'`'|&$$$$$$$%$&&&&&&&&&&&&&&$$$$$$$$%%%|:`'''''''''```..`....
....`''```''```... .:%%%%|:......   .......'
 .````````````````````````````````````````````.``..```````:|&$$$$$$|%$&&&&&&&&&&&&&&&&&$$$$%%%|:'`'''''''````..`....
.``'`````''````. :%%%%%;`....... ........'
  .``````````````````````````````````````````..`..`````````:%&&&&&&$%|%$&&&&&&&&&&&&&&&&$$$%$%|:'`'''''''````.``.....
.``'`````'`'':|:.'!%%%%|'.`````.   .......'
  .```````````````````````````````````````````'`..`````````.:$&&&&&&&&&&&&&&&&&&&&&&&&&&&$$$$%|;'```''''```````.......
.``'```'``':|%%|'.'|%%%%;```````...........'
  .`````````````````````````````````````````:'`..''````````..!&&&&&&&&&&&&&&&&&&&&&&&&&&&$$&$%%;''```''````````....
.                .'''`.`'``'`;%%%;``;%%$%!'..................'
  .``````````````````````````````````````;|:.``.`''''`````...`|&&&&&&&&&&&&&&&&&&&&&&&&&&$$&&$%!:'```````````.``..
.`''```;;'''';%%%:.:%&&$|:...................:
  .```````````````````````````````````'!!```'`.`:!:''`````.
..`|&&&&&&&&&&&$%%$$$%|%$$&&&$$&&&$%;'`.``..`......`.
..`''''!;''''':|%$|'`!&&$|:...................`:
   .````````````````````````````````;|:````'::`'!;::''''`.
...`!&&&&&&&&&&&&&&$&&&&&&&&&&$&&&&$|:'```.``.........        .`.
....`'''```''''';$&&&;`:!;`......................`:
   .`````````````````````````````:|;`````'';:'`';:'''```..
......'|&&&&&&&&&&&%%%$&&&&&&&$$&&&&&%;'`'`..```........         ``
`;:`''''``'```'!$&&&%!'.....................```````:
   .``````````````````````````:|;``````'':;:'``':'''`````.
........`:%&&&&&&&&&&&&&&&&&&&$&&&&|!!!;'''`....``......`.
.;%%;`''''';|||!;:''::;;' ..``......``````````````````:
   .```````````````````````!%:````````'':;:'```'':''````..
............'|&@&&&&&&&&&&&&&&&$$|;!!!!!;:'``'`....``...````..
`:;;:'''!%;::::::::;;`. .````````'''````````````````:
   .```````````````````:%!'``````````'::::'````'''''````.
.............```;$&@&&&&&&&&&&$|;;!!!!!!!!;```::`  ...``.';;'```.
`:::'';$$|;:::::!;'`....``````''''''''````''''`````:
   .```````````````:%|'`````````````'::::'`````'''''''``.  .....
....`````````;$&&&&@&|::!!;!!!!!!!!|!'```';'..
....'!%%|!::``'':'':|$&&$|!!;:;:'`. .```````':::::''''''''''''''':
   .``````````;||:```````````````''::'::'````.`'''''''`.. .......
..`````````````';:`````';!!!!!!!||||!;'```';!:`...
..`:|%%!'''''`:!%$&&&$$$!:;;;:'``'```''::::::::''''':::::'''';
   ..``';;:'```````````````````'''::'':'```````'''::''`..  .......
..``````````````````.
'||!!!|||||||!!;'````'!|;'``````````'''`';:'`'%&&&&&&|;!;:;!!!;;::;;::::::''''::::::::::;
``...``````````````````````''''''::''''`````````':::''``. .........
....```````````...   .:!||||||%%%|||!!!!;'````'!||;'```.`'''`.`.`''`.;$&&&$:
.``'::::;!|%$$%|;:''':::::::::::;
    .````````````````````'''''''::```'````````.`:::'''```. ..........
.............    `!%|!!||||%%||||!!!!!|%%%!:````````..`'''..``''`.....':. .
..`':;;;;;;;::::;!%&%!;;;;:::::;
    .`````````````````''''''''''''''::'''````..`::''''`````.
........``............
.;%%%|!!!||%%%%%%%|!!!|%%%%%%||%%%%%%!'`'''`````....``.....       ..
.`':;;;;;;:::::;!%%!;;;;;;
    .`````````````'''''''''''```''''::'''''``..`''''''```````........````````.....:%%%$%||!|||%%%%%%%||!|%%%%%%%%%%%%|||!'`'''';%%|;`````......'`.
..      `':::::::;;;;;|%!;;;
    .``````````''''''''''```````'```''''````..`.`'''''````````....  ..```....
`|%%%%$%|||!|%%%%%%%%%|!|%%%%%%%%%%%%%%%|:`'''`;%%%%!:.:!|%$$$$|;:'`...   .``..
.`.  '::::;;;;;;!%!;
%!'..```````''''''''''``````````'```'````````.``.`'''''```````.......
.;%%%%%%%|!!!||%%%%%|!!!!!|%%%%%%%%%%%%%%%%:`''''!%%%!!!:`:!|%$$$$$$$$$%|'`.
.```'`.``.  ..     .':::|%
%%%%%%;```''''''''```````````````'``''``````.````.`''''`..'````.......`'''';|%%%%%%%%||%%%%%%%%%%%|!!%%%%%%%%%%%%%%%%%:`'''';%%||||:`:!|%$$$$$$$$$$$$$%;``.
.``''`.                '
$$$%%%%%$$|:''````````````````````''''``````.`````.`'''''`...
.`':|%$$%%%%%%%%%|!|%%%%%%%%%%|||%%$$%%%%%%%%%%%%%:`'''';%||%%%;.:||%$$$$$$$$$$$$$$&&&%:...````''`
'
$$$$$$$$$$%%%%%;`.``````````````````':'``````.`````.`'''''`......`''':!%%%%%%%%%%%%|!!%%%%|||%%|!|%%%%%%%%%%%%%%%%%%:`'''';||%%%|:`;||%$$$$$$$$$$$$&&&&&&&$|'...```'''`
'
|%%$$%%$$$$$%%%%%%%;`..`.`````````````':'````.``.```..`''````''''':|!:|%%%%%%%%%%%%|!!|%|||%||!!%%%%%%%%%%%%%%%%%%%:`'''':|%%%%;``;|%%$$$$$$$$$$&&&&&&&&&&&$$;...`````''`
'
@$|||||%%%%%%%%%%%%%%%%|:`....```````.`''``'''`````````.````''':;|%|;:|%%%%%%%%%%||!!|||||||!!|%%%%%%%%%%%%%%%%%%%:`'''';|%%%%:.'!|%$$$$$$$$$$$$$&&&&&&&&&&&&$!`..``````''`
'
||||&@&%|!|||%%%%%%%%%%%%%%|:.....```````'````.........`!%%%%%%%%|||;:|%%%%%%%%%%||!!|%|%|!!||%%%%%%%%%%%%%%%%%%%:`'''';|%%%!'`;||%$$$$$$$&&$$$$$&&&&&&@&&&&&$$!`..``````````.
'
|||||!|!|$@&%|!|||%%%%%%%%%%%%%:.
...`````''``........:|%%%%%%%%%||%;:|%%%%%%%$%%%%!;|||!!||||%%%%%%%%%%%%%%%%%%:`'''';||%|:`:||%%$$$&&&&&$$%%$$&&&&@######&&$$$!`..``..```````.
'
||||||||!|!!!!|%$%|||||%$%%%%%%%%%!`.....````'`......'|%%%%%%%%%%|%$|;%&&&&&&&&$$$$|!|!!%$$$$&&&&&&&&&$$$$$$$$%;`''':;|||;``;||%$$&&&&&&&$$%%$$&&&&&@######&$$$$%:.....``````````.
'
||%%||||||!!|!!|||!|%$%|||%%%%%%%%%%%|:.....`.`'`...'|%%%%%%%%%$$$$&|:|&&&&&&&&$$$$|;!|$$$$&&&&&&&&&&&&&&&&@&$;`''':;||;``!||%$&$$$&&&&$%%%%%$&#####@@###@&$$&$$$!`.......`````````:
   .:!|%%|||||||||||||||!|||||%%%%$%%%%%;`.......``:%&&&&&&&&&&&&$$$%;;$&&&&&&&&&&$||$$$&&@@@@@@@@@@@@@@@@@@&!'''':;!:`'!||%$&$$$$$&&&$%%|%%$@######@&&&&&$$$$$$$%;..`...`...`````':
    .....`;|%%%%%|||!|||!|||!!!||%%%%%%%%%%;`.....:%&&&&&&&&&&&&$$$&$|'!$&&&&&&&$$%$$$&&@@@@@@@@@@@@@@@@@@@@!'::':'`.'|||%$$&&$$$$$&&$%%||%$@#########@@&&$$$$&$%$!`..`...```..````:
    ..........'!|%|%|||||!||||||||||%%%%%%%%%:...'|&&&&&&&&&&&&&$$&&&$;'|&&&&&$%%$$$&&@@@@@@@@@@@@@@@@@##@@!'::::'`:|%%%$$$&&&$$$$$$$%%|%&@@##########@@@@&&&$$$$$%;..``....````.``:
    .....```......`;|%|%%||||||||||||||%%%%%%|!:`!&&&&&&&&&&&&&&$$$&&$%::%&$$%%$$&&&@@@@@@@@@@@@@@@@@@@#@&|::::::;|%%$$$$&&&&&$$$$$$%%%%&@@@###########@@@@&&&&&$$$!`..`.....`````.'
    ..................`;|%%%|%%|||!|||||||%%%|||$&&&&&&&&&&&@&&&$$$&&&$%;;%%%$$&&@@@@@@@@@@@@@@@@@##@#@&$;':::::!%%$$$$&&&&&&&$$$%%%%%|$@@@@##@#########@@@@@&&&&$$|:...`.....`````:
    ............``..```...`;|%|||%||!!|||||||||%&&&&&&&&&&@@@@&&$$$&&&&$%;;%$$&@@@@@@@@@@@@@@@@@@#@@&$!`.`:::::!%$$$&&&&&&&&$$$$$$%%%||&@@@@##@@@@########@@@@&&&&$$!`...`.....````:
    ...................```...`:|%||||||||!||||%&&&&&&&&&&&@@@@@&$%&&$$$%%%;:!$&@@@@@@@@@@@@@@#@@&&%;``;|;:::::!$$$&&&&&&&&&&$$$$$%%%%||&@@@@@@@@@@##@#####@@@@@&&&&&$:..```.....```:
    .....................``.`...`;|%|||||||||%&&&&&&&&&&&&@@@@@@$$&$$%%$$$%!::|&@@@@@####@@@&$%;'':|%|!::::::!$$&&&&&@&&&&&&$$$$$$%%|||$@@@@@@@@@@@@@@@####@@@@@&&&&&%'.```..`...``:
    ...........................``..'!%|||||!%$&&&&&&&&&&&&@@@@@&$$$%%$$@@@&$%!::|&&&&&&&&$|;::;||||%%|::::::!$$&&&&@@@&&&&&&$$$$$$%%|||$@@@@&&&&&&&@@@@@@@@@@@@@@&&&$$|'.```.......:
    ...........................```...`;||||%$&&&&&&&&&&&&@@@@@&$%%$$&@@@@&&&&$|!!;;!||;:;!|%%||%$&&$|;:::::!$$&&&@@@@@@@&&&&$$$$$$%%%%|%&@@@&&&&&&&&&&&&&&&&&@@@&&&&$$$!`.``.......:
*/

const int Inf = 0x3f3f3f3f;

namespace MinCostFl {
struct Edge {
  int to, cap, cost, rev;
  Edge(int to, int cap, int cost, int rev)
      : to(to), cap(cap), cost(cost), rev(rev) {}
};
vector<Edge> egs[1005];
int T;
int h[1005], dis[1005], prv[1005], pre[1005];
int iter[1005];
bool lgl[1005];
int res;
void newedge(int u, int v, int cap, int cost) {
  egs[u].emplace_back(v, cap, cost, egs[v].size());
  egs[v].emplace_back(u, 0, -cost, egs[u].size() - 1);
}
int dijk() {
  int F = 0, C = 0;
  priority_queue<pair<int, int>> pq;
  fill(dis, dis + T + 1, Inf);
  dis[0] = 0;
  pq.emplace(0, 0);
  while (pq.size()) {
    pair<int, int> X = pq.top();
    pq.pop();
    int now = X.second;
    if (dis[now] != -X.first) {
      continue;
    }
    for (int i = 0; i < egs[now].size(); i++) {
      Edge &e = egs[now][i];
      if (e.cap && dis[e.to] > dis[now] + e.cost + h[now] - h[e.to]) {
        dis[e.to] = dis[now] + e.cost + h[now] - h[e.to];
        prv[e.to] = now;
        pre[e.to] = i;
        pq.emplace(-dis[e.to], e.to);
      }
    }
  }
  return dis[T] != Inf;
}
int dfs(int now, int cap) {
  if (now == T) {
    return cap;
  }
  lgl[now] = 1;
  int ans = 0;
  for (int i = 0; i < egs[now].size(); i++) {
    Edge &e = egs[now][i];
    if (!lgl[e.to] && e.cap &&
        dis[e.to] == dis[now] + e.cost + h[now] - h[e.to]) {
      int x = dfs(e.to, min(cap - ans, e.cap));
      if (x) {
        res += x * e.cost;
        e.cap -= x;
        egs[e.to][e.rev].cap += x;
        ans += x;
      }
    }
  }
  lgl[now] = 0;
  return ans;
}
pair<int, int> flow() {
  fill(h, h + T + 1, 0);
  int ans = 0;
  res = 0;
  while (dijk()) {
    fill(iter, iter + T + 1, 0);
    int x;
    while ((x = dfs(0, Inf))) {
      ans += x;
    }
  }
  return make_pair(ans, res);
}
} // namespace MinCostFl

using namespace MinCostFl;
int n;
int s[505], t[505];
unsigned long long u[505], v[505];
unsigned long long ans[505][505];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", s + i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", t + i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%llu", u + i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%llu", v + i);
  }
  T = 2 * n + 1;
  for (int b = 0; b < 64; b++) {
    for (int i = 0; i <= T; i++) {
      egs[i].clear();
    }
    int delta = 0;
    for (int i = 1; i <= n; i++) {
      if (!s[i] && !((u[i] >> b) & 1)) {
        newedge(0, i, n - 1, 1);
      } else if (!s[i] && ((u[i] >> b) & 1)) {
        newedge(0, i, n, 0);
        delta += n;
      } else if (s[i] && ((u[i] >> b) & 1)) {
        newedge(0, i, 1, 0);
        newedge(0, i, n - 1, 1);
        delta++;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (!t[i] && !((v[i] >> b) & 1)) {
        newedge(i + n, T, n - 1, 1);
      } else if (!t[i] && ((v[i] >> b) & 1)) {
        newedge(i + n, T, n, 0);
        delta += n;
      } else if (t[i] && ((v[i] >> b) & 1)) {
        newedge(i + n, T, 1, 0);
        newedge(i + n, T, n - 1, 1);
        delta++;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        newedge(i, j + n, 1, 0);
      }
    }
    pair<int, int> res = flow();
    if (res.second + delta != 2 * res.first) {
      puts("-1");
      return 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < egs[i].size(); j++) {
        Edge &e = egs[i][j];
        if (e.to && !e.cap) {
          ans[i][e.to - n] |= (1ull << b);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      printf("%llu%c", ans[i][j], j == n ? '\\n' : ' ');
    }
  }
  return 0;
}