#include using namespace std; #include using namespace atcoder; #define ll long long struct edge { int to, seq; ll cup; }; class max_flow { public: int siz; vector> side; vector dist; max_flow (int n) { siz = n; side.assign(siz, {}); return ; } void set(int pos, int to, ll x) { int a = side[to].size(); int b = side[pos].size(); side[pos].push_back(edge{to, a, x}); side[to].push_back(edge{pos, b, 0}); return ; } void bfs(int s) { dist.assign(siz, -1); dist[s] = 0; queue q; q.push(s); while(!q.empty()) { int pos = q.front(); q.pop(); for(int i=0; i= 0) continue; dist[e.seq] = dist[pos]+1; q.push(e.seq); } } } ll dfs(int pos, int goal, ll F) { if(pos == goal) return F; return 0; } ll len(int s, int t) { ll res = 0; while(1) { bfs(s); if(dist[t] < 0) return res; } return res; } }; /* signed main() { int h, w; cin >> h >> w; int ax = h+w+1; max_flow F(ax+1); vector c(h+1, 0); for(int i=0; i> g; int x = i/w; c[x+1] += g; int y = i%w; F.set(x+1, h+y+1, g); } ll ans = 0; for(int j=1; j<=h; j++) { ll r; cin >> r; r -= c[j]; if(r < 0) F.set(0, j, -r); else { ans += r; F.set(j, ax, r); } } for(int i=1; i<=w; i++) { ll r; cin >> r; F.set(h+i, ax, r); ans += r; } ans -= F.len(0, ax); cout << ans << endl; } */ signed main() { int h, w; cin >> h >> w; int ax = h+w+1; mf_graph F(ax+1); vector c(h+1, 0); for(int i=0; i> g; int x = i/w; c[x+1] += g; int y = i%w; F.add_edge(x+1, h+y+1, g); } ll ans = 0; for(int j=1; j<=h; j++) { ll r; cin >> r; r -= c[j]; if(r < 0) F.add_edge(0, j, -r); else { ans += r; F.add_edge(j, ax, r); } } for(int i=1; i<=w; i++) { ll r; cin >> r; F.add_edge(h+i, ax, r); ans += r; } ans -= F.flow(0, ax); cout << ans << endl; }