-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChef Segment
59 lines (51 loc) Β· 1.22 KB
/
Chef Segment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const string YES = "YES";
const string NO = "NO";
void solve() {
int n;
cin >> n;
vector<int> a(n);
map<int, vector<int>> factors;
unordered_map<int, int> dp;
for (int i = 0; i < n; i++) {
cin >> a[i];
int x = a[i];
if (factors.find(x) == factors.end()) {
for (int d = 1; d * d <= x; d++) {
if (x % d == 0) {
factors[x].push_back(d);
if ((x / d) != d) {
factors[x].push_back(x / d);
}
}
}
sort(factors[x].begin(), factors[x].end());
}
}
ll ans = 0;
sort(a.rbegin(), a.rend());
for (int i = 0; i < n; i++) {
vector<int> f = factors[a[i]];
for (auto &d: f) {
dp[d] = max(dp[d], 1 + dp[a[i]]);
}
}
for (int i = 0; i < n; i++) {
ans = max(ans, 1LL * dp[a[i]] * a[i]);
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}