#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const long long maxn = 30000 + 100;
const long long MAX_NODE = maxn * 4 + 10;
long long n, q;
struct info {
  long long no, s, wno;
  info(long long _no = 0, long long _s = 0, long long _wno = 0) {
    no = _no;
    s = _s;
    wno = _wno;
  }
};
info h[maxn], w[maxn];
long long posH[maxn], posW[maxn];
long long f[maxn];
long long tree[MAX_NODE][3][3];
long long dp(long long l, long long r) {
  f[l - 1] = 0;
  for (long long i = l; i <= r; i++) f[i] = -1;
  for (long long i = l; i <= r; i++) {
    long long j = i;
    if (j >= l && f[j - 1] != -1 && w[i].no != h[j].no) {
      f[i] = max(f[i], f[j - 1] + (long long)w[i].s * (long long)h[j].s);
    }
    j--;
    if (j >= l && f[j - 1] != -1) {
      if (w[i].no != h[j].no && w[j].no != h[i].no)
        f[i] = max(f[i], f[j - 1] + (long long)w[i].s * (long long)h[j].s +
                             (long long)w[j].s * (long long)h[i].s);
    }
    j--;
    if (j >= l && f[j - 1] != -1) {
      if (w[j].no != h[j + 1].no && w[j + 1].no != h[j + 2].no &&
          w[i].no != h[j].no)
        f[i] = max(f[i], f[j - 1] + (long long)w[j].s * (long long)h[j + 1].s +
                             (long long)w[j + 1].s * (long long)h[j + 2].s +
                             (long long)w[i].s * (long long)h[j].s);
      if (w[j].no != h[i].no && w[j + 1].no != h[j].no &&
          w[i].no != h[j + 1].no)
        f[i] = max(f[i], f[j - 1] + (long long)w[j].s * (long long)h[i].s +
                             (long long)w[j + 1].s * (long long)h[j].s +
                             (long long)w[i].s * (long long)h[j + 1].s);
    }
  }
  return f[r];
}
void pushUp(long long rt, long long l, long long r) {
  long long mid = ((l + r) >> 1), lRt = (rt << 1), rRt = ((rt << 1) | 1);
  if (mid - l + 1 < 4 || r - mid < 4) {
    for (long long i = 0; i <= 2; i++) {
      for (long long j = 0; j <= 2; j++) {
        long long tmp = dp(l + i, r - j);
        tree[rt][i][j] = tmp;
      }
    }
  } else {
    for (long long i = 0; i <= 2; i++)
      for (long long j = 0; j <= 2; j++) tree[rt][i][j] = -1;
    for (long long xa = 0; xa <= 2; xa++) {
      for (long long ya = 0; ya <= 2; ya++) {
        if (tree[lRt][xa][ya] != -1) {
          for (long long xb = 0; xb + ya <= 3 && xb <= 2; xb++) {
            for (long long yb = 0; yb <= 2; yb++) {
              if (tree[rRt][xb][yb] != -1) {
                long long &key = tree[rt][xa][yb];
                long long a = tree[lRt][xa][ya], b = tree[rRt][xb][yb];
                if (ya == 0 && xb == 0) key = max(key, a + b);
                if (ya == 0 || xb == 0) continue;
                if (ya == 1 && xb == 1) {
                  if (w[mid].no != h[mid + 1].no && w[mid + 1].no != h[mid].no)
                    key = max(
                        key, a + b +
                                 (long long)w[mid].s * (long long)h[mid + 1].s +
                                 (long long)w[mid + 1].s * (long long)h[mid].s);
                }
                if ((ya == 1 && xb == 2) || (ya == 2 && xb == 1)) {
                  long long i = mid - ya + 1;
                  long long j = i + 1;
                  long long k = j + 1;
                  if (w[i].no != h[j].no && w[j].no != w[k].no &&
                      w[k].no != h[i].no)
                    key =
                        max(key, a + b + (long long)w[i].s * (long long)h[j].s +
                                     (long long)w[j].s * (long long)h[k].s +
                                     (long long)w[k].s * (long long)h[i].s);
                  if (w[i].no != w[k].no && w[j].no != h[i].no &&
                      w[k].no != h[j].no)
                    key =
                        max(key, a + b + (long long)w[i].s * (long long)h[k].s +
                                     (long long)w[j].s * (long long)h[i].s +
                                     (long long)w[k].s * (long long)h[j].s);
                }
              }
            }
          }
        }
      }
    }
  }
}
void dfs(long long rt, long long l, long long r) {
  if (r - l + 1 < 4) return;
  long long mid = ((l + r) >> 1), lRt = (rt << 1), rRt = ((rt << 1) | 1);
  dfs(lRt, l, mid);
  dfs(rRt, mid + 1, r);
  pushUp(rt, l, r);
}
void initail() { dfs(1, 1, n); }
void change(long long a, long long b, long long c, long long d, long long rt,
            long long l, long long r) {
  if (r - l + 1 < 4) return;
  long long mid = ((l + r) >> 1), lRt = (rt << 1), rRt = ((rt << 1) | 1);
  if ((a >= l && a <= mid) || (b >= l && b <= mid) || (c >= l && c <= mid) ||
      (d >= l && d <= mid)) {
    change(a, b, c, d, lRt, l, mid);
  }
  if ((a >= mid + 1 && a <= r) || (b >= mid + 1 && b <= r) ||
      (c >= mid + 1 && c <= r) || (d >= mid + 1 && d <= r)) {
    change(a, b, c, d, rRt, mid + 1, r);
  }
  pushUp(rt, l, r);
}
void check_dfs(long long rt, long long l, long long r) {
  if (r - l + 1 < 4) return;
  for (long long i = 0; i <= 2; i++)
    for (long long j = 0; j <= 2; j++) {
      if (tree[rt][i][j] != dp(l + i, r - j)) {
        printf("l %I64d r %I64d  i %I64d j %I64d  tree %I64d   %I64d\n", l, r,
               i, j, tree[rt][i][j], dp(l + i, r - j));
      }
    }
  long long mid = ((l + r) >> 1), lRt = (rt << 1), rRt = ((rt << 1) | 1);
  check_dfs(lRt, l, mid);
  check_dfs(rRt, mid + 1, r);
}
void solve() {
  initail();
  while (q--) {
    long long a, b;
    scanf("%I64d%I64d", &a, &b);
    swap(w[posW[a]].no, w[posW[b]].no);
    if (n >= 4) {
      change(posW[a], posW[b], posH[w[posW[a]].no], posH[w[posW[b]].no], 1, 1,
             n);
      printf("%I64d\n", tree[1][0][0]);
    } else {
      printf("%I64d\n", dp(1, n));
    }
  }
}
bool cmp(info a, info b) { return a.s < b.s; }
int main() {
  while (scanf("%I64d%I64d", &n, &q) != EOF) {
    for (long long i = 1; i <= n; i++) {
      scanf("%I64d", &w[i].s);
      w[i].no = i;
      w[i].wno = i;
    }
    for (long long i = 1; i <= n; i++) {
      scanf("%I64d", &h[i].s);
      h[i].no = i;
    }
    sort(w + 1, w + 1 + n, cmp);
    sort(h + 1, h + 1 + n, cmp);
    for (long long i = 1; i <= n; i++) {
      posW[w[i].wno] = i;
      posH[h[i].no] = i;
    }
    solve();
  }
  return 0;
}
