#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; typedef unsigned long long int ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } inline double time() { return static_cast(chrono::duration_cast( chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } vector Zalgo(vector s){ int n=s.size(); vector a(n,0); int from=-1,last=-1; for(int i=1;i> n >> m; vector a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } a.push_back(-1); for (int i = 0; i < m; ++i) { int x; cin >> x; a.push_back(x); } vector c(m); for (int i = 0; i < m; ++i) { cin >> c[i]; } vector> v; for (int i = 0; i < m; ++i) { v.push_back({c[i], i}); } sort(v.begin(), v.end()); vector uo(m,-1); set st; for (int i = 0; i < m; ++i) { st.insert(i); } auto Z = Zalgo(a); for (auto p:v) { int i = p.second; int len = Z[n+1+i]; while (true) { auto it = st.lower_bound(i+len); if (it == st.begin()) break; it--; if (*it < i) break; assert(uo[*it] == -1); uo[*it] = p.first; st.erase(it); } } if (st.size()) { cout << -1 << endl; return 0; } ll cost = 0; for (int i = 0; i < m; ++i) { ll u = uo[i]; if (u == -1) { cost = -1; break; } cost += u; } cout << cost << endl; }