Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cstdio>
- const int N = 111;
- int n, count[N], g[N][N], left[N], right[N], max_value, ans, f[N];
- void input() {
- std::cin >> n;
- for (int i = 1; i <= n; ++i) {
- int x;
- std::cin >> x;
- right[x] = i;
- if (!left[x]) {
- left[x] = i;
- }
- ++count[x];
- max_value = std::max(max_value, x);
- }
- }
- void build_graph() {
- for (int i = 1; i <= max_value; ++i) {
- for (int j = 1; j <= max_value; ++j) {
- if (count[i] && count[j] && right[i] < left[j]) {
- g[i][j] = 1;
- }
- }
- }
- }
- int calc(int v) {
- if (f[v]) {
- return f[v];
- }
- f[v] = count[v];
- for (int next = 1; next <= max_value; ++next) {
- if (g[v][next]) {
- f[v] = std::max(f[v], count[v] + calc(next));
- }
- }
- return f[v];
- }
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- input();
- build_graph();
- for (int i = 1; i <= max_value; ++i) {
- ans = std::max(ans, calc(i));
- }
- std::cout << ans << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement