通过测试发现是判 -1 出了问题,但是不会调
#include <bits/stdc++.h>
using namespace std;
#define int ll
typedef long long ll;
typedef pair<int , int> pi;
#define endl '\n'
const int N = 2e5 + 10;
int n , m;
pi a[N];
const __int128 c = 1;
__int128 getsum(int l) {
return c * (l + 1) * l / 2;
}
int sum(int l , int r) {
return getsum(r) - getsum(l - 1);
}
signed main() {
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
cin >> m >> n;
int s = 0;
for (int i = 1; i <= n; i++) cin >> a[i].first;
for (int i = 1; i <= n; i++) cin >> a[i].second , s += a[i].second;
sort(a + 1 , a + 1 + n);
if (s < m || a[1].first != 1) {
cout << -1;
return 0;
}
int lst = m;
int ans = 0;
for (int i = n; i >= 1; i--) {
int len = min((ll)a[i].second , lst - a[i].first + 1);
ans += sum(lst - len + 1 , lst) - len * a[i].first;
lst = lst - len;
}
if (lst != 0) cout << -1;
else {
cout << (ll)ans << endl;
}
return 0;
}