#include using namespace std; #include using namespace atcoder; //高速化 struct ponjuice{ponjuice(){cin.tie(0);ios::sync_with_stdio(0);cout<using vc = vector; templateusing vvc = vc>; templateusing vvvc = vvc>; using vi = vc; using vvi = vvc; using vvvi = vvvc; using vl = vc; using vvl = vvc; using vvvl = vvvc; using pi = pair; using pl = pair; using ull = unsigned ll; templateusing priq = priority_queue; templateusing priqg = priority_queue, greater>; // for文 #define overload4(a, b, c, d, e, ...) e #define rep1(n) for(ll i = 0; i < n; i++) #define rep2(i, n) for(ll i = 0; i < n; i++) #define rep3(i, a, b) for(ll i = a; i < b; i++) #define rep4(i, a, b, step) for(ll i = a; i < b; i+= step) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define per1(n) for(ll i = n-1; i >= 0; i--) #define per2(i, n) for(ll i = n-1; i >= 0; i--) #define per3(i, a, b) for(ll i = b-1; i >= a; i--) #define per4(i, a, b, step) for(ll i = b-1; i >= a; i-= step) #define per(...) overload4(__VA_ARGS__, per4, per3, per2, per1)(__VA_ARGS__) #define fore1(a) for(auto&& i : a) #define fore2(i,a) for(auto&& i : a) #define fore3(x,y,a) for(auto&& [x, y] : a) #define fore4(x,y,z,a) for(auto&& [x, y, z] : a) #define fore(...) overload4(__VA_ARGS__, fore4, fore3, fore2, fore1)(__VA_ARGS__) //関数 #define mp make_pair #define mt make_tuple #define a first #define b second #define pb emplace_back #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define si(x) (ll)(x).size() templateinline bool chmax(S& a, T b){return a < b && ( a = b , true);} templateinline bool chmin(S& a, T b){return a > b && ( a = b , true);} templatevoid uniq(vc&a){sort(all(a));a.erase(unique(all(a)),a.end());} templatevc operator++(vc&v,signed){auto res = v;fore(e,v)e++;return res;} templatevc operator--(vc&v,signed){auto res = v;fore(e,v)e--;return res;} templatevc operator++(vc&v){fore(e,v)e++;return v;} templatevc operator--(vc&v){fore(e,v)e--;return v;} //入出力(operator) templateistream&operator>>(istream&is,static_modint&a){ll v;is>>v;a=v;return is;} istream&operator>>(istream&is,modint&a){ll v;cin>>v;a=v;return is;} templateistream&operator>>(istream&is,pair&a){is>>a.a>>a.b;return is;} templateistream&operator>>(istream&is,vc&a){fore(e,a)is>>e;return is;} templateostream&operator<<(ostream&os,static_modinta){return os<ostream&operator<<(ostream&os,pair&a){return os<ostream&operator<<(ostream&os,set&a){fore(it,a){os<ostream&operator<<(ostream&os,multiset&a){fore(it,a){os<ostream&operator<<(ostream&os,map&a){fore(x,y,a){os<ostream&operator<<(ostream&os,unordered_set&a){fore(it,a){os<ostream&operator<<(ostream&os,unordered_map&a){fore(x,y,a){os<ostream&operator<<(ostream&os,vc&a){fore(e,a)os<ostream&operator<<(ostream&os,vvc&a){fore(e,a)os<>a;return a;} vl readvl(ll n){vl a(n);cin>>a;return a;} vvi readg(ll n,ll m,bool bidirected=true){vvi g(n);rep(i,m){ll a,b;cin>>a>>b;a--;b--;g[a].pb(b);if(bidirected)g[b].pb(a);}return g;} vvcreadgc(ll n,ll m,bool bidirected=true){vvc g(n);rep(i,m){ll a,b,c;cin>>a>>b>>c;a--;b--;g[a].pb(b,c);if(bidirected)g[b].pb(a,c);}return g;} vvi readt(ll n,bool bidirected=true){return readg(n,n-1,bidirected);} vvc readtc(ll n,bool bidirected=true){return readgc(n,n-1,bidirected);} inline void yes(){cout << "Yes\n";} inline void no(){cout << "No\n";} inline void yesno(bool y = true){if(y)yes();else no();} inline void print(){cout<inline void print(T a){cout<inline void print(T a,Ts ...b){cout<>t; while(t--)solve(); } #include using ll = long long; using namespace std; const static ll inf = 1e18L; class LiChaoTree { struct Line { ll a, b; Line(ll a, ll b) : a(a), b(b) {} Line() : a(0), b(0) {} inline ll f(ll x) const { return a * x + b; } Line& operator+=(const Line &ln) { if (b >= inf || ln.b >= inf) { a = 0; b = inf; } else { a += ln.a; b += ln.b; } return *this; } bool operator==(const Line &ln) const { return a == ln.a && b == ln.b; } }; const Line line_inf = Line{0, inf}; const Line line_zero = Line{0, 0}; struct Node { Node *left, *right; Line line, lazy; Node(Line line) : left(nullptr), right(nullptr), line(line) {} }; Node *root; ll xl, xr; inline bool compare(Line &l0, Line &l1, ll x) const { return l0.f(x) <= l1.f(x); } inline void _push_line(Node *nd, ll l, ll r) { if (nd == nullptr || nd->line == line_inf) return; ll m = (l + r) / 2; nd->left = _add_line(nd->left, nd->line, l, m); nd->right = _add_line(nd->right, nd->line, m, r); nd->line = line_inf; } inline void _add_lazy(Node *nd, Line &line) { if (nd == nullptr) return; nd->line += line; nd->lazy += line; } inline void _push_lazy(Node *nd, ll l, ll r) { if (nd == nullptr || nd->lazy == line_zero) return; _add_lazy(nd->left, nd->lazy); _add_lazy(nd->right, nd->lazy); nd->lazy = line_zero; } Node* _add_line(Node *nd, Line line, ll l, ll r) { if (l == r) return nullptr; ll m = (l + r) / 2; if (nd == nullptr) return new Node(line); _push_lazy(nd, l, r); bool left = compare(line, nd->line, l); bool mid = compare(line, nd->line, m); bool right = compare(line, nd->line, r); if (mid) { swap(nd->line, line); } if(r-l > 1 && left != right) { if (left != mid) { nd->left = _add_line(nd->left, line, l, m); } else { nd->right = _add_line(nd->right, line, m, r); } } return nd; } Node* _add_val(ll a, ll b, Node *nd, Line &line, ll l, ll r) { if (r <= a || b <= l) { return nd; } if (a <= l && r <= b) { _add_lazy(nd, line); return nd; } _push_lazy(nd, l, r); _push_line(nd, l, r); if (nd == nullptr) { nd = new Node(line_inf); } ll m = (l + r) / 2; nd->left = _add_val(a, b, nd->left, line, l, m); nd->right = _add_val(a, b, nd->right, line, m, r); return nd; } Node* _add_segment_line(ll a, ll b, Node *nd, Line &line, ll l, ll r) { if (r <= a || b <= l) { return nd; } if (a <= l && r <= b) { return _add_line(nd, line, l, r); } _push_lazy(nd, l, r); if (nd == nullptr) { nd = new Node(line_inf); } ll m = (l + r) / 2; nd->left = _add_segment_line(a, b, nd->left, line, l, m); nd->right = _add_segment_line(a, b, nd->right, line, m, r); return nd; } ll _query_min(ll k, ll l, ll r) { Node *nd = root; ll s = inf; while (r - l > 0 && nd != nullptr) { ll m = (l + r) / 2; _push_lazy(nd, l, r); s = min(s, nd->line.f(k)); if (k < m) { r = m; nd = nd->left; } else { l = m; nd = nd->right; } } return s; } public: LiChaoTree(ll xl, ll xr) : xl(xl), xr(xr), root(nullptr) {} // a_i <- min(a_i, a*i + b) for i in [xl, xr) void add_line(ll a, ll b) { Line line = Line{a, b}; root = _add_line(root, line, xl, xr); } // a_i <- min(a_i, a*i + b) for i in [l, r) void add_segment_line(ll a, ll b, ll l, ll r) { Line line = Line{a, b}; root = _add_segment_line(l, r, root, line, xl, xr); } // a_i <- a_i + (a*i + b) for i in [l, r) void add_val(ll a, ll b, ll l, ll r) { Line line = Line{a, b}; root = _add_val(l, r, root, line, xl, xr); } // a_i <- +∞ for i in [l, r) void reset_val(ll l, ll r) { Line line = line_inf; root = _add_val(l, r, root, line, xl, xr); } // get a_i ll query_min(ll x) { return _query_min(x, xl, xr); } }; void solve(){ int n,m; cin>>n >> m; vl a = readvl(n); vl b = readvl(m); vl c = readvl(m); vl z = a; z.emplace_back(-1); rep(i,m){ z.emplace_back(b[i]); } vi za = z_algorithm(z); LiChaoTree tree(0, m+1); tree.add_segment_line(0,0,0,1); rep(i,m){ ll cost = tree.query_min(i); tree.add_segment_line(c[i],(cost-c[i]*i),i,i+za[n+1+i]+1); } print(tree.query_min(m)==inf?-1:tree.query_min(m)); }