#include #include #include #include using namespace std; using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) template using nega_queue = priority_queue, greater>; const i64 INF = 1001001001001001001; int main(){ int N, M; cin >> N >> M; vector> E(N); rep(i,M){ int u,v; cin >> u >> v; u--; v--; E[u].push_back(v); E[v].push_back(u); } vector A[2]; rep(t,2) A[t].resize(N); rep(t,2) rep(i,N) cin >> A[t][i]; bool is_bipartite = true; { vector C(N, -1); C[0] = 0; vector bfs = {0}; rep(i,bfs.size()){ int p = bfs[i]; for(int e : E[p]) if(C[e] == -1){ bfs.push_back(e); C[e] = C[p] ^ 1; } for(int e : E[p]) if(C[e] == C[p]){ is_bipartite = false; } } } if(is_bipartite){ vector C(N, -1); C[0] = 0; vector bfs = {0}; rep(i,bfs.size()){ int p = bfs[i]; for(int e : E[p]) if(C[e] == -1){ bfs.push_back(e); C[e] = C[p] ^ 1; } } i64 sc[2] = {}; rep(i,N) rep(t,2) sc[t] = A[C[i]^t][i]; i64 ans = max(sc[0], sc[1]); cout << ans << '\n'; return 0; } vector maxA(N); rep(i,N) maxA[i] = max(A[0][i], A[1][i]); i64 ans = 0; i64 full = 0; rep(i,N) full += maxA[i]; vector>> adj(N*2); vector cost(N*2); rep(t,2) rep(u,N) cost[N*t+u] = maxA[u] - A[t][u]; rep(u,N) for(int v : E[u]){ adj[u].push_back(make_pair(N+v, cost[N+v])); adj[N+u].push_back(make_pair(v, cost[v])); } int anss = -1; rep(s,N*2){ int t = (s+N) % (N*2); vector D(N*2, INF); nega_queue> Que; D[s] = 0; Que.push(make_pair(D[s], s)); while(Que.size()){ auto [d,p] = Que.top(); Que.pop(); if(D[p] != d) continue; for(auto [to, dd] : adj[p]){ i64 nxd = d + dd; if(D[to] <= nxd) continue; D[to] = nxd; Que.push(make_pair(D[to], to)); } } if(ans < full - D[t]) anss = s; ans = max(ans, full - D[t]); } cout << ans << '\n'; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } ios_do_not_sync_instance;