#include using namespace std; using int64 = long long; const int mod = 998244353; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 >& p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { explicit FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } #line 1 "string/z-algorithm.hpp" /** * @brief Z Algorithm * @see https://heno239.hatenablog.com/entry/2020/07/07/140651 * @docs docs/z-algorithm.md */ template< typename T = char > struct ZAlgorithm { private: vector< T > s; vector< int > deleted; vector< vector< int > > delete_hist; vector< int > z; queue< int > cur; public: ZAlgorithm() : delete_hist{{}} {} template< typename S > ZAlgorithm(const S &s) : ZAlgorithm() { for(auto &c: s) add(c); } void add(const T &c) { s.emplace_back(c); delete_hist.emplace_back(); deleted.emplace_back(0); z.emplace_back(0); z[0]++; int len = (int) s.size(); if(len == 1) return; if(s[0] == c) cur.emplace(len - 1); else deleted[len - 1] = 1; auto set = [&](int t, int len) { deleted[t] = 1; delete_hist[len].emplace_back(t); z[t] = len - t - 1; }; while(not cur.empty()) { int t = cur.front(); if(deleted[t]) { cur.pop(); } else if(s[len - t - 1] == s.back()) { break; } else { set(t, len); cur.pop(); } } if(not cur.empty()) { int t = cur.front(); for(auto &p: delete_hist[len - t]) { set(p + t, len); } } } int get(int k) const { return deleted[k] ? z[k] : (int) s.size() - k; } int operator[](int k) const { return get(k); } vector< int > get() { vector< int > ret(s.size()); for(int i = 0; i < (int) s.size(); i++) { ret[i] = get(i); } return ret; } }; #line 1 "structure/convex-hull-trick/dynamic-li-chao-tree.hpp" /** * @brief Dynamic-Li-Chao-Tree * @docs docs/dynamic-li-chao-tree.md */ template< typename T, T x_low, T x_high, T id > struct DynamicLiChaoTree { struct Line { T a, b; Line(T a, T b) : a(a), b(b) {} inline T get(T x) const { return a * x + b; } }; struct Node { Line x; Node *l, *r; Node(const Line &x) : x{x}, l{nullptr}, r{nullptr} {} }; Node *root; DynamicLiChaoTree() : root{nullptr} {} Node *add_line(Node *t, Line &x, const T &l, const T &r, const T &x_l, const T &x_r) { if(!t) return new Node(x); T t_l = t->x.get(l), t_r = t->x.get(r); if(t_l <= x_l && t_r <= x_r) { return t; } else if(t_l >= x_l && t_r >= x_r) { t->x = x; return t; } else { T m = (l + r) / 2; if(m == r) --m; T t_m = t->x.get(m), x_m = x.get(m); if(t_m > x_m) { swap(t->x, x); if(x_l >= t_l) t->l = add_line(t->l, x, l, m, t_l, t_m); else t->r = add_line(t->r, x, m + 1, r, t_m + x.a, t_r); } else { if(t_l >= x_l) t->l = add_line(t->l, x, l, m, x_l, x_m); else t->r = add_line(t->r, x, m + 1, r, x_m + x.a, x_r); } return t; } } void add_line(const T &a, const T &b) { Line x(a, b); root = add_line(root, x, x_low, x_high, x.get(x_low), x.get(x_high)); } Node *add_segment(Node *t, Line &x, const T &a, const T &b, const T &l, const T &r, const T &x_l, const T &x_r) { if(r < a || b < l) return t; if(a <= l && r <= b) { Line y{x}; return add_line(t, y, l, r, x_l, x_r); } if(t) { T t_l = t->x.get(l), t_r = t->x.get(r); if(t_l <= x_l && t_r <= x_r) return t; } else { t = new Node(Line(0, id)); } T m = (l + r) / 2; if(m == r) --m; T x_m = x.get(m); t->l = add_segment(t->l, x, a, b, l, m, x_l, x_m); t->r = add_segment(t->r, x, a, b, m + 1, r, x_m + x.a, x_r); return t; } void add_segment(const T &l, const T &r, const T &a, const T &b) { Line x(a, b); root = add_segment(root, x, l, r - 1, x_low, x_high, x.get(x_low), x.get(x_high)); } T query(const Node *t, const T &l, const T &r, const T &x) const { if(!t) return id; if(l == r) return t->x.get(x); T m = (l + r) / 2; if(m == r) --m; if(x <= m) return min(t->x.get(x), query(t->l, l, m, x)); else return min(t->x.get(x), query(t->r, m + 1, r, x)); } T query(const T &x) const { return query(root, x_low, x_high, x); } }; int main() { int N, M; cin >> N >> M; vector< int64 > A(N), B(M), C(M); cin >> A >> B >> C; ZAlgorithm< int > z; for(int i = 0; i < N; i++) z.add(A[i]); z.add(-1); for(int i = 0; i < M; i++) z.add(B[i]); vector< int64 > dp(M + 1); fill_v(dp, infll); dp[0] = 0; DynamicLiChaoTree< int64, 0, 200001, infll > cht; for(int i = 0; i <= M; i++) { chmin(dp[i], cht.query(i)); if(i < M) { int len = z.get(N + 1 + i); dp[i] -= C[i] * i; cht.add_segment(i + 1, i + len + 1, C[i], dp[i]); } } if(dp[M] >= infll) cout << -1 << endl; else cout << dp[M] << endl; }