#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <string>
#include <map>
#include <math.h>
#include <cmath>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <assert.h>
#include <fstream>
#include <bitset>
#include <iomanip>
typedef long long ll;
using namespace std;
int MOD = (int)1e9;
int MAXN = 1e6;
//classes
//global
vector<vector<int>> dp;
vector<int> vec;
int sol(int left, int right, int lvalue, int rvalue){
if(dp[left][right] != -1) return dp[left][right];
else if(left+1 == right) return lvalue != rvalue;
else if(left == right) return 0;
if(lvalue == rvalue) dp[left][right] = sol(left+1, right-1, vec[left+1], vec[right-1]);
else{
dp[left][right] = 1+ min(sol(left+1, right, lvalue+ vec[left+1], rvalue), sol(left, right-1, lvalue, rvalue+vec[right-1]));
}
return dp[left][right];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//stop
int n;
cin >> n;
vec.resize(n);
dp.resize(n, vector<int>(n, -1));
for(int& i: vec) cin >> i;
cout << sol(0, n-1, vec[0], vec[n-1]) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1368 KB |
Output is correct |
2 |
Correct |
4 ms |
2392 KB |
Output is correct |
3 |
Correct |
5 ms |
3928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3672 KB |
Output is correct |
2 |
Correct |
7 ms |
4440 KB |
Output is correct |
3 |
Correct |
7 ms |
4488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
31 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
30 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
26 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |