6
12 8 12 17 20 5
17 4 8 8 8 4
 6
4 1 12 9 3 11
12 20 19 12 19 11
 1
1
1
 2
1 2
1 1
 1
7
5
 6
16 20 18 7 14 2
17 12 4 10 7 4

705
 917
 1
 9
 5
 741

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long int> vec1, vec2;
  stack<long long int> st;
  vector<long long int> dp;
  long long int a, b;
  for (int i = 0; i < n; i++) {
    cin >> a;
    vec1.push_back(a);
  }
  for (int i = 0; i < n; i++) {
    cin >> b;
    vec2.push_back(b);
  }
  for (int i = 0; i < n; i++) {
    if (vec1[i] < vec2[i]) {
      dp.push_back(vec1[i]);
      if (i != n - 1 && vec2[i] > vec1[i + 1]) {
        st.push(vec2[i]);
      } else {
        dp.push_back(vec2[i]);
      }
    } else {
      dp.push_back(vec2[i]);
      if (i != n - 1 && vec1[i] > vec2[i + 1]) {
        st.push(vec1[i]);
      } else
        dp.push_back(vec1[i]);
    }
  }
  while (!st.empty()) {
    dp.push_back(st.top());
    st.pop();
  }
  long long int sum = 0;
  for (int i = 0; i < dp.size(); i++) {
    sum += dp[i] * i;
  }
  cout << sum << " ";
}
