Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef int_fast64_t lli;
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- lli n, m;
- cin >> n >> m;
- vector<lli> a(n);
- vector<lli> b(m);
- for(lli i = 0; i < n; i++)
- {
- cin >> a[i];
- }
- for(lli i = 0; i < m; i++)
- {
- cin >> b[i];
- b[i]--;
- }
- lli ans = 0;
- lli acc = 0;
- for(lli i = m-1; i >= 0; i--)
- {
- if(a[b[i]] > 0 && acc > 0)
- {
- a[b[i]] = -1;
- ans += 2;
- acc--;
- }
- else
- {
- acc++;
- }
- }
- lli c = 0;
- for(lli i = 0; i < n; i++)
- {
- c += a[i]+1;
- }
- if(c > acc)
- {
- cout << -1 << '\n';
- }
- else
- {
- cout << ans + c << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement