#include #include #define MAXV 200001 // For each weight value v, store sum of Y values of items with X_i == v long long sum_at[MAXV]; // Track if any item exists at weight v int has_item[MAXV]; int main(void) { int N, W; scanf("%d %d", &N, &W); int X[N]; long long Y[N]; int max_x = 0; for (int i = 0; i < N; i++) { scanf("%d", &X[i]); if (X[i] > max_x) max_x = X[i]; } for (int i = 0; i < N; i++) { scanf("%lld", &Y[i]); } // Accumulate values at each weight memset(sum_at, 0, sizeof(sum_at)); for (int i = 0; i < N; i++) { sum_at[X[i]] += Y[i]; has_item[X[i]] = 1; } long long ans = 0; // Sieve: for each candidate GCD g >= W, sum all items whose weight is divisible by g for (int g = W; g <= max_x; g++) { long long total = 0; int found = 0; for (int mult = g; mult <= max_x; mult += g) { if (has_item[mult]) { total += sum_at[mult]; found = 1; } } if (found && total > ans) { ans = total; } } printf("%lld\n", ans); return 0; }