#include #include #include #include #include #include using namespace std; const int MAXN = 5000; const int INF = 1000000000; pair a[MAXN]; long long f[MAXN + 1]; int main() { int tests, n; for (assert(scanf("%d", &tests) == 1 && 1 <= tests && tests <= 5000); tests --;) { assert(scanf("%d", &n) == 1 && 1 <= n && n <= MAXN); for (int i = 0; i < n; ++ i) { assert(scanf("%d", &a[i].second) == 1); assert(0 <= a[i].second && a[i].second <= INF); } for (int i = 0; i < n; ++ i) { assert(scanf("%d", &a[i].first) == 1); assert(0 <= a[i].first && a[i].first <= INF); } // sort by the first key decreasingly sort(a, a + n); reverse(a, a + n); memset(f, 0, sizeof(f)); for (int i = 0; i < n; ++ i) { for (int j = i; j >= 0; -- j) { f[j + 1] = max(f[j + 1], f[j] + (long long)j * a[i].first + a[i].second); } } for (int i = 1; i <= n; ++ i) { printf("%lld%c", f[i], i == n ? '\n' : ' '); } } return 0; }