#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
struct data {
  long long v;
  int id;
} a[30010], b[30010];
int n, q, nowa[30010], nowb[30010], ban[30010];
long long f[30010], w1[30010], w2[30010], w3[30010];
bool cmp(data a, data b) { return a.v < b.v; }
void calc(int i) {
  w1[i] = w2[i] = w3[i] = -inf;
  if (i >= 1 && ban[i] != i) w1[i] = a[i].v * b[i].v;
  if (i >= 2 && ban[i] != i - 1 && ban[i - 1] != i)
    w2[i] = a[i].v * b[i - 1].v + a[i - 1].v * b[i].v;
  if (i >= 3) {
    if (ban[i] != i - 2 && ban[i - 1] != i && ban[i - 2] != i - 1)
      w3[i] =
          a[i].v * b[i - 2].v + a[i - 1].v * b[i].v + a[i - 2].v * b[i - 1].v;
    if (ban[i] != i - 1 && ban[i - 1] != i - 2 && ban[i - 2] != i)
      w3[i] = max(w3[i], a[i].v * b[i - 1].v + a[i - 1].v * b[i - 2].v +
                             a[i - 2].v * b[i].v);
  }
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i].v), a[i].id = i;
  for (int i = 1; i <= n; ++i) scanf("%I64d", &b[i].v), b[i].id = i;
  sort(a + 1, a + n + 1, cmp);
  sort(b + 1, b + n + 1, cmp);
  for (int i = 1; i <= n; ++i) nowa[a[i].id] = i, nowb[b[i].id] = i;
  for (int i = 1; i <= n; ++i) ban[i] = nowb[a[i].id];
  for (int i = 1; i <= n; ++i) calc(i);
  for (int j = 1; j <= q; ++j) {
    int x, y;
    scanf("%d%d", &x, &y);
    x = nowa[x];
    y = nowa[y];
    swap(ban[x], ban[y]);
    for (int i = max(1, x - 5); i <= min(n, x + 5); i++) calc(i);
    for (int i = max(1, y - 5); i <= min(n, y + 5); i++) calc(i);
    f[0] = 0;
    for (int i = 1; i <= n; i++) {
      if (i >= 1) f[i] = f[i - 1] + w1[i];
      if (i >= 2) f[i] = max(f[i], f[i - 2] + w2[i]);
      if (i >= 3) f[i] = max(f[i], f[i - 3] + w3[i]);
    }
    printf("%I64d\n", f[n]);
  }
}
