WA3个点,难受
#include <bits/stdc++.h>
#define el "\n"
#define sp " "
#define fi first
#define se second
#define inf 1e18
#define r0 return 0
#define int long long
#define F(i, a, b, c) for (int i = a; i <= b; i += c)
#define debug printf ("Yuexingfei_qwq\n")
#define TheEnd continue
#define base(s) s = sp + s
using namespace std;
typedef long long ll;
typedef string str;
using ull = unsigned ll;
int n, m;
int ans = 0, s = 0;
struct Info {
int x, a;
} a[210000];
inline bool cmp(Info x, Info y) {
return x.x < y.x;
}
signed main(void) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a[i].x;
}
for (int i = 1; i <= m; i++) {
cin >> a[i].a;
s += a[i].a;
}
sort(a + 1, a + m + 1, cmp);
for (int i = 1; i < m; i++) {
if (a[i].a > a[i + 1].x - a[i].x) {
int p = a[i].a - (a[i + 1].x - a[i].x);
a[i + 1].a += a[i].a - (a[i + 1].x - a[i].x);
a[i].a = a[i + 1].x - a[i].x;
ans += (a[i + 1].x - a[i].x) * p;
}
}
for (int i = 1; i < m; i++) {
if (a[i + 1].x - a[i].x > a[i].a) {
cout << -1 << el;
r0;
}
}
if (n - a[m].x > a[m].a) {
cout << -1 << el;
r0;
}
if (s != n) {
cout << -1 << el;
r0;
}
for (int i = 1; i < m; i++) {
int y = a[i + 1].x - a[i].x - 1;
ans += (y + 1) * y / 2;
}
int y = n - a[m].x;
if (y > 0) {
ans += (y + 1) * y / 2;
}
cout << ans << el;
r0;
}