#include <bits/stdc++.h>
using namespace std;
struct seg {
  long long dp[3][3];
} S[30005 * 4];
int n, q, x, y;
int match[30005], posa[30005], posb[30005];
pair<int, int> a[30005], b[30005];
void create(int node, int bas) {
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) S[node].dp[i][j] = -20000000000000000;
  S[node].dp[0][1] = S[node].dp[1][0] = 0;
  if (match[bas] != bas) S[node].dp[0][0] = 1ll * a[bas].first * b[bas].first;
}
long long get(int x, int y) {
  if (y - x == 1) {
    if (match[x] != y && match[y] != x)
      return 1ll * a[x].first * b[y].first + 1ll * a[y].first * b[x].first;
    return -20000000000000000;
  } else {
    long long res = -20000000000000000;
    if (match[x] != x + 1 && match[x + 1] != y && match[y] != x)
      res = max(res, 1ll * a[x].first * b[x + 1].first +
                         1ll * a[x + 1].first * b[y].first +
                         1ll * a[y].first * b[x].first);
    if (match[x] != y && match[x + 1] != x && match[y] != x + 1)
      res = max(res, 1ll * a[x].first * b[y].first +
                         1ll * a[x + 1].first * b[x].first +
                         1ll * a[y].first * b[x + 1].first);
    return res;
  }
}
seg merge(seg x, seg y, int bas, int son) {
  seg res;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
      res.dp[i][j] = (i + j == son - bas + 1) ? 0 : -20000000000000000;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (son - j + 1 < ((bas + son) >> 1) + 1)
        res.dp[i][j] =
            max(res.dp[i][j], x.dp[i][j - (son - ((bas + son) >> 1))]);
      if (bas + i - 1 > ((bas + son) >> 1))
        res.dp[i][j] =
            max(res.dp[i][j], y.dp[i - (((bas + son) >> 1) - bas + 1)][j]);
      res.dp[i][j] = max(res.dp[i][j], x.dp[i][0] + y.dp[0][j]);
      res.dp[i][j] = max(res.dp[i][j],
                         x.dp[i][1] + y.dp[1][j] +
                             get(((bas + son) >> 1), ((bas + son) >> 1) + 1));
      res.dp[i][j] = max(res.dp[i][j],
                         x.dp[i][1] + y.dp[2][j] +
                             get(((bas + son) >> 1), ((bas + son) >> 1) + 2));
      res.dp[i][j] = max(res.dp[i][j], x.dp[i][2] + y.dp[1][j] +
                                           get(((bas + son) >> 1) - 1,
                                               ((bas + son) >> 1) + 1));
    }
  }
  return res;
}
void up(int node, int bas, int son, int x, int y) {
  if (bas > y || son < x || (bas > x && son < y)) return;
  if (bas == son) {
    create(node, bas);
    return;
  }
  up(node * 2, bas, ((bas + son) >> 1), x, y);
  up(node * 2 + 1, ((bas + son) >> 1) + 1, son, x, y);
  S[node] = merge(S[node * 2], S[node * 2 + 1], bas, son);
}
void build(int node, int bas, int son) {
  if (bas == son) {
    create(node, bas);
    return;
  }
  build(node * 2, bas, ((bas + son) >> 1));
  build(node * 2 + 1, ((bas + son) >> 1) + 1, son);
  S[node] = merge(S[node * 2], S[node * 2 + 1], bas, son);
}
int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i].first);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i].first);
  for (int i = 1; i <= n; i++) a[i].second = b[i].second = i;
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n);
  for (int i = 1; i <= n; i++) {
    posb[b[i].second] = i;
    posa[a[i].second] = i;
  }
  for (int i = 1; i <= n; i++) {
    match[i] = posb[a[i].second];
  }
  build(1, 1, n);
  while (q--) {
    scanf("%d %d", &x, &y);
    swap(match[posa[x]], match[posa[y]]);
    if (posa[x] > posa[y]) swap(x, y);
    up(1, 1, n, posa[x], posa[y]);
    printf("%lld\n", S[1].dp[0][0]);
  }
}
