#include using namespace std; #define REP(i,n) for(int i=0;i<(int)(n);++i) #define REPR(i,n) for (int i=(int)(n)-1;i>=0;--i) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define ALL(c) (c).begin(), (c).end() #define valid(y,x,h,w) (0<=y&&y pii; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (bbasic_ostream& operator<<(basic_ostream&os, const C& c){os<<'[';for(auto i=begin(c);i!=end(c);++i)os<<(i==begin(c)?"":" ")<<*i;return os<<']';} templateostream&operator<<(ostream &o,const pair&t){return o<<'('<void output(ostream&,const Tp&){} templatevoid output(ostream &o,const Tp&t){if(N)o<<',';o<(t);output(o,t);} templateostream&operator<<(ostream&o,const tuple&t){o<<'(';output<0,tuple,Ts...>(o,t);return o<<')';} templatevoid output(T t,char z=10){if(t<0)t=-t,putchar(45);int c[20]; int k=0;while(t)c[k++]=t%10,t/=10;for(k||(c[k++]=0);k;)putchar(c[--k]^48);putchar(z);} templatevoid outputs(T t){output(t);} templatevoid outputs(S a,T...t){output(a,32);outputs(t...);} templatevoid output(T *a,int n){REP(i,n)cout<void output(T *a,int n,int m){REP(i,n)output(a[i],m);} templatebool input(T &t){int n=1,c;for(t=0;!isdigit(c=getchar())&&~c&&c-45;); if(!~c)return 0;for(c-45&&(n=0,t=c^48);isdigit(c=getchar());)t=10*t+c-48;t=n?-t:t;return 1;} templatebool input(S&a,T&...t){input(a);return input(t...);} templatebool inputs(T *a, int n) { REP(i,n) if(!input(a[i])) return 0; return 1;} typedef int Weight; struct Edge { int src, dst; Weight capacity, cost; int rev; Edge(int src, int dst, Weight capacity, Weight cost) : src(src), dst(dst), capacity(capacity), cost(cost) { } Edge(int src, int dst, Weight capacity, Weight cost, int rev) : src(src), dst(dst), capacity(capacity), cost(cost), rev(rev) { } }; bool operator < (const Edge &e, const Edge &f) { return e.cost != f.cost ? e.cost > f.cost : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector Edges; typedef vector Graph; typedef vector Array; typedef vector Matrix; void add_edge(Graph &g, int s, int d, Weight cap, Weight cost) { g[s].push_back(Edge(s,d,cap,cost)); g[d].push_back(Edge(d,s,0,-cost)); } #define RESIDUE(u,v) (capacity[u][v] - flow[u][v]) #define RCOST(u,v) (cost[u][v] + h[u] - h[v]) // Dijkstra pair minimumCostFlow(const Graph &g, int s, int t) { const int n = g.size(); Matrix capacity(n, Array(n)), cost(n, Array(n)), flow(n, Array(n)); REP(u,n) FOR(e,g[u]) { capacity[e->src][e->dst] += e->capacity; cost[e->src][e->dst] += e->cost; } pair total; // (cost, flow) vector h(n, INF); h[s] = 0; // ベルマンフォードでポテンシャルを求めて負辺に対応 REP(k, n) REP(i, n) FOR(e,g[i]) if (capacity[e->src][e->dst]) h[e->dst] = min(h[e->dst], h[e->src] + cost[e->src][e->dst]); for (Weight F = INF; F > 0; ) { // residual flow vector d(n, INF); d[s] = 0; vector p(n, -1); priority_queue Q; for (Q.push(Edge(-2, s, 0, 0)); !Q.empty(); ) { Edge e = Q.top(); Q.pop(); if (p[e.dst] != -1) continue; p[e.dst] = e.src; FOR(f, g[e.dst]) if (RESIDUE(f->src, f->dst) > 0) { if (d[f->dst] > d[f->src] + RCOST(f->src, f->dst)) { d[f->dst] = d[f->src] + RCOST(f->src, f->dst); Q.push( Edge(f->src, f->dst, 0, d[f->dst]) ); } } } if (p[t] == -1) break; Weight f = F; for (int u = t; u != s; u = p[u]) f = min(f, RESIDUE(p[u], u)); for (int u = t; u != s; u = p[u]) { total.first += f * cost[p[u]][u]; flow[p[u]][u] += f; flow[u][p[u]] -= f; } F -= f; total.second += f; REP(u, n) if (h[u] != INF) h[u] += d[u]; // ifいらない? } return total; } int B[50]; int D[50]; int l1[50], r1[50]; int l2[50], r2[50]; int main() { int n; while(input(n)) { int A; cin >> A; inputs(B,A); int C; cin >> C; inputs(D,C); REP(i,n) { l1[i] = (i / A) * A; r1[i] = (i / A + 1) * A; l2[i] = (i / C) * C; r2[i] = (i / C + 1) * C; } int N = r1[n-1]; int M = r2[n-1]; set S; REP(i,N) { for (int j=l2[i]; j D[y%C])); } cout << -minimumCostFlow(g,N+M,N+M+1).first << endl; } }