#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 30005;
struct Node {
  ll v;
  int id;
} a[N], b[N];
int n, q, pa[N], pb[N], vis[N];
ll dp[N], wa[N], wb[N], wc[N];
void calc(int p) {
  wa[p] = wb[p] = wc[p] = INT_MIN >> 1;
  if (p && vis[p] != p) wa[p] = a[p].v * b[p].v;
  if (p > 1 && vis[p] != p - 1 && vis[p - 1] != p)
    wb[p] = a[p].v * b[p - 1].v + a[p - 1].v * b[p].v;
  if (p > 2) {
    if (vis[p] != p - 2 && vis[p - 1] != p && vis[p - 2] != p - 1)
      wc[p] =
          a[p].v * b[p - 2].v + a[p - 1].v * b[p].v + a[p - 2].v * b[p - 1].v;
    if (vis[p] != p - 1 && vis[p - 1] != p - 2 && vis[p - 2] != p)
      wc[p] = max(wc[p], a[p].v * b[p - 1].v + a[p - 1].v * b[p - 2].v +
                             a[p - 2].v * b[p].v);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) cin >> a[i].v, a[i].id = i;
  for (int i = 1; i <= n; i++) cin >> b[i].v, b[i].id = i;
  sort(a + 1, a + n + 1, [](Node a, Node b) { return a.v < b.v; });
  sort(b + 1, b + n + 1, [](Node a, Node b) { return a.v < b.v; });
  for (int i = 1; i <= n; i++) pa[a[i].id] = i, pb[b[i].id] = i;
  for (int i = 1; i <= n; i++) vis[i] = pb[a[i].id];
  for (int i = 1; i <= n; i++) calc(i);
  while (q--) {
    int x, y;
    cin >> x >> y;
    x = pa[x];
    y = pa[y];
    swap(vis[x], vis[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);
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
      dp[i] = dp[i - 1] + wa[i];
      if (i > 1) dp[i] = max(dp[i], dp[i - 2] + wb[i]);
      if (i > 2) dp[i] = max(dp[i], dp[i - 3] + wc[i]);
    }
    cout << dp[n] << endl;
  }
  return 0;
}
