#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int LG = 17;
using ii = pair<int, int>;
using ll = long long;
ii bears[30004];
ii horses[30004];
int BearsPos[30004];
int HorsesPos[30004];
ll cost[30004][2];
ll dp[30004];
int n, q;
ll assign(int bear, int horse) {
  if (min(bear, horse) <= 0 || max(bear, horse) > n) return -1e16;
  return 1ll * bears[bear].first * horses[horse].first;
}
void upd(int i) {
  if (i <= 0 || i > n) return;
  cost[i][0] = assign(i, i + 1) + assign(i + 1, i);
  cost[i][1] = max(assign(i, i + 1) + assign(i + 1, i + 2) + assign(i + 2, i),
                   assign(i, i + 2) + assign(i + 1, i) + assign(i + 2, i + 1));
}
int main() {
  cin >> n >> q;
  for (int i = 1; i < n + 1; i++) {
    cin >> bears[i].first;
    bears[i].second = i;
  }
  for (int i = 1; i < n + 1; i++) {
    cin >> horses[i].first;
    horses[i].second = i;
  }
  sort(bears + 1, bears + n + 1);
  sort(horses + 1, horses + n + 1);
  for (int i = 1; i < n + 1; i++) {
    BearsPos[bears[i].second] = i;
    HorsesPos[horses[i].second] = i;
  }
  for (int i = 1; i < n + 1; i++) upd(i);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    swap(horses[HorsesPos[x]].second, horses[HorsesPos[y]].second);
    swap(HorsesPos[x], HorsesPos[y]);
    for (int i = -2; i < 3; i++) {
      upd(BearsPos[x] + i);
      upd(BearsPos[y] + i);
      upd(HorsesPos[x] + i);
      upd(HorsesPos[y] + i);
    }
    dp[n + 1] = 0;
    for (int i = n; i; --i) {
      dp[i] = max(dp[i + 2] + cost[i][0], dp[i + 3] + cost[i][1]);
      if (horses[i].second != bears[i].second)
        dp[i] = max(dp[i], dp[i + 1] + 1ll * horses[i].first * bears[i].first);
    }
    cout << dp[1] << '\n';
  }
  return 0;
}
