Advertisement
leminhkt

noname64

Jul 7th, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. int n = readint(), m = readint();
  2. pair<int, int> a[100001];
  3.  
  4. int main(){
  5.     FOR(i, 1, n + 1) a[readint()].fus = i;
  6.     FOR(i, 1, m + 1) a[i].sec = readint();
  7.     sort(a + 1, a + m + 1);
  8.     int res = a[1].sec + 1, d = a[1].fus - a[1].sec - 1;
  9.     if(d < 0) return cout << -1, 0;
  10.     FOR(i, 2, m + 1){
  11.         res += a[i].sec + 1,
  12.         d += a[i].fus - a[i - 1].fus - a[i].sec - 1;
  13.         if(d < 0) return cout << -1, 0;
  14.     }
  15.     return cout << res, 0;
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement