Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- const int maxn = 250;
- int main() {
- ios_base::sync_with_stdio(false);
- ifstream cin("248.in");
- ofstream cout("248.out");
- int n;
- cin >> n;
- vector<int> cows(n);
- vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
- for(int i = 0; i < n; i++) {
- cin >> cows[i];
- dp[i][i] = cows[i];
- }
- for(int i = n - 1; i >= 0; i--) {
- for(int j = i + 1; j < n; j++) {
- dp[i][j] = -2e9;
- if(i + 1 < n) {
- if(dp[i + 1][j] == cows[i]) {
- dp[i][j] = max(dp[i][j], cows[i] + 1);
- }
- }
- if(j - 1 >= 0) {
- if(dp[i][j - 1] == cows[j]) {
- dp[i][j] = max(dp[i][j], cows[j] + 1);
- }
- }
- for(int k = i + 1; k < j; k++) {
- if(dp[i][k] == dp[k + 1][j] and dp[k + 1][j] != -2e9) {
- dp[i][j] = max(dp[i][j], dp[i][k] + 1);
- }
- }
- }
- }
- int result = 0;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < n; j++) {
- result = max(result, dp[i][j]);
- }
- }
- cout << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement