Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int n = readint(), m = readint();
- pair<int, int> a[100001];
- int main(){
- FOR(i, 1, n + 1) a[readint()].fus = i;
- FOR(i, 1, m + 1) a[i].sec = readint();
- sort(a + 1, a + m + 1);
- int res = a[1].sec + 1, d = a[1].fus - a[1].sec - 1;
- if(d < 0) return cout << -1, 0;
- FOR(i, 2, m + 1){
- res += a[i].sec + 1,
- d += a[i].fus - a[i - 1].fus - a[i].sec - 1;
- if(d < 0) return cout << -1, 0;
- }
- return cout << res, 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement