#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * power(a * a % md, b / 2, md) % md
                      : power(a * a % md, b / 2, md) % md));
}
const int xn = 3e4 + 10;
const int xm = -20;
const int sq = 320;
const int inf = 1e9 + 10;
const long long INF = 1e18 + 10;
const int mod = 1e9 + 7;
const int base = 257;
long long n, q, match[xn], seg[xn << 2][3][3];
long long p[4][6][3], fact[4];
pair<long long, long long> P[xn], a[xn], b[xn];
long long solve(int l, int r) {
  if (r - l > 3) return -INF;
  if (r == l) return 0;
  long long ans = -INF;
  for (int i = 0; i < fact[r - l]; ++i) {
    bool flag = true;
    long long res = 0;
    for (int j = l; j < r; ++j) {
      flag &= (match[j] != l + p[r - l][i][j - l]);
      res += a[j].first * b[l + p[r - l][i][j - l]].first;
    }
    if (flag) ans = max(ans, res);
  }
  return ans;
}
void merge(int id, int l, int r) {
  int mid = l + r >> 1;
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      seg[id][i][j] = -INF;
      if (i + j > r - l) continue;
      if (r - l - i - j < 4) {
        seg[id][i][j] = solve(l + i, r - j);
        continue;
      }
      for (int ii = 0; ii < 3; ++ii)
        for (int jj = 0; jj < 3; ++jj)
          seg[id][i][j] =
              max(seg[id][i][j], seg[id << 1][i][ii] + seg[id << 1 | 1][jj][j] +
                                     solve(mid - ii, mid + jj));
    }
  }
}
void build(int id, int l, int r) {
  if (r - l == 1) {
    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j) seg[id][i][j] = -INF;
    seg[id][1][0] = seg[id][0][1] = 0;
    seg[id][0][0] = solve(l, r);
    return;
  }
  int mid = l + r >> 1;
  build(id << 1, l, mid);
  build(id << 1 | 1, mid, r);
  merge(id, l, r);
}
void modify(int id, int l, int r, int pos) {
  if (r - l == 1) {
    seg[id][0][0] = solve(l, r);
    return;
  }
  int mid = l + r >> 1;
  if (pos < mid)
    modify(id << 1, l, mid, pos);
  else
    modify(id << 1 | 1, mid, r, pos);
  merge(id, l, r);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  fact[1] = 1, fact[2] = 2, fact[3] = 6;
  p[1][0][0] = 0;
  p[2][0][0] = 0, p[2][0][1] = 1;
  p[2][1][0] = 1, p[2][1][1] = 0;
  p[3][0][0] = 0, p[3][0][1] = 1, p[3][0][2] = 2;
  p[3][1][0] = 0, p[3][1][1] = 2, p[3][1][2] = 1;
  p[3][2][0] = 1, p[3][2][1] = 0, p[3][2][2] = 2;
  p[3][3][0] = 1, p[3][3][1] = 2, p[3][3][2] = 0;
  p[3][4][0] = 2, p[3][4][1] = 0, p[3][4][2] = 1;
  p[3][5][0] = 2, p[3][5][1] = 1, p[3][5][2] = 0;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) cin >> a[i].first, a[i].second = i;
  for (int i = 1; i <= n; ++i) cin >> b[i].first, b[i].second = i;
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i) P[a[i].second].first = P[b[i].second].second = i;
  for (int i = 1; i <= n; ++i) match[P[i].first] = P[i].second;
  build(1, 1, n + 1);
  while (q--) {
    int x, y;
    cin >> x >> y;
    swap(match[P[x].first], match[P[y].first]);
    modify(1, 1, n + 1, P[x].first);
    modify(1, 1, n + 1, P[y].first);
    cout << seg[1][0][0] << '\n';
  }
  return 0;
}
