#include <bits/stdc++.h>
using namespace std;
inline int re_ad() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
  return x * f;
}
inline int mi(int x, int y) { return x < y ? x : y; }
inline long long ma(long long x, long long y) { return x > y ? x : y; }
int n, q;
struct node {
  int nu, ii;
} a[30010], b[30010];
bool cmp(node x, node y) { return x.nu > y.nu; }
int plaa[30010], plab[30010];
int to[30010];
long long nu1[30010], nu2[30010], nu3[30010];
long long dp[30010];
inline void change(int x) {
  nu1[x] = nu2[x] = nu3[x] = -10000000000000000ll;
  if (to[x] != x) nu1[x] = 1ll * a[x].nu * b[x].nu;
  if (x >= 2 && to[x] != x - 1 && to[x - 1] != x)
    nu2[x] = 1ll * a[x].nu * b[x - 1].nu + 1ll * a[x - 1].nu * b[x].nu;
  if (x >= 3) {
    if (to[x] != x - 2 && to[x - 1] != x && to[x - 2] != x - 1)
      nu3[x] = 1ll * a[x].nu * b[x - 2].nu + 1ll * a[x - 1].nu * b[x].nu +
               1ll * a[x - 2].nu * b[x - 1].nu;
    if (to[x] != x - 1 && to[x - 1] != x - 2 && to[x - 2] != x)
      nu3[x] = ma(nu3[x], 1ll * a[x].nu * b[x - 1].nu +
                              1ll * a[x - 1].nu * b[x - 2].nu +
                              1ll * a[x - 2].nu * b[x].nu);
  }
}
inline void update(int x) {
  dp[x] = dp[x - 1] + nu1[x];
  if (x >= 2) dp[x] = ma(dp[x], dp[x - 2] + nu2[x]);
  if (x >= 3) dp[x] = ma(dp[x], dp[x - 3] + nu3[x]);
}
int main() {
  register int i, j, x, y;
  n = re_ad();
  q = re_ad();
  for (i = 1; i <= n; ++i) a[i].nu = re_ad(), a[i].ii = i;
  for (i = 1; i <= n; ++i) b[i].nu = re_ad(), b[i].ii = i;
  sort(a + 1, a + n + 1, cmp);
  sort(b + 1, b + n + 1, cmp);
  for (i = 1; i <= n; ++i) plaa[a[i].ii] = i, plab[b[i].ii] = i;
  for (i = 1; i <= n; ++i) to[i] = plab[a[i].ii];
  for (i = 1; i <= n; ++i) change(i);
  dp[0] = 0;
  for (i = 1; i <= n; ++i) update(i);
  while (q--) {
    x = plaa[re_ad()];
    y = plaa[re_ad()];
    swap(to[x], to[y]);
    for (i = ma(1, x - 5); i <= mi(n, x + 5); ++i) change(i);
    for (i = ma(1, y - 5); i <= mi(n, y + 5); ++i) change(i);
    for (i = ma(1, mi(x - 2, y - 2)); i <= n; ++i) update(i);
    printf("%lld\n", dp[n]);
  }
  return 0;
}
