Submission #64559724
Source Code Expand
Copy
/*inf = 1 << 60n = int(input())c = list(map(int, input().split()))x = list(map(int, input().split()))C = c + cans=inffor s in range(n):goal = C[s : s + n]dp = [[inf] * n for _ in range(n)]for i in range(n):dp[i][i] = 1 + x[goal[i] - 1]for i in range(2, n + 1):for l in range(0, n - i + 1):r = l + i - 1dp[l][r] = min(dp[l][r],dp[l][r - 1] + 1 + x[goal[r] - 1])for k in range(l, r):if goal[k] == goal[r]:c = dp[l][k]+r-kif k + 1 < r:c += dp[k + 1][r - 1]
/*
inf = 1 << 60
n = int(input())
c = list(map(int, input().split()))
x = list(map(int, input().split()))
C = c + c
ans=inf
for s in range(n):
goal = C[s : s + n]
dp = [[inf] * n for _ in range(n)]
for i in range(n):
dp[i][i] = 1 + x[goal[i] - 1]
for i in range(2, n + 1):
for l in range(0, n - i + 1):
r = l + i - 1
dp[l][r] = min(dp[l][r],dp[l][r - 1] + 1 + x[goal[r] - 1])
for k in range(l, r):
if goal[k] == goal[r]:
c = dp[l][k]+r-k
if k + 1 < r:
c += dp[k + 1][r - 1]
if c < dp[l][r]:
dp[l][r] = c
ans=min(dp[0][-1],ans)
print(ans)
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
int main() {
const ll inf = 1LL << 60;
int n;
cin >> n;
vector<int> c(n);
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
vector<int> x(n);
for (int i = 0; i < n; ++i) {
cin >> x[i];
}
vector<int> C(2 * n);
for (int i = 0; i < n; ++i) {
C[i] = c[i];
C[i + n] = c[i];
}
ll ans = inf;
for (int s = 0; s < n; ++s) {
vector<int> goal(C.begin() + s, C.begin() + s + n);
vector<vector<ll>> dp(n, vector<ll>(n, inf));
for (int i = 0; i < n; ++i) {
dp[i][i] = 1 + x[goal[i] - 1];
}
for (int len = 2; len <= n; ++len) {
for (int l = 0; l <= n - len; ++l) {
int r = l + len - 1;
dp[l][r] = min(dp[l][r], dp[l][r - 1] + 1 + x[goal[r] - 1]);
for (int k = l; k < r; ++k) {
if (goal[k] == goal[r]) {
ll cost = dp[l][k] + (r - k);
if (k + 1 < r) {
cost += dp[k + 1][r - 1];
}
dp[l][r] = min(dp[l][r], cost);
}
}
}
}
ans = min(ans, dp[0][n - 1]);
}
cout << ans << '\n';
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | F - Happy Birthday! 3 |
| User | juten |
| Language | C++ 20 (gcc 12.2) |
| Score | 0 |
| Code Size | 2219 Byte |
| Status | TLE |
| Exec Time | 3310 ms |
| Memory | 4612 KB |
Judge Result
| Set Name | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 550 | ||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample00.txt, sample01.txt, sample02.txt |
| All | sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| sample00.txt | AC | 1 ms | 3468 KB |
| sample01.txt | AC | 1 ms | 3456 KB |
| sample02.txt | AC | 1 ms | 3488 KB |
| testcase00.txt | TLE | 3310 ms | 4536 KB |
| testcase01.txt | TLE | 3310 ms | 4584 KB |
| testcase02.txt | TLE | 3310 ms | 4612 KB |
| testcase03.txt | AC | 2080 ms | 4520 KB |
| testcase04.txt | AC | 2082 ms | 4420 KB |
| testcase05.txt | AC | 2083 ms | 4476 KB |
| testcase06.txt | AC | 2025 ms | 3900 KB |
| testcase07.txt | TLE | 3310 ms | 4512 KB |
| testcase08.txt | AC | 1028 ms | 3788 KB |
| testcase09.txt | TLE | 3310 ms | 4608 KB |
| testcase10.txt | AC | 769 ms | 3592 KB |
| testcase11.txt | TLE | 3310 ms | 4516 KB |
| testcase12.txt | AC | 45 ms | 3552 KB |
| testcase13.txt | TLE | 3310 ms | 4472 KB |
| testcase14.txt | AC | 764 ms | 3588 KB |
| testcase15.txt | TLE | 3310 ms | 4612 KB |
| testcase16.txt | AC | 1921 ms | 3864 KB |
| testcase17.txt | TLE | 3310 ms | 4456 KB |
| testcase18.txt | AC | 87 ms | 3692 KB |
| testcase19.txt | TLE | 3310 ms | 4568 KB |
| testcase20.txt | AC | 30 ms | 3536 KB |
| testcase21.txt | TLE | 3310 ms | 4520 KB |
| testcase22.txt | AC | 2376 ms | 3964 KB |
| testcase23.txt | TLE | 3310 ms | 4464 KB |
| testcase24.txt | AC | 509 ms | 3500 KB |
| testcase25.txt | TLE | 3310 ms | 4448 KB |
| testcase26.txt | AC | 874 ms | 3784 KB |
| testcase27.txt | TLE | 3310 ms | 4480 KB |
| testcase28.txt | AC | 55 ms | 3576 KB |
| testcase29.txt | TLE | 3310 ms | 4612 KB |
| testcase30.txt | AC | 21 ms | 3764 KB |
| testcase31.txt | AC | 2200 ms | 4492 KB |
| testcase32.txt | AC | 773 ms | 3904 KB |
| testcase33.txt | AC | 2179 ms | 4436 KB |
| testcase34.txt | AC | 3 ms | 3484 KB |
| testcase35.txt | AC | 2187 ms | 4520 KB |
| testcase36.txt | AC | 299 ms | 3592 KB |
| testcase37.txt | AC | 2210 ms | 4424 KB |
| testcase38.txt | AC | 687 ms | 3964 KB |
| testcase39.txt | AC | 2193 ms | 4468 KB |
| testcase40.txt | AC | 197 ms | 3652 KB |
| testcase41.txt | AC | 2207 ms | 4468 KB |