Skip to content

Commit

Permalink
Create Chef Segment
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhiupman568 authored Sep 29, 2023
1 parent 4c2e3f8 commit 105f646
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Chef Segment
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}

0 comments on commit 105f646

Please sign in to comment.