problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long dp[405][405]; long long pre[405]; int main() { int N; cin >> N; for (int i = 1; i <= N; i++) { cin >> pre[i]; pre[i] += pre[i - 1]; } for (int len = 2; len <= N; len++) { for (int l = 0; l <= N; l++) { int r = l + len; dp[l][r] = LLONG_MAX / 2 - 5; for (int k = l + 1; k < r; k++) { dp[l][r] = min(dp[l][k] + dp[k][r], dp[l][r]); } dp[l][r] += pre[r] - pre[l]; } } cout << dp[0][N] << endl; }
#include <bits/stdc++.h> using namespace std; long long dp[405][405]; long long pre[405]; int main() { int N; cin >> N; for (int i = 1; i <= N; i++) { cin >> pre[i]; pre[i] += pre[i - 1]; } for (int len = 2; len <= N; len++) { for (int l = 0; l + len <= N; l++) { int r = l + len; dp[l][r] = LLONG_MAX / 2 - 5; for (int k = l + 1; k < r; k++) { dp[l][r] = min(dp[l][k] + dp[k][r], dp[l][r]); } dp[l][r] += pre[r] - pre[l]; } } cout << dp[0][N] << endl; }
replace
15
16
15
16
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a), _b = (b); i < (_b); ++i) #define FORD(i, a, b) for (int i = (a), _b = (b); i > (_b); --i) #define pb push_back #define mp make_pair #define all(c) (c).begin(), (c).end() #define present(c, x) ((c).find(x) != (c).end()) #define cpresent(c, x) (find(all(c), x) != (c).end()) #define endl '\n' typedef long long ll; typedef unsigned long long ull; typedef unsigned char byte; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; const int MX = 405; ll dp[MX][MX]; int main(int argc, char *argv[]) { #ifdef HTRINH_UNIT_TEST freopen(argv[1], "r", stdin); ifstream cin(argv[1]); #endif #if 0 ofstream cout(argv[2]); #endif ios ::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; ll a[n]; FOR(i, 0, n) cin >> a[i]; ll sum[n]; sum[0] = a[0]; FOR(i, 1, n) sum[i] = sum[i - 1] + a[i]; FOR(d, 2, n + 1) FOR(l, 0, n) { int r = l + d - 1; ll tot = 0; if (l - 1 >= 0) tot = sum[r] - sum[l - 1]; else tot = sum[r]; ll best = 1LL << 60; FOR(k, l, r) best = min(best, dp[l][k] + dp[k + 1][r]); dp[l][r] = best + tot; } cout << dp[0][n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a), _b = (b); i < (_b); ++i) #define FORD(i, a, b) for (int i = (a), _b = (b); i > (_b); --i) #define pb push_back #define mp make_pair #define all(c) (c).begin(), (c).end() #define present(c, x) ((c).find(x) != (c).end()) #define cpresent(c, x) (find(all(c), x) != (c).end()) #define endl '\n' typedef long long ll; typedef unsigned long long ull; typedef unsigned char byte; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; const int MX = 405; ll dp[MX][MX]; int main(int argc, char *argv[]) { #ifdef HTRINH_UNIT_TEST freopen(argv[1], "r", stdin); ifstream cin(argv[1]); #endif #if 0 ofstream cout(argv[2]); #endif ios ::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; ll a[n]; FOR(i, 0, n) cin >> a[i]; ll sum[n]; sum[0] = a[0]; FOR(i, 1, n) sum[i] = sum[i - 1] + a[i]; FOR(d, 2, n + 1) FOR(l, 0, n) { int r = l + d - 1; if (r >= n) break; ll tot = 0; if (l - 1 >= 0) tot = sum[r] - sum[l - 1]; else tot = sum[r]; ll best = 1LL << 60; FOR(k, l, r) best = min(best, dp[l][k] + dp[k + 1][r]); dp[l][r] = best + tot; } cout << dp[0][n - 1] << endl; return 0; }
insert
45
45
45
47
0
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)n; i++) typedef long long ll; const int MAX = 401; const ll INF = 1e17; ll sum[MAX] = {}; vector<vector<ll>> dp(MAX, vector<ll>(MAX, -1)); ll dfs(int i, int j) { if (dp[i][j] != -1) return dp[i][j]; if (i == j) return dp[i][j] = 0; ll d = INF; for (int k = i; k < j; k++) { d = min(d, dfs(i, k) + dfs(k + 1, j) + sum[j] - sum[i - 1]); } return d; } int main() { int n; cin >> n; rep(i, n) { ll a; cin >> a; sum[i + 1] += sum[i] + a; } cout << dfs(1, n) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)n; i++) typedef long long ll; const int MAX = 401; const ll INF = 1e17; ll sum[MAX] = {}; vector<vector<ll>> dp(MAX, vector<ll>(MAX, -1)); ll dfs(int i, int j) { if (dp[i][j] != -1) return dp[i][j]; if (i == j) return dp[i][j] = 0; ll d = INF; for (int k = i; k < j; k++) { d = min(d, dfs(i, k) + dfs(k + 1, j) + sum[j] - sum[i - 1]); } return dp[i][j] = d; } int main() { int n; cin >> n; rep(i, n) { ll a; cin >> a; sum[i + 1] += sum[i] + a; } cout << dfs(1, n) << endl; return 0; }
replace
19
20
19
20
TLE
p03173
C++
Runtime Error
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; ///////////////////library zone!!!!!!!!!!!!!!!!!!!!!!!!!!!! typedef long long ll; typedef long double ld; #define all(a) (a).begin(), (a).end() #define EPS (1e-5) const ll Mod = 1000000007; const ll mod = 998244353; struct H { ll x, y; bool operator<(const H &b) const { if (x != b.x) return x < b.x; return y < b.y; } bool operator>(const H &b) const { if (x != b.x) return x > b.x; return y > b.y; } bool operator==(const H &b) const { return x == b.x && y == b.y; } bool operator!=(const H &b) const { return (*this) != b; } }; struct P { ll pos, cost; bool operator<(const P &b) const { return cost < b.cost; } bool operator>(const P &b) const { return cost > b.cost; } }; struct B { ll to, cost; }; struct E { ll from, to, cost; bool operator<(const E &b) const { return cost < b.cost; } bool operator>(const E &b) const { return cost > b.cost; } }; template <typename T, typename U> void chmin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> void chmax(T &a, U b) { if (a < b) a = b; } template <typename T> T max_0(T a) { if (a < 0) return 0; return a; } template <typename T> T min_0(T a) { if (a > 0) return 0; return a; } ll read() { ll u; ll k = scanf("%lld", &u); return u; } ll gcd(ll i, ll j) { if (i > j) swap(i, j); if (i == 0) return j; return gcd(j % i, i); } ll mod_pow(ll x, ll n, ll p) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % p; x = x * x % p; n >>= 1; } return res; } // x^n%p vector<string> split(string s, char c = ' ') { vector<string> vec; string h = ""; for (int i = 0; i < s.size();) { int j = i; while (j < s.size() && s[j] != ' ') { h += s[j++]; } if (h.size() > 0) vec.push_back(h); h = ""; i = j + 1; } return vec; } template <typename T> void print_vec(vector<T> a) { for (int i = 0; i < a.size(); i++) { cout << (i == 0 ? "" : " ") << a[i]; } cout << endl; } const ll Inf = 3023372036854775807; const int inf = 1500000000; #define int long long //---------------------------------------------------- int n; int a[500]; int b[500]; int dp[500][500]; // iからjまで連結させるときのコスト signed main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (i > 0) b[i] += b[i - 1]; b[i] += a[i]; } for (int k = 1; k < n; k++) { for (int i = 0; i < n; i++) { // iからi+kまで int j = i + k; int sum = Inf; for (int z = i; z < j; z++) { sum = min(sum, dp[i][z] + dp[z + 1][j]); } sum += b[j] - (i == 0 ? 0 : b[i - 1]); dp[i][j] = sum; } } cout << dp[0][n - 1] << endl; }
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; ///////////////////library zone!!!!!!!!!!!!!!!!!!!!!!!!!!!! typedef long long ll; typedef long double ld; #define all(a) (a).begin(), (a).end() #define EPS (1e-5) const ll Mod = 1000000007; const ll mod = 998244353; struct H { ll x, y; bool operator<(const H &b) const { if (x != b.x) return x < b.x; return y < b.y; } bool operator>(const H &b) const { if (x != b.x) return x > b.x; return y > b.y; } bool operator==(const H &b) const { return x == b.x && y == b.y; } bool operator!=(const H &b) const { return (*this) != b; } }; struct P { ll pos, cost; bool operator<(const P &b) const { return cost < b.cost; } bool operator>(const P &b) const { return cost > b.cost; } }; struct B { ll to, cost; }; struct E { ll from, to, cost; bool operator<(const E &b) const { return cost < b.cost; } bool operator>(const E &b) const { return cost > b.cost; } }; template <typename T, typename U> void chmin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> void chmax(T &a, U b) { if (a < b) a = b; } template <typename T> T max_0(T a) { if (a < 0) return 0; return a; } template <typename T> T min_0(T a) { if (a > 0) return 0; return a; } ll read() { ll u; ll k = scanf("%lld", &u); return u; } ll gcd(ll i, ll j) { if (i > j) swap(i, j); if (i == 0) return j; return gcd(j % i, i); } ll mod_pow(ll x, ll n, ll p) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % p; x = x * x % p; n >>= 1; } return res; } // x^n%p vector<string> split(string s, char c = ' ') { vector<string> vec; string h = ""; for (int i = 0; i < s.size();) { int j = i; while (j < s.size() && s[j] != ' ') { h += s[j++]; } if (h.size() > 0) vec.push_back(h); h = ""; i = j + 1; } return vec; } template <typename T> void print_vec(vector<T> a) { for (int i = 0; i < a.size(); i++) { cout << (i == 0 ? "" : " ") << a[i]; } cout << endl; } const ll Inf = 3023372036854775807; const int inf = 1500000000; #define int long long //---------------------------------------------------- int n; int a[500]; int b[500]; int dp[500][500]; // iからjまで連結させるときのコスト signed main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (i > 0) b[i] += b[i - 1]; b[i] += a[i]; } for (int k = 1; k < n; k++) { for (int i = 0; i < n; i++) { // iからi+kまで int j = i + k; if (j >= n) break; int sum = Inf; for (int z = i; z < j; z++) { sum = min(sum, dp[i][z] + dp[z + 1][j]); } sum += b[j] - (i == 0 ? 0 : b[i - 1]); dp[i][j] = sum; } } cout << dp[0][n - 1] << endl; }
insert
137
137
137
139
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define ld long double #define fi first #define se second #define pb push_back #define mp make_pair ll mod = 1e9 + 7; ll a[200005]; ll b[200005]; ll idx[100005][2]; // ll cnt[200005]; ll pre[200005]; // ll end[200005]; map<ll, ll> m; bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.fi - a.se) > (b.fi - b.se); } bool revcomp(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.fi) > (b.fi); } ll ceil11(ll a, ll b) { if (a % b != 0) return a / b + 1; else return a / b; } ll power(ll a, ll b, ll n) { ll ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % n; a = (a * a) % n; b /= 2; } return ans; } ll count1 = 0; ll gcd(ll a, ll b) { // if(a<b)swap(a,b); if (b == 0) return a; return gcd(b, a % b); } ll primepos(ll fact, ll p) { ll res = 0; while (fact > 0) { res += fact / p; fact /= p; } return res; } // void dfs(ll n, ll p){ // if(vis[n]!=0){ // return; // } // else{ // vis[n]=1; // b[n]=p; // for(int i=0;i<v[n].size();i++){ // dfs(v[n][i],p); // } // } ll ceil1(ll a, ll b) { return a / b; } int main() { ios::sync_with_stdio(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif ll n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long int dp[n][n], temp; for (int i = 0; i < n - 1; i++) { dp[i][i + 1] = a[i] + a[i + 1]; dp[i][i] = 0; } dp[n - 1][n - 1] = 0; long long int sm[n][n]; for (int i = 0; i < n; i++) { sm[i][i] = a[i]; for (int j = i + 1; j < n; j++) { sm[i][j] = sm[i][j - 1] + a[j]; } } for (int i = 2; i < n; i++) { for (int j = 0; j + i < n; j++) { temp = LONG_MAX; for (int k = 0; k < i; k++) { temp = min(temp, dp[j][j + k] + dp[j + k + 1][j + i]); } dp[j][j + i] = temp + sm[j][j + i]; } } cout << dp[0][n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define ld long double #define fi first #define se second #define pb push_back #define mp make_pair ll mod = 1e9 + 7; ll a[200005]; ll b[200005]; ll idx[100005][2]; // ll cnt[200005]; ll pre[200005]; // ll end[200005]; map<ll, ll> m; bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.fi - a.se) > (b.fi - b.se); } bool revcomp(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.fi) > (b.fi); } ll ceil11(ll a, ll b) { if (a % b != 0) return a / b + 1; else return a / b; } ll power(ll a, ll b, ll n) { ll ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % n; a = (a * a) % n; b /= 2; } return ans; } ll count1 = 0; ll gcd(ll a, ll b) { // if(a<b)swap(a,b); if (b == 0) return a; return gcd(b, a % b); } ll primepos(ll fact, ll p) { ll res = 0; while (fact > 0) { res += fact / p; fact /= p; } return res; } // void dfs(ll n, ll p){ // if(vis[n]!=0){ // return; // } // else{ // vis[n]=1; // b[n]=p; // for(int i=0;i<v[n].size();i++){ // dfs(v[n][i],p); // } // } ll ceil1(ll a, ll b) { return a / b; } int main() { ios::sync_with_stdio(0); ll n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long int dp[n][n], temp; for (int i = 0; i < n - 1; i++) { dp[i][i + 1] = a[i] + a[i + 1]; dp[i][i] = 0; } dp[n - 1][n - 1] = 0; long long int sm[n][n]; for (int i = 0; i < n; i++) { sm[i][i] = a[i]; for (int j = i + 1; j < n; j++) { sm[i][j] = sm[i][j - 1] + a[j]; } } for (int i = 2; i < n; i++) { for (int j = 0; j + i < n; j++) { temp = LONG_MAX; for (int k = 0; k < i; k++) { temp = min(temp, dp[j][j + k] + dp[j + k + 1][j + i]); } dp[j][j + i] = temp + sm[j][j + i]; } } cout << dp[0][n - 1]; return 0; }
delete
80
83
80
80
TLE
p03173
C++
Runtime Error
/************ * @Just Another Source code by: ankit.sangwan1999 * @created on: 24 Jun 2020 * @Based On : MCM */ #include <bits/stdc++.h> using namespace std; #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) //;cout.tie(NULL) #define ll long long const int mod = 1e9 + 7; const long double pie = 3.14159265358979323846; #define endl '\n' #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; gp_hash_table<int, ll> ma; // orderd_map using pb_ds const int N = 405; ll dp[N][N]; ll sum[N]; void pre_sum(int *arr, int n) { sum[0] = arr[0]; for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + arr[i]; } // Cost of interval l to r is -> minimum among the partitions from (k=i to k=j) // + sum of terms i to j dp[i][j] -> stores the minimum cost of the interval i // to j. // O(n^3) time and space ll MCM(int i, int j, int *arr) { if (dp[i][j] != -1) return dp[i][j]; if (i == j) { return dp[i][j] = 0; } ll ans = ULLONG_MAX / 2; ll curr_cost = (i == 0 ? sum[j] : sum[j] - sum[i - 1]); // sum till i to j.(cost of joining the // left and right intervals) for (int k = i; k < j; k++) { ll left_cost = MCM(i, k, arr); ll right_cost = MCM(k + 1, j, arr); ll tmp = left_cost + right_cost + curr_cost; ans = min(ans, tmp); } return dp[i][j] = ans; } void solve() { memset(dp, -1, sizeof(dp)); int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } pre_sum(arr, n); // to calculate the prefix sums. // TOP DOWN // cout<<MCM(0,n-1, arr); // Bottom-Up: for (int len = 1; len <= n; len++) { // length of interval -> moving from // smaller to larger intervals. for (int i = 0; i < n; i++) { // fixing the starting point. int j = i + len - 1; // considering interval i to j if (i == j) { dp[i][j] = 0; } else { ll tmp = ULLONG_MAX / 2; ll curr_cost = (i == 0 ? sum[j] : sum[j] - sum[i - 1]); // sum till i to j.(cost of joining // the left and right intervals) for (int k = i; k < j; k++) { tmp = min(tmp, dp[i][k] + dp[k + 1][j] + curr_cost); } dp[i][j] = tmp; } } } cout << dp[0][n - 1] << endl; } signed main() { fastio; int t = 1; // cin>>t; while (--t >= 0) { solve(); } return 0; }
/************ * @Just Another Source code by: ankit.sangwan1999 * @created on: 24 Jun 2020 * @Based On : MCM */ #include <bits/stdc++.h> using namespace std; #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) //;cout.tie(NULL) #define ll long long const int mod = 1e9 + 7; const long double pie = 3.14159265358979323846; #define endl '\n' #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; gp_hash_table<int, ll> ma; // orderd_map using pb_ds const int N = 405; ll dp[N][N]; ll sum[N]; void pre_sum(int *arr, int n) { sum[0] = arr[0]; for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + arr[i]; } // Cost of interval l to r is -> minimum among the partitions from (k=i to k=j) // + sum of terms i to j dp[i][j] -> stores the minimum cost of the interval i // to j. // O(n^3) time and space ll MCM(int i, int j, int *arr) { if (dp[i][j] != -1) return dp[i][j]; if (i == j) { return dp[i][j] = 0; } ll ans = ULLONG_MAX / 2; ll curr_cost = (i == 0 ? sum[j] : sum[j] - sum[i - 1]); // sum till i to j.(cost of joining the // left and right intervals) for (int k = i; k < j; k++) { ll left_cost = MCM(i, k, arr); ll right_cost = MCM(k + 1, j, arr); ll tmp = left_cost + right_cost + curr_cost; ans = min(ans, tmp); } return dp[i][j] = ans; } void solve() { memset(dp, -1, sizeof(dp)); int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } pre_sum(arr, n); // to calculate the prefix sums. // TOP DOWN // cout<<MCM(0,n-1, arr); // Bottom-Up: for (int len = 1; len <= n; len++) { // length of interval -> moving from // smaller to larger intervals. for (int i = 0; i <= n - len; i++) { // fixing the starting point. int j = i + len - 1; // considering interval i to j if (i == j) { dp[i][j] = 0; } else { ll tmp = ULLONG_MAX / 2; ll curr_cost = (i == 0 ? sum[j] : sum[j] - sum[i - 1]); // sum till i to j.(cost of joining // the left and right intervals) for (int k = i; k < j; k++) { tmp = min(tmp, dp[i][k] + dp[k + 1][j] + curr_cost); } dp[i][j] = tmp; } } } cout << dp[0][n - 1] << endl; } signed main() { fastio; int t = 1; // cin>>t; while (--t >= 0) { solve(); } return 0; }
replace
70
73
70
73
0
p03173
C++
Runtime Error
#include <stdio.h> typedef long long ll; using namespace std; int main(void) { ll i, j, k, n; scanf("%lld", &n); ll a[n], dp[n][n], sum[n]; for (i = 0; i < n; ++i) { scanf("%lld", &a[i]); if (i) sum[i] = sum[i - 1] + a[i]; else sum[i] = a[i]; } for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) dp[i][j] = 1e18; for (i = 0; i < n; ++i) dp[i][0] = 0; for (i = 1; i < n; ++i) for (j = 0; j < n; ++j) { for (k = 0; k < i; ++k) if (dp[j][i] > dp[j][k] + dp[j + k + 1][i - k - 1] + sum[j + i] - (j ? sum[j - 1] : 0)) { dp[j][i] = dp[j][k] + dp[j + k + 1][i - k - 1] + sum[j + i] - (j ? sum[j - 1] : 0); } } printf("%lld", dp[0][n - 1]); return 0; }
#include <stdio.h> typedef long long ll; using namespace std; int main(void) { ll i, j, k, n; scanf("%lld", &n); ll a[n], dp[n][n], sum[n]; for (i = 0; i < n; ++i) { scanf("%lld", &a[i]); if (i) sum[i] = sum[i - 1] + a[i]; else sum[i] = a[i]; } for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) dp[i][j] = 1e18; for (i = 0; i < n; ++i) dp[i][0] = 0; for (i = 1; i < n; ++i) for (j = 0; j < n - i; ++j) { for (k = 0; k < i; ++k) if (dp[j][i] > dp[j][k] + dp[j + k + 1][i - k - 1] + sum[j + i] - (j ? sum[j - 1] : 0)) { dp[j][i] = dp[j][k] + dp[j + k + 1][i - k - 1] + sum[j + i] - (j ? sum[j - 1] : 0); } } printf("%lld", dp[0][n - 1]); return 0; }
replace
21
22
21
22
0
p03173
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; int n; long long memo[405][405]; long long pre[405]; long long dp(int i, int j) { if (i == j) return 0; else if (memo[i][j] != -1) return memo[i][j]; long long ans = 1000000000000000000; for (int x = i; x < j; x++) { ans = min(ans, dp(i, x) + dp(x + 1, j) + pre[j] - pre[i - 1]); } return ans; } int main() { // freopen("input.txt","r",stdin); memset(memo, -1, sizeof(memo)); scanf("%d", &n); for (int x = 1; x <= n; x++) { scanf("%lld", &pre[x]); pre[x] += pre[x - 1]; } printf("%lld\n", dp(1, n)); }
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; int n; long long memo[405][405]; long long pre[405]; long long dp(int i, int j) { if (i == j) return 0; else if (memo[i][j] != -1) return memo[i][j]; long long ans = 1000000000000000000; for (int x = i; x < j; x++) { ans = min(ans, dp(i, x) + dp(x + 1, j) + pre[j] - pre[i - 1]); } return memo[i][j] = ans; } int main() { // freopen("input.txt","r",stdin); memset(memo, -1, sizeof(memo)); scanf("%d", &n); for (int x = 1; x <= n; x++) { scanf("%lld", &pre[x]); pre[x] += pre[x - 1]; } printf("%lld\n", dp(1, n)); }
replace
16
17
16
17
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define rrep1(i, n) for (int i = (n); i > 0; i--) #define ll long long #define pi pair<int, int> #define pll pair<ll, ll> #define MOD 1000000007 #define INF 1000000000000000LL using namespace std; #define MAXN 400 ll dp[MAXN + 1][MAXN + 1]; bool used[MAXN + 1][MAXN + 1]; vector<ll> a(MAXN + 1), suma(MAXN + 2); ll f(int l, int r) { if (used[l][r]) return dp[l][r]; used[l][r] = true; if (l == r) return 0; ll sum = suma[r] - suma[l - 1]; ll fans = INF; for (int m = l; m <= r; m++) { fans = min<ll>(fans, f(l, m) + f(m + 1, r)); } // cout<<l<<' '<<r<<' '<<sum<<' '<<fans<<endl; return dp[l][r] = fans + sum; } int main() { int n; cin >> n; rep(i, n) cin >> a[i]; ll sum = 0; rep(i, n) sum += a[i]; rep(i, n) suma[i + 1] += suma[i] + a[i]; cout << f(1, n) << endl; return 0; rep(i, n + 1) { rep(j, n + 1) cout << dp[i][j] << ' '; cout << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define rrep1(i, n) for (int i = (n); i > 0; i--) #define ll long long #define pi pair<int, int> #define pll pair<ll, ll> #define MOD 1000000007 #define INF 1000000000000000LL using namespace std; #define MAXN 400 ll dp[MAXN + 2][MAXN + 2]; bool used[MAXN + 2][MAXN + 2]; vector<ll> a(MAXN + 1), suma(MAXN + 2); ll f(int l, int r) { if (used[l][r]) return dp[l][r]; used[l][r] = true; if (l == r) return 0; ll sum = suma[r] - suma[l - 1]; ll fans = INF; for (int m = l; m <= r; m++) { fans = min<ll>(fans, f(l, m) + f(m + 1, r)); } // cout<<l<<' '<<r<<' '<<sum<<' '<<fans<<endl; return dp[l][r] = fans + sum; } int main() { int n; cin >> n; rep(i, n) cin >> a[i]; ll sum = 0; rep(i, n) sum += a[i]; rep(i, n) suma[i + 1] += suma[i] + a[i]; cout << f(1, n) << endl; return 0; rep(i, n + 1) { rep(j, n + 1) cout << dp[i][j] << ' '; cout << endl; } return 0; }
replace
18
20
18
20
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define rrep1(i, n) for (int i = (n); i > 0; i--) #define ll long long #define pi pair<int, int> #define pll pair<ll, ll> #define MOD 1000000007 #define INF 1000000000000000LL using namespace std; #define MAXN 400 ll dp[MAXN + 1][MAXN + 1]; bool used[MAXN + 1][MAXN + 1]; vector<ll> a(MAXN + 1), suma(MAXN + 1); ll f(int l, int r) { if (used[l][r]) return dp[l][r]; used[l][r] = true; if (l == r) return 0; ll sum = suma[r] - suma[l - 1]; ll fans = INF; for (int m = l; m <= r; m++) { fans = min<ll>(fans, f(l, m) + f(m + 1, r)); } // cout<<l<<' '<<r<<' '<<sum<<' '<<fans<<endl; return dp[l][r] = fans + sum; } int main() { int n; cin >> n; rep(i, n) cin >> a[i]; ll sum = 0; rep(i, n) sum += a[i]; rep(i, n) suma[i + 1] += suma[i] + a[i]; cout << f(1, n) << endl; return 0; rep(i, n + 1) { rep(j, n + 1) cout << dp[i][j] << ' '; cout << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define rrep1(i, n) for (int i = (n); i > 0; i--) #define ll long long #define pi pair<int, int> #define pll pair<ll, ll> #define MOD 1000000007 #define INF 1000000000000000LL using namespace std; #define MAXN 400 ll dp[MAXN + 10][MAXN + 10]; bool used[MAXN + 10][MAXN + 10]; vector<ll> a(MAXN + 10), suma(MAXN + 10); ll f(int l, int r) { if (used[l][r]) return dp[l][r]; used[l][r] = true; if (l == r) return 0; ll sum = suma[r] - suma[l - 1]; ll fans = INF; for (int m = l; m <= r; m++) { fans = min<ll>(fans, f(l, m) + f(m + 1, r)); } // cout<<l<<' '<<r<<' '<<sum<<' '<<fans<<endl; return dp[l][r] = fans + sum; } int main() { int n; cin >> n; rep(i, n) cin >> a[i]; ll sum = 0; rep(i, n) sum += a[i]; rep(i, n) suma[i + 1] += suma[i] + a[i]; cout << f(1, n) << endl; return 0; rep(i, n + 1) { rep(j, n + 1) cout << dp[i][j] << ' '; cout << endl; } return 0; }
replace
18
21
18
21
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define el '\n' #define sz(v) ((int)((v).size())) #define all(v) ((v).begin()), ((v).end()) #define clr(v, d) memset(v, d, sizeof(v)) double const EPS = 1e-8, PI = acos(-1); const int N = 405 + 9, M = 100 + 9, OO = (int)1e9 + 1; const long long MOD = 1e9 + 7, INF = 1e18 + 9; typedef long long ll; void OUTPUT() { cout << fixed << setprecision(12); ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); } void INPUT() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif } int n, arr[N]; ll dp[N][N], cum[N]; int main() { INPUT(); OUTPUT(); cin >> n; for (int i = 0; i < n; ++i) { cin >> arr[i]; cum[i] = arr[i] + cum[i - 1] * (i != 0); } for (int L = n - 1; L >= 0; L--) { for (int R = L; R < n; R++) { if (L == R) dp[L][R] = 0; else { dp[L][R] = INF; ll s = cum[R]; if (L != 0) s -= cum[L - 1]; for (int i = L; i < R; i++) { dp[L][R] = min(dp[L][R], dp[L][i] + dp[i + 1][R] + s); } } } } cout << dp[0][n - 1]; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define el '\n' #define sz(v) ((int)((v).size())) #define all(v) ((v).begin()), ((v).end()) #define clr(v, d) memset(v, d, sizeof(v)) double const EPS = 1e-8, PI = acos(-1); const int N = 405 + 9, M = 100 + 9, OO = (int)1e9 + 1; const long long MOD = 1e9 + 7, INF = 1e18 + 9; typedef long long ll; void OUTPUT() { cout << fixed << setprecision(12); ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); } void INPUT() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif } int n, arr[N]; ll dp[N][N], cum[N]; int main() { // INPUT(); OUTPUT(); cin >> n; for (int i = 0; i < n; ++i) { cin >> arr[i]; cum[i] = arr[i] + cum[i - 1] * (i != 0); } for (int L = n - 1; L >= 0; L--) { for (int R = L; R < n; R++) { if (L == R) dp[L][R] = 0; else { dp[L][R] = INF; ll s = cum[R]; if (L != 0) s -= cum[L - 1]; for (int i = L; i < R; i++) { dp[L][R] = min(dp[L][R], dp[L][i] + dp[i + 1][R] + s); } } } } cout << dp[0][n - 1]; return 0; }
replace
37
38
37
38
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define maxn 450 using namespace std; typedef long long LL; LL a[maxn]; LL f[maxn][maxn]; int main() { ios::sync_with_stdio(false); int N; cin >> N; for (int i = 1; i <= N; i += 1) cin >> a[i]; for (int L = 2; L <= N; L += 1) for (int i = 1; i <= N; i += 1) { f[i][L] = 1E18; for (int j = 1; j < L; j += 1) f[i][L] = min(f[i][L], f[i][j] + f[i + j][L - j]); for (int j = i; j < i + L; j += 1) f[i][L] += a[j]; } cout << f[1][N]; }
#include <bits/stdc++.h> #define maxn 450 using namespace std; typedef long long LL; LL a[maxn]; LL f[maxn][maxn]; int main() { ios::sync_with_stdio(false); int N; cin >> N; for (int i = 1; i <= N; i += 1) cin >> a[i]; for (int L = 2; L <= N; L += 1) for (int i = 1; i + L <= N + 1; i += 1) { f[i][L] = 1E18; for (int j = 1; j < L; j += 1) f[i][L] = min(f[i][L], f[i][j] + f[i + j][L - j]); for (int j = i; j < i + L; j += 1) f[i][L] += a[j]; } cout << f[1][N]; }
replace
13
14
13
14
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define mod 1000000007 #define sp ' ' #define intmax 2147483647 #define llmax 9223372036854775807 #define nyan "(=^・ω・^=)" #define mkp make_pair #define lP pair<ll, ll> #define iP pair<int, int> typedef long long ll; using namespace std; int N, a[400]; ll DP[400][400], s[401]; int main() { cin >> N; for (int i = 0; i != N; ++i) cin >> a[i]; for (int i = 0; i != N; ++i) s[i + 1] = s[i] + a[i]; for (int i = 1; i != N; ++i) { for (int j = 0; i + j != N; ++j) { DP[j][i + j] = llmax; for (int k = j; k != i + j + 1; ++k) { DP[j][i + j] = min(DP[j][i + j], DP[j][k] + DP[k + 1][i + j] + s[i + j + 1] - s[j]); } } } cout << DP[0][N - 1] << endl; return 0; }
#include <bits/stdc++.h> #define mod 1000000007 #define sp ' ' #define intmax 2147483647 #define llmax 9223372036854775807 #define nyan "(=^・ω・^=)" #define mkp make_pair #define lP pair<ll, ll> #define iP pair<int, int> typedef long long ll; using namespace std; int N, a[400]; ll DP[400][400], s[401]; int main() { cin >> N; for (int i = 0; i != N; ++i) cin >> a[i]; for (int i = 0; i != N; ++i) s[i + 1] = s[i] + a[i]; for (int i = 1; i != N; ++i) { for (int j = 0; i + j != N; ++j) { DP[j][i + j] = llmax; for (int k = j; k != i + j; ++k) { DP[j][i + j] = min(DP[j][i + j], DP[j][k] + DP[k + 1][i + j] + s[i + j + 1] - s[j]); } } } cout << DP[0][N - 1] << endl; return 0; }
replace
23
24
23
24
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define FORR(i, m, n) for (ll i = m; i >= n; --i) #define ALL(v) (v).begin(), (v).end() template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const ll INF = 1LL << 60; const int inf = (1 << 30) - 1; const int mod = 1e9 + 7; int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1}; int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1}; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<ll> a(n), sm(n + 1); REP(i, n) { cin >> a[i]; sm[i + 1] += sm[i] + a[i]; } vector<vector<ll>> dp(n + 1, vector<ll>(n + 1)); REP(i, n + 1) { dp[i][i + 2] = sm[i + 2] - sm[i]; } FOR(i, 2, n + 1) { for (int j = 0; j + i <= n; j++) { ll tmp = INF; for (int k = j + 1; k < j + i; k++) { chmin(tmp, dp[j][k] + dp[k][j + i] + sm[j + i] - sm[j]); } dp[j][j + i] = tmp; } } cout << dp[0][n] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define FORR(i, m, n) for (ll i = m; i >= n; --i) #define ALL(v) (v).begin(), (v).end() template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const ll INF = 1LL << 60; const int inf = (1 << 30) - 1; const int mod = 1e9 + 7; int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1}; int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1}; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<ll> a(n), sm(n + 1); REP(i, n) { cin >> a[i]; sm[i + 1] += sm[i] + a[i]; } vector<vector<ll>> dp(n + 1, vector<ll>(n + 1)); REP(i, n - 1) { dp[i][i + 2] = sm[i + 2] - sm[i]; } FOR(i, 2, n + 1) { for (int j = 0; j + i <= n; j++) { ll tmp = INF; for (int k = j + 1; k < j + i; k++) { chmin(tmp, dp[j][k] + dp[k][j + i] + sm[j + i] - sm[j]); } dp[j][j + i] = tmp; } } cout << dp[0][n] << endl; }
replace
39
40
39
40
-11
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define pii pair<ll, ll> #define NIL -(long long)(1e9) #define ll long long #define satir cout << "\n" #define ALL(v) (v).begin(), (v).end() #define f first #define s second #define sps << " " << #define sp << " " #define spl cout << "\n"; #define INF 0x3f3f3f3f3f3f3f3f #define cntout \ cout << " " << rndcntbo; \ ++rndcntbo; const ll N = 1 * 1000 + 7; ll n, n1, n2, n3, k, t, d1, d2, m, ans = -1, rndcntbo = 190; ll arr[N]; ll dp[N][N]; ll pref[N]; void max_self(int &a, int b) { a = min(a, b); return; } ll dpa(int l, int r) { // cout<<l sps r<<" aadsd ";spl; if (l == r) return 0; // dp[l][r]=arr[l]; // cout<<"test1";spl; // if(abs(l-r)==1) return (dp[l][r]=arr[l]+arr[r]); if (dp[l][r] != -1) return dp[l][r]; // cout<<"test2";spl; ll nax = INF; for (int i = l; i < r; ++i) { // cout<<dpa(l,i)+dpa(i+1,r);spl; // cout<<"myl-myr"<<l sps r <<"calling"<<l sps i;spl; nax = min(nax, dpa(l, i) + dpa(i + 1, r) + pref[i] - pref[l - 1] + pref[r] - pref[i]); } dp[l][r] = nax; // cout<<nax;spl; return nax; } inline void coz() { cin >> n; pref[0] = 0; for (ll i = 1; i <= n; ++i) { cin >> arr[i]; // dp[i][i]=arr[i]; pref[i] = pref[i - 1] + arr[i]; } // cout<<"asd"<<n;spl; cout << dpa(1, n); // cout<<INF; // cout<<"bittiaq";spl; // cout<<dp[1][n]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); memset(dp, -1, sizeof(dp)); #ifndef ONLINE_JUDGE freopen("factory.in", "r", stdin); freopen("factory.out", "w", stdout); #endif // cin>>t; // while(t--) coz(); // cout<<ans<<" "; return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define pii pair<ll, ll> #define NIL -(long long)(1e9) #define ll long long #define satir cout << "\n" #define ALL(v) (v).begin(), (v).end() #define f first #define s second #define sps << " " << #define sp << " " #define spl cout << "\n"; #define INF 0x3f3f3f3f3f3f3f3f #define cntout \ cout << " " << rndcntbo; \ ++rndcntbo; const ll N = 1 * 1000 + 7; ll n, n1, n2, n3, k, t, d1, d2, m, ans = -1, rndcntbo = 190; ll arr[N]; ll dp[N][N]; ll pref[N]; void max_self(int &a, int b) { a = min(a, b); return; } ll dpa(int l, int r) { // cout<<l sps r<<" aadsd ";spl; if (l == r) return 0; // dp[l][r]=arr[l]; // cout<<"test1";spl; // if(abs(l-r)==1) return (dp[l][r]=arr[l]+arr[r]); if (dp[l][r] != -1) return dp[l][r]; // cout<<"test2";spl; ll nax = INF; for (int i = l; i < r; ++i) { // cout<<dpa(l,i)+dpa(i+1,r);spl; // cout<<"myl-myr"<<l sps r <<"calling"<<l sps i;spl; nax = min(nax, dpa(l, i) + dpa(i + 1, r) + pref[i] - pref[l - 1] + pref[r] - pref[i]); } dp[l][r] = nax; // cout<<nax;spl; return nax; } inline void coz() { cin >> n; pref[0] = 0; for (ll i = 1; i <= n; ++i) { cin >> arr[i]; // dp[i][i]=arr[i]; pref[i] = pref[i - 1] + arr[i]; } // cout<<"asd"<<n;spl; cout << dpa(1, n); // cout<<INF; // cout<<"bittiaq";spl; // cout<<dp[1][n]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); memset(dp, -1, sizeof(dp)); // #ifndef ONLINE_JUDGE // freopen("factory.in","r",stdin); freopen("factory.out","w",stdout); // #endif // cin>>t; // while(t--) coz(); // cout<<ans<<" "; return 0; }
replace
70
74
70
73
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long int bs_upper_bound(int a[], int n, int x) { int l = 0; int h = n; // Not n - 1 while (l < h) { int mid = l + (h - l) / 2; if (x >= a[mid]) { l = mid + 1; } else { h = mid; } } return l; } int bs_lower_bound(int a[], int n, int x) { int l = 0; int h = n; // Not n - 1 while (l < h) { int mid = l + (h - l) / 2; if (x <= a[mid]) { h = mid; } else { l = mid + 1; } } return l; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } int main() { IOS; int n; cin >> n; ll a[n]; for (int i = 0; i < n; i++) cin >> a[i]; ll dp[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) dp[i][j] = LONG_MAX; } for (int i = 0; i < n; i++) dp[i][i] = 0; for (int k = 2; k <= n; k++) { for (int i = 0; i < n; i++) { ll s = a[i]; for (int j = i + 1; j < i + k; j++) { s += a[j]; for (int l = i; l < j; l++) dp[i][j] = min(dp[i][j], s + dp[i][l] + dp[l + 1][j]); } } } cout << dp[0][n - 1]; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long int bs_upper_bound(int a[], int n, int x) { int l = 0; int h = n; // Not n - 1 while (l < h) { int mid = l + (h - l) / 2; if (x >= a[mid]) { l = mid + 1; } else { h = mid; } } return l; } int bs_lower_bound(int a[], int n, int x) { int l = 0; int h = n; // Not n - 1 while (l < h) { int mid = l + (h - l) / 2; if (x <= a[mid]) { h = mid; } else { l = mid + 1; } } return l; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } int main() { IOS; int n; cin >> n; ll a[n]; for (int i = 0; i < n; i++) cin >> a[i]; ll dp[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) dp[i][j] = LONG_MAX; } for (int i = 0; i < n; i++) dp[i][i] = 0; for (int k = 2; k <= n; k++) { for (int i = 0; i < n - k + 1; i++) { ll s = a[i]; for (int j = i + 1; j < i + k; j++) { s += a[j]; for (int l = i; l < j; l++) dp[i][j] = min(dp[i][j], s + dp[i][l] + dp[l + 1][j]); } } } cout << dp[0][n - 1]; }
replace
56
57
56
57
0
p03173
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair<int, int> P; #define stop \ char nyaa; \ cin >> nyaa; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef long double ld; typedef complex<ld> Point; const ld eps = 1e-3; const ld pi = acos(-1.0); typedef pair<ll, ll> LP; typedef pair<ld, ld> LDP; int n; ll a[400]; ll s[401]; bool chked[400][400]; ll ans[400][400]; ll dfs(int le, int ri) { if (le == ri) return 0; if (chked[le][ri]) return ans[le][ri]; chked[le][ri] - true; ll res = INF; Rep(i, le, ri) { ll sum = dfs(le, i) + dfs(i + 1, ri); res = min(res, sum); } res += s[ri + 1] - s[le]; return ans[le][ri] = res; } int main() { cin >> n; rep(i, n) cin >> a[i]; s[0] = 0; rep(i, n) s[i + 1] = s[i] + a[i]; cout << dfs(0, n - 1) << endl; // stop return 0; }
#include <algorithm> #include <bitset> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair<int, int> P; #define stop \ char nyaa; \ cin >> nyaa; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef long double ld; typedef complex<ld> Point; const ld eps = 1e-3; const ld pi = acos(-1.0); typedef pair<ll, ll> LP; typedef pair<ld, ld> LDP; int n; ll a[400]; ll s[401]; bool chked[400][400]; ll ans[400][400]; ll dfs(int le, int ri) { if (le == ri) return 0; if (chked[le][ri]) return ans[le][ri]; chked[le][ri] = true; ll res = INF; Rep(i, le, ri) { ll sum = dfs(le, i) + dfs(i + 1, ri); res = min(res, sum); } res += s[ri + 1] - s[le]; return ans[le][ri] = res; } int main() { cin >> n; rep(i, n) cin >> a[i]; s[0] = 0; rep(i, n) s[i + 1] = s[i] + a[i]; cout << dfs(0, n - 1) << endl; // stop return 0; }
replace
48
49
48
49
TLE
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> #define st first #define nd second #define turbo \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define vi vector<int> #define vvi vector<vi> #define qi queue<int> #define ld long double using namespace std; /*---------------------------------------------------------///CODE///---------------------------------------------------------*/ ll dp[402][402]; ll T[402]; ll pref[402]; ll count_dp(int a, int b) { if (dp[a][b] != 1e18) return dp[a][b]; if (a == b) return 0; for (int i = a; i <= b; i++) for (int j = i; j <= b; j++) dp[a][b] = min(dp[a][b], count_dp(a, j) + count_dp(j + 1, b) + pref[b] - pref[j] + pref[j] - pref[a - 1]); return dp[a][b]; } int main() { turbo int n; cin >> n; for (int i = 1; i <= n; i++) cin >> T[i]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dp[i][j] = 1e18; for (int j = 1; j <= n; j++) pref[j] = pref[j - 1] + T[j]; return cout << count_dp(1, n), 0; }
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> #define st first #define nd second #define turbo \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define vi vector<int> #define vvi vector<vi> #define qi queue<int> #define ld long double using namespace std; /*---------------------------------------------------------///CODE///---------------------------------------------------------*/ ll dp[402][402]; ll T[402]; ll pref[402]; ll count_dp(int a, int b) { if (dp[a][b] != 1e18) return dp[a][b]; if (a == b) return 0; for (int i = a; i <= b; i++) dp[a][b] = min(dp[a][b], count_dp(a, i) + count_dp(i + 1, b) + pref[b] - pref[i] + pref[i] - pref[a - 1]); return dp[a][b]; } int main() { turbo int n; cin >> n; for (int i = 1; i <= n; i++) cin >> T[i]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dp[i][j] = 1e18; for (int j = 1; j <= n; j++) pref[j] = pref[j - 1] + T[j]; return cout << count_dp(1, n), 0; }
replace
30
33
30
32
TLE
p03173
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iostream> #include <map> #include <set> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < n; i++) template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } ll n; ll s[410] = {}; ll dp[410][410]; ll rec(ll l, ll r) { if (l == r) return 0; if (dp[l][r] != -1) return dp[l][r]; ll res = 1LL << 60; for (ll i = l + 1; i <= r; i++) { chmin(res, rec(l, i - 1) + rec(i, r) + s[r] - s[l - 1]); } return res; } int main() { cin >> n; rep(i, n) { ll tmp; cin >> tmp; s[i + 1] = s[i] + tmp; } memset(dp, -1, sizeof(dp)); cout << rec(1, n) << endl; }
#include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iostream> #include <map> #include <set> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < n; i++) template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } ll n; ll s[410] = {}; ll dp[410][410]; ll rec(ll l, ll r) { if (l == r) return 0; if (dp[l][r] != -1) return dp[l][r]; ll res = 1LL << 60; for (ll i = l + 1; i <= r; i++) { chmin(res, rec(l, i - 1) + rec(i, r) + s[r] - s[l - 1]); } return dp[l][r] = res; } int main() { cin >> n; rep(i, n) { ll tmp; cin >> tmp; s[i + 1] = s[i] + tmp; } memset(dp, -1, sizeof(dp)); cout << rec(1, n) << endl; }
replace
35
36
35
36
TLE
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define inf (pow(10, 18)) #define ll long long int #define mod 1000000007 #define ntq(z) \ long long int z; \ cin >> z; \ for (long long int i = 0; i < z; i++) #define fi(x9, y9) for (long long int i = x9; i < y9; i++) #define fj(x9, y9) for (long long int j = x9; j < y9; j++) #define fk(x9, y9) for (long long int k = x9; k < y9; k++) #define f(z9) for (long long int i = 0; i < z9; i++) #define endl "\n" #define sz(a) int((a).size()) #define pb push_back #define all(c) (c).begin(), (c).end() #define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) #define present(c, x) ((c).find(x) != (c).end()) #define cpresent(c, x) (find(all(c), x) != (c).end()) #define stoi(s, n) \ stringstream str(s); \ str >> n; #define get_vi(v, n) \ vi v(n); \ f(n) { cin >> v[i]; } typedef vector<ll> vi; typedef vector<pair<ll, ll>> vp; typedef vector<vi> vvi; typedef pair<ll, ll> ii; void printprecise(double l, ll precision) { std::cout << std::fixed; std::cout << std::setprecision(precision); std::cout << l; } ll ncr(ll n, ll k) { ll C[n + 1][k + 1]; ll i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= min(i, k); j++) { if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } return C[n][k]; } ll power_wm(ll x, ll y, ll p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } ll power(ll x, ll y) { ll res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x); // y must be even now y = y >> 1; // y = y/2 x = (x * x); } return res; } ll root(ll x, vi &roots) { if (roots[x] != x) roots[x] = root(roots[x], roots); return roots[x]; } void unoin(ll p, ll q, vi &roots, vi &rank) { ll rp = root(p, roots); ll rq = root(q, roots); if (rank[rq] < rank[rp]) { roots[rq] = rp; } else if (rank[rp] > rank[rq]) { roots[rp] = rq; } else { roots[rp] = rq; rank[rq]++; } } ll dig_sum(ll n) { ll s = 0; while (n) { s += n % 10; n /= 10; } return s; } ll gcd(ll a, ll b) { if (a < b) { a = a + b; b = a - b; a = a - b; } if (b == 0) return a; return gcd(b, a % b); } ll toD(ll x, ll y, ll m) { return x * m + y % m; } pair<ll, ll> twoD(ll p, ll m) { return (make_pair(p / m, p % m)); } vi parentof, vis, dist; vvi g; void dfs(int p) { vis[p] = 1; f(g[p].size()) { if (vis[g[p][i]] == 0) { parentof[g[p][i]] = p; dist[g[p][i]] = dist[p] + 1; dfs(g[p][i]); } } } vi lps(1000); void create_lps(string p) { ll m = p.length(); fi(1, m) { if (p[i] == p[lps[i - 1]]) { lps[i] = lps[i - 1] + 1; } else { ll l = lps[i - 1]; while (l > 0 && p[i] != p[l]) { l = lps[l - 1]; } if (l == 0 && p[i] == p[0]) { l = 1; } lps[i] = l; } } } ll kmp(string s, string p) { ll l = 0; f(s.length()) { if (s[i] == p[l]) { l++; } else { while (l > 0) { l = lps[l - 1]; if (s[i] == p[l]) { l++; break; } } } if (l == p.length()) { return i - l + 1; } } return -1; } vvi dp(401, vi(401)); vi heights(401); ll calcHeight(ll x, ll y) { ll ans = heights[y]; if (x > 0) { ans -= heights[x - 1]; } return ans; } ll calcCost(ll x, ll y) { if (x == y) { return 0; } if (dp[x][y] != 0) { return dp[x][y]; } ll ans = inf; fi(x, y) { ll xi = calcCost(x, i), iy = calcCost(i + 1, y); ans = min(ans, xi + calcHeight(x, i) + iy + calcHeight(i + 1, y)); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; f(n) { cin >> heights[i]; } fi(1, n) { heights[i] += heights[i - 1]; } ll ans = calcCost(0, n - 1); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define inf (pow(10, 18)) #define ll long long int #define mod 1000000007 #define ntq(z) \ long long int z; \ cin >> z; \ for (long long int i = 0; i < z; i++) #define fi(x9, y9) for (long long int i = x9; i < y9; i++) #define fj(x9, y9) for (long long int j = x9; j < y9; j++) #define fk(x9, y9) for (long long int k = x9; k < y9; k++) #define f(z9) for (long long int i = 0; i < z9; i++) #define endl "\n" #define sz(a) int((a).size()) #define pb push_back #define all(c) (c).begin(), (c).end() #define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) #define present(c, x) ((c).find(x) != (c).end()) #define cpresent(c, x) (find(all(c), x) != (c).end()) #define stoi(s, n) \ stringstream str(s); \ str >> n; #define get_vi(v, n) \ vi v(n); \ f(n) { cin >> v[i]; } typedef vector<ll> vi; typedef vector<pair<ll, ll>> vp; typedef vector<vi> vvi; typedef pair<ll, ll> ii; void printprecise(double l, ll precision) { std::cout << std::fixed; std::cout << std::setprecision(precision); std::cout << l; } ll ncr(ll n, ll k) { ll C[n + 1][k + 1]; ll i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= min(i, k); j++) { if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } return C[n][k]; } ll power_wm(ll x, ll y, ll p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } ll power(ll x, ll y) { ll res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x); // y must be even now y = y >> 1; // y = y/2 x = (x * x); } return res; } ll root(ll x, vi &roots) { if (roots[x] != x) roots[x] = root(roots[x], roots); return roots[x]; } void unoin(ll p, ll q, vi &roots, vi &rank) { ll rp = root(p, roots); ll rq = root(q, roots); if (rank[rq] < rank[rp]) { roots[rq] = rp; } else if (rank[rp] > rank[rq]) { roots[rp] = rq; } else { roots[rp] = rq; rank[rq]++; } } ll dig_sum(ll n) { ll s = 0; while (n) { s += n % 10; n /= 10; } return s; } ll gcd(ll a, ll b) { if (a < b) { a = a + b; b = a - b; a = a - b; } if (b == 0) return a; return gcd(b, a % b); } ll toD(ll x, ll y, ll m) { return x * m + y % m; } pair<ll, ll> twoD(ll p, ll m) { return (make_pair(p / m, p % m)); } vi parentof, vis, dist; vvi g; void dfs(int p) { vis[p] = 1; f(g[p].size()) { if (vis[g[p][i]] == 0) { parentof[g[p][i]] = p; dist[g[p][i]] = dist[p] + 1; dfs(g[p][i]); } } } vi lps(1000); void create_lps(string p) { ll m = p.length(); fi(1, m) { if (p[i] == p[lps[i - 1]]) { lps[i] = lps[i - 1] + 1; } else { ll l = lps[i - 1]; while (l > 0 && p[i] != p[l]) { l = lps[l - 1]; } if (l == 0 && p[i] == p[0]) { l = 1; } lps[i] = l; } } } ll kmp(string s, string p) { ll l = 0; f(s.length()) { if (s[i] == p[l]) { l++; } else { while (l > 0) { l = lps[l - 1]; if (s[i] == p[l]) { l++; break; } } } if (l == p.length()) { return i - l + 1; } } return -1; } vvi dp(401, vi(401)); vi heights(401); ll calcHeight(ll x, ll y) { ll ans = heights[y]; if (x > 0) { ans -= heights[x - 1]; } return ans; } ll calcCost(ll x, ll y) { if (x == y) { return 0; } if (dp[x][y] != 0) { return dp[x][y]; } ll ans = inf; fi(x, y) { ll xi = calcCost(x, i), iy = calcCost(i + 1, y); ans = min(ans, xi + calcHeight(x, i) + iy + calcHeight(i + 1, y)); } dp[x][y] = ans; return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; f(n) { cin >> heights[i]; } fi(1, n) { heights[i] += heights[i - 1]; } ll ans = calcCost(0, n - 1); cout << ans; return 0; }
insert
218
218
218
219
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; // #define int ll using PII = pair<ll, ll>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; } template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &a) { out << '(' << a.first << ',' << a.second << ')'; return out; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) { is >> x; } return is; } template <class T> ostream &operator<<(ostream &out, const vector<T> &a) { out << '['; for (T i : a) { out << i << ','; } out << ']'; return out; } int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL const int INF = 1 << 30; const ll LLINF = 1LL << 60; const int MOD = 1000000007; ll dp[405][405]; signed main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vector<ll> a(n); REP(i, n) cin >> a[i]; vector<ll> b(a); FOR(i, 1, n) b[i] += b[i - 1]; REP(i, n) REP(j, n) dp[i][j] = LLINF; REP(i, n) dp[i][i] = 0; FOR(w, 2, n + 1) { REP(l, n) { ll r = l + w - 1; FOR(i, l, r) { chmin(dp[l][r], dp[l][i] + dp[i + 1][r] + b[r] - (l == 0 ? 0 : b[l - 1])); } } } cout << dp[0][n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; // #define int ll using PII = pair<ll, ll>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; } template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &a) { out << '(' << a.first << ',' << a.second << ')'; return out; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) { is >> x; } return is; } template <class T> ostream &operator<<(ostream &out, const vector<T> &a) { out << '['; for (T i : a) { out << i << ','; } out << ']'; return out; } int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL const int INF = 1 << 30; const ll LLINF = 1LL << 60; const int MOD = 1000000007; ll dp[405][405]; signed main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vector<ll> a(n); REP(i, n) cin >> a[i]; vector<ll> b(a); FOR(i, 1, n) b[i] += b[i - 1]; REP(i, n) REP(j, n) dp[i][j] = LLINF; REP(i, n) dp[i][i] = 0; FOR(w, 2, n + 1) { REP(l, n) { ll r = l + w - 1; if (r >= n) break; FOR(i, l, r) { chmin(dp[l][r], dp[l][i] + dp[i + 1][r] + b[r] - (l == 0 ? 0 : b[l - 1])); } } } cout << dp[0][n - 1] << endl; return 0; }
insert
73
73
73
75
0
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long int #define lld long double #define pb push_back #define pii pair<ll, ll> #define mi map<ll, ll> #define vec vector<ll> #define all(a) (a).begin(), (a).end() #define F first #define S second #define mod 1000000007 #define rep(i, a, b) for (ll i = a; i < b; i++) #define repi(i, a, b) for (ll i = a; i <= b; i++) #define per(i, a, b) for (ll i = a; i >= b; i--) #define mp make_pair #define mit map<ll, ll>::iterator #define sit set<ll>::iterator #define pit pair<ll, ll>::iterator #define tr(container, it) \ for (__typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) using namespace std; ll xo(ll x, ll y) { return (x | y) & (~x | ~y); } ll bin_Expo(ll x, ll n) { if (x == 0) return 0; if (n == 0) return 1; else if (n % 2 == 0) // n is even return bin_Expo(x * x, n / 2); else // n is odd return x * bin_Expo(x * x, (n - 1) / 2); } ll mod_Expo(ll x, ll n, ll M) { if (x == 0) return 0; if (n == 0) return 1; else if (n % 2 == 0) // n is even return mod_Expo((x * x) % M, n / 2, M); else // n is odd return (x * mod_Expo((x * x) % M, (n - 1) / 2, M)) % M; } ll NcR(int n, int r) { ll p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r) { p *= n; k *= r; ll m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else p = 1; return p; } bool prime_check(ll x) { bool prime = (x >= 2); for (ll i = 2; i * i <= x; i++) { if (x % i == 0) { prime = false; break; } } return prime; } ll logg(ll base, ll x) { return (ll)(log(x) / log(base)); } ll dp[405][405], a[405]; ll sum(ll x, ll y) { ll d = 0; repi(i, x, y) d += a[i]; return d; } ll f(ll st, ll ed) { if (st > ed) return 1000000000000000; if (st == ed) return dp[st][ed] = 0; if (dp[st][ed] != -1) return dp[st][ed]; ll ans = 1000000000000000, b; repi(i, st, ed - 1) { ans = min(ans, f(st, i) + f(i + 1, ed) + sum(st, ed)); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // cout<<setprecision(12)<<fixed; int TESTS = 1; // cin>>TESTS; while (TESTS--) { ll i, j, k, l, m, first, last, n; cin >> n; vec v; rep(i, 0, n) cin >> a[i]; repi(i, 0, n) repi(j, 0, n) dp[i][j] = -1; cout << f(0, n - 1); } return 0; }
#include <bits/stdc++.h> #define ll long long int #define lld long double #define pb push_back #define pii pair<ll, ll> #define mi map<ll, ll> #define vec vector<ll> #define all(a) (a).begin(), (a).end() #define F first #define S second #define mod 1000000007 #define rep(i, a, b) for (ll i = a; i < b; i++) #define repi(i, a, b) for (ll i = a; i <= b; i++) #define per(i, a, b) for (ll i = a; i >= b; i--) #define mp make_pair #define mit map<ll, ll>::iterator #define sit set<ll>::iterator #define pit pair<ll, ll>::iterator #define tr(container, it) \ for (__typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) using namespace std; ll xo(ll x, ll y) { return (x | y) & (~x | ~y); } ll bin_Expo(ll x, ll n) { if (x == 0) return 0; if (n == 0) return 1; else if (n % 2 == 0) // n is even return bin_Expo(x * x, n / 2); else // n is odd return x * bin_Expo(x * x, (n - 1) / 2); } ll mod_Expo(ll x, ll n, ll M) { if (x == 0) return 0; if (n == 0) return 1; else if (n % 2 == 0) // n is even return mod_Expo((x * x) % M, n / 2, M); else // n is odd return (x * mod_Expo((x * x) % M, (n - 1) / 2, M)) % M; } ll NcR(int n, int r) { ll p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r) { p *= n; k *= r; ll m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else p = 1; return p; } bool prime_check(ll x) { bool prime = (x >= 2); for (ll i = 2; i * i <= x; i++) { if (x % i == 0) { prime = false; break; } } return prime; } ll logg(ll base, ll x) { return (ll)(log(x) / log(base)); } ll dp[405][405], a[405]; ll sum(ll x, ll y) { ll d = 0; repi(i, x, y) d += a[i]; return d; } ll f(ll st, ll ed) { if (st > ed) return 1000000000000000; if (st == ed) return dp[st][ed] = 0; if (dp[st][ed] != -1) return dp[st][ed]; ll ans = 1000000000000000, b; repi(i, st, ed - 1) { ans = min(ans, f(st, i) + f(i + 1, ed) + sum(st, ed)); } return dp[st][ed] = ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // cout<<setprecision(12)<<fixed; int TESTS = 1; // cin>>TESTS; while (TESTS--) { ll i, j, k, l, m, first, last, n; cin >> n; vec v; rep(i, 0, n) cin >> a[i]; repi(i, 0, n) repi(j, 0, n) dp[i][j] = -1; cout << f(0, n - 1); } return 0; }
replace
88
89
88
89
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; using namespace std; typedef pair<int, int> pii; #define endl '\n' #define F first #define S second const int MAX_N = 4e2 + 4; int n; int a[MAX_N]; ll dp[MAX_N][MAX_N], na[MAX_N][MAX_N]; int main() { memset(dp, 0x7F, sizeof(dp)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), dp[i][i] = 0, na[i][i] = a[i]; for (int d = 1; d < n; d++) for (int i = 1, j = i + d; i <= n; i++, j++) for (int k = i; k < j; k++) { ll tmp = dp[i][k] + dp[k + 1][j] + na[i][k] + na[k + 1][j]; if (dp[i][j] > tmp) { dp[i][j] = tmp; na[i][j] = na[i][k] + na[k + 1][j]; } else if (dp[i][j] == tmp) na[i][j] = min(na[i][j], na[i][k] + na[k + 1][j]); } printf("%lld\n", dp[1][n]); return 0; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; typedef pair<int, int> pii; #define endl '\n' #define F first #define S second const int MAX_N = 4e2 + 4; int n; int a[MAX_N]; ll dp[MAX_N][MAX_N], na[MAX_N][MAX_N]; int main() { memset(dp, 0x7F, sizeof(dp)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), dp[i][i] = 0, na[i][i] = a[i]; for (int d = 1; d < n; d++) for (int i = 1, j = i + d; j <= n; i++, j++) for (int k = i; k < j; k++) { ll tmp = dp[i][k] + dp[k + 1][j] + na[i][k] + na[k + 1][j]; if (dp[i][j] > tmp) { dp[i][j] = tmp; na[i][j] = na[i][k] + na[k + 1][j]; } else if (dp[i][j] == tmp) na[i][j] = min(na[i][j], na[i][k] + na[k + 1][j]); } printf("%lld\n", dp[1][n]); return 0; }
replace
19
20
19
20
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 400; int n, a[N]; long long sum[N], mem[N][N]; bool vis[N][N]; long long dp(int i, int j) { if (i == j) { return 0; } if (vis[i][j]) { return mem[i][j]; } vis[i][j] = true; long long ans = LLONG_MAX; for (int m = i; m < j; m++) { ans = min(ans, dp(i, m) + dp(m + 1, j)); } long long cost = sum[j] - sum[i - 1]; return mem[i][j] = cost + ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } cout << dp(1, n) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 405; int n, a[N]; long long sum[N], mem[N][N]; bool vis[N][N]; long long dp(int i, int j) { if (i == j) { return 0; } if (vis[i][j]) { return mem[i][j]; } vis[i][j] = true; long long ans = LLONG_MAX; for (int m = i; m < j; m++) { ans = min(ans, dp(i, m) + dp(m + 1, j)); } long long cost = sum[j] - sum[i - 1]; return mem[i][j] = cost + ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } cout << dp(1, n) << '\n'; return 0; }
replace
2
3
2
3
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int dp[500][500], sum[500][500]; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int n, x; cin >> n; for (int i = 0; i < n; i++) { cin >> x; for (int j = 0; j <= i; j++) sum[j][i] = x; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { sum[i][j] += sum[i][j - 1]; } if (i < n - 1) dp[i][i + 1] = sum[i][i + 1]; } for (int len = 2; len < n; len++) for (int i = 0; i < n - 1; i++) { int j = i + len; dp[i][j] = sum[i][i] + sum[i + 1][j] + dp[i + 1][j]; for (int k = i + 1; k < j; k++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + sum[i][k] + sum[k + 1][j]); } } cout << dp[0][n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int dp[500][500], sum[500][500]; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int n, x; cin >> n; for (int i = 0; i < n; i++) { cin >> x; for (int j = 0; j <= i; j++) sum[j][i] = x; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { sum[i][j] += sum[i][j - 1]; } if (i < n - 1) dp[i][i + 1] = sum[i][i + 1]; } for (int len = 2; len < n; len++) for (int i = 0; i < n - len; i++) { int j = i + len; dp[i][j] = sum[i][i] + sum[i + 1][j] + dp[i + 1][j]; for (int k = i + 1; k < j; k++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + sum[i][k] + sum[k + 1][j]); } } cout << dp[0][n - 1]; return 0; }
replace
22
23
22
23
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define mp make_pair #define fi first #define se second #define VAL 100000 #define ll long long #define INF 10000000000000000LL #define pll pair<long long, long long> const int MAX = 1e5 + 10; const int MOD = 1e9 + 7; const int TOT_PRIMES = 19; const int MAX_A = 70; using namespace std; vector<int> adj[MAX]; int coun(int x) { return __builtin_popcount(x); } ll dp[400][400]; int n; int main() { // ios_base::sync_with_stdio(false); // cin.tie(NULL); // #ifndef ONLINE_JUDGE // freopen("inp.txt" ,"r", stdin ); // freopen("out.txt", "w", stdout); // #endif cin >> n; ll arr[n + 1]; for (int i = 1; i <= n; i++) cin >> arr[i]; ll pre[n + 1]; memset(pre, 0, sizeof(pre)); for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + arr[i]; for (int i = n; i >= 1; i--) { for (int j = i; j <= n; j++) { if (i == j) { dp[i][j] = 0; continue; } dp[i][j] = INF; for (int k = i; k < j; k++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + pre[j] - pre[i - 1]); } // cout<<dp[i][j]<<" "<<i<<" "<<j<<"\n"; } } cout << dp[1][n]; }
#include <bits/stdc++.h> #define mp make_pair #define fi first #define se second #define VAL 100000 #define ll long long #define INF 10000000000000000LL #define pll pair<long long, long long> const int MAX = 1e5 + 10; const int MOD = 1e9 + 7; const int TOT_PRIMES = 19; const int MAX_A = 70; using namespace std; vector<int> adj[MAX]; int coun(int x) { return __builtin_popcount(x); } ll dp[405][405]; int n; int main() { // ios_base::sync_with_stdio(false); // cin.tie(NULL); // #ifndef ONLINE_JUDGE // freopen("inp.txt" ,"r", stdin ); // freopen("out.txt", "w", stdout); // #endif cin >> n; ll arr[n + 1]; for (int i = 1; i <= n; i++) cin >> arr[i]; ll pre[n + 1]; memset(pre, 0, sizeof(pre)); for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + arr[i]; for (int i = n; i >= 1; i--) { for (int j = i; j <= n; j++) { if (i == j) { dp[i][j] = 0; continue; } dp[i][j] = INF; for (int k = i; k < j; k++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + pre[j] - pre[i - 1]); } // cout<<dp[i][j]<<" "<<i<<" "<<j<<"\n"; } } cout << dp[1][n]; }
replace
15
16
15
16
0
p03173
C++
Runtime Error
// Exp val. // https://atcoder.jp/contests/dp/tasks/dp_j // https://codeforces.com/blog/entry/64250?#comment-523543 #include <bits/stdc++.h> //Carefully Crafted by hetp111 using namespace std; #define int long long #define double long double #define all(v) (v).begin(), (v).end() #define vi vector<int> #define vvi vector<vi> #define pii pair<int, int> #define vii vector<pii> #define MOD (1000000007) // #define MOD (998244353) #define PI acos(-1) #define eps (1e-8) #define INF (1e18) #define FASTER \ ios_base::sync_with_stdio(0); \ cin.tie(0) template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { for (const A &it : a) out << it << " "; return out; } template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) { return in >> a.first >> a.second; } template <class A> istream &operator>>(istream &in, vector<A> &a) { for (A &i : a) in >> i; return in; } // ifstream cinn("input.txt");ofstream coutt("output.txt"); int n; int dp[100][100]; int v[100]; int sum(int i, int j) { if (i == 0) return v[j]; return v[j] - v[i - 1]; } int f(int i, int j) { if (i == j) return 0; int &ans = dp[i][j]; if (ans != -1) return ans; ans = INF; for (int k = i; k < j; k++) { ans = min(ans, f(i, k) + f(k + 1, j) + sum(i, j)); } return ans; } signed main() { FASTER; memset(dp, -1, sizeof dp); cin >> n; for (int i = 0; i < n; i++) cin >> v[i]; for (int i = 1; i < n; i++) v[i] += v[i - 1]; cout << f(0, n - 1); }
// Exp val. // https://atcoder.jp/contests/dp/tasks/dp_j // https://codeforces.com/blog/entry/64250?#comment-523543 #include <bits/stdc++.h> //Carefully Crafted by hetp111 using namespace std; #define int long long #define double long double #define all(v) (v).begin(), (v).end() #define vi vector<int> #define vvi vector<vi> #define pii pair<int, int> #define vii vector<pii> #define MOD (1000000007) // #define MOD (998244353) #define PI acos(-1) #define eps (1e-8) #define INF (1e18) #define FASTER \ ios_base::sync_with_stdio(0); \ cin.tie(0) template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { for (const A &it : a) out << it << " "; return out; } template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) { return in >> a.first >> a.second; } template <class A> istream &operator>>(istream &in, vector<A> &a) { for (A &i : a) in >> i; return in; } // ifstream cinn("input.txt");ofstream coutt("output.txt"); int n; int dp[400][400]; int v[400]; int sum(int i, int j) { if (i == 0) return v[j]; return v[j] - v[i - 1]; } int f(int i, int j) { if (i == j) return 0; int &ans = dp[i][j]; if (ans != -1) return ans; ans = INF; for (int k = i; k < j; k++) { ans = min(ans, f(i, k) + f(k + 1, j) + sum(i, j)); } return ans; } signed main() { FASTER; memset(dp, -1, sizeof dp); cin >> n; for (int i = 0; i < n; i++) cin >> v[i]; for (int i = 1; i < n; i++) v[i] += v[i - 1]; cout << f(0, n - 1); }
replace
40
42
40
42
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long signed main() { ios_base::sync_with_stdio(false); int n; cin >> n; int dp[n + 1][n + 1]; int arr[n + 1]; int prefix[n + 1]; arr[0] = 0; for (int i = 1; i <= n; i++) { cin >> arr[i]; dp[i][i] = 0; dp[i - 1][i] = arr[i - 1] + arr[i]; } partial_sum(arr, arr + 1 + n, prefix); for (int i = n; i >= 1; i--) { for (int j = i + 2; j <= n; j++) { dp[i][j] = LLONG_MAX; for (int k = i; k <= j; k++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + prefix[j] - prefix[i - 1]); } } } cout << dp[1][n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long signed main() { ios_base::sync_with_stdio(false); int n; cin >> n; int dp[n + 1][n + 1]; int arr[n + 1]; int prefix[n + 1]; arr[0] = 0; for (int i = 1; i <= n; i++) { cin >> arr[i]; dp[i][i] = 0; dp[i - 1][i] = arr[i - 1] + arr[i]; } partial_sum(arr, arr + 1 + n, prefix); for (int i = n; i >= 1; i--) { for (int j = i + 2; j <= n; j++) { dp[i][j] = LLONG_MAX; for (int k = i; k < j; k++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + prefix[j] - prefix[i - 1]); } } } cout << dp[1][n] << endl; return 0; }
replace
21
22
21
22
0
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long const int MOD = 10000000000007LL; int dp[410][410] = {}; int a[410]; //[l, r) int solve(int l, int r) { if (!dp[l][r] == MOD) return dp[l][r]; if (r - l == 1) { dp[l][r] = 0; return dp[l][r]; } int rec = MOD; int sum = 0; for (int i = l; i < r; i++) { sum += a[i]; } for (int i = l + 1; i < r; i++) { rec = min(rec, solve(l, i) + solve(i, r) + sum); } dp[l][r] = rec; return dp[l][r]; } signed main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = MOD; } } cout << solve(0, n) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long const int MOD = 10000000000007LL; int dp[410][410] = {}; int a[410]; //[l, r) int solve(int l, int r) { // cout << l << " " << r << endl; if (!(dp[l][r] == MOD)) return dp[l][r]; if (r - l == 1) { dp[l][r] = 0; return dp[l][r]; } int rec = MOD; int sum = 0; for (int i = l; i < r; i++) { sum += a[i]; } for (int i = l + 1; i < r; i++) { rec = min(rec, solve(l, i) + solve(i, r) + sum); } dp[l][r] = rec; return dp[l][r]; } signed main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = MOD; } } cout << solve(0, n) << endl; return 0; }
replace
12
13
12
14
TLE
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = V<VV<T>>; template <class S, class T> using P = pair<S, T>; template <class... T> using TP = tuple<T...>; using ll = long long; using ull = unsigned long long; using dbl = double; using str = string; using vll = V<ll>; using vvll = V<vll>; using vvvll = V<vvll>; using pl = P<ll, ll>; using tl = TP<ll, ll, ll>; using vpl = V<pl>; using vvpl = V<vpl>; using vtl = V<tl>; using vvtl = V<vtl>; using vs = V<str>; using vvs = V<vs>; using vd = V<dbl>; using vvd = V<vd>; using vvvd = V<vvd>; using qll = queue<ll>; using qpl = queue<pl>; using stll = stack<ll>; using stpl = stack<pl>; using mapll = map<ll, ll>; using setll = set<ll>; using pqll = priority_queue<ll>; // #define int ll #define fi first #define se second #define mp make_pair #define mt make_tuple #define pb push_back #define pob pop_back() #define pf push_front #define pof pop_front() #define sz size() #define bgn begin() #define en end() #define asn assign #define emp empty() #define fr front() #define bk back() #define clr clear() #define ins insert #define ers erase #define res resize #define tp top() #define p_q priority_queue #define inv inverse() #define FOR(i, a, b) for (ll i = (a); i <= (ll)(b); i++) #define rFOR(i, a, b) for (ll i = (b); i >= (ll)(a); i--) #define REP(i, a) FOR((i), 0, (ll)(a)-1) #define REP0(i, a) FOR((i), 0, (ll)(a)) #define REP1(i, a) FOR((i), 1, (ll)(a)) #define rREP(i, a) rFOR((i), 0, (ll)(a)-1) #define rREP0(i, a) rFOR((i), 0, (ll)(a)) #define rREP1(i, a) rFOR((i), 1, (ll)(a)) #define ROR(v, i) for (auto &(i) : (v)) #define IOTA(a, n) iota((a).bgn, (a).en, (n)) #define SORT(a) sort((a).bgn, (a).en) #define rSORT(a) sort((a).rbegin(), (a).rend()) #define UNIQUE(a) (a).erase(unique((a).bgn, (a).en), (a).en) #define PREVP(a) prev_permutation((a).bgn, (a).en) #define NEXTP(a) next_permutation((a).bgn, (a).en) #define BINS(a, b) binary_search((a).bgn, (a).en, (b)) #define LOWB(a, b) (lower_bound((a).bgn, (a).en, (b)) - (a).bgn) #define UPB(a, b) (upper_bound((a).bgn, (a).en, (b)) - (a).bgn) #define CNT(a, b) count((a).bgn, (a).en, b) #define SUM(a) accumulate((a).bgn, (a).en, 0) #define REV(a) reverse((a).bgn, (a).en) #define REGS(a, b) regex_search((a), regex(b)) #define REGM(a, b) regex_match((a), regex(b)) #define yn(a) cout << ((a) ? "yes" : "no") << "\n"; #define Yn(a) cout << ((a) ? "Yes" : "No") << "\n"; #define YN(a) cout << ((a) ? "YES" : "NO") << "\n"; #define imp(a) cout << ((a) ? "possible" : "impossible") << "\n"; #define Imp(a) cout << ((a) ? "Possible" : "Impossible") << "\n"; #define IMP(a) cout << ((a) ? "POSSIBLE" : "IMPOSSIBLE") << "\n"; #define fs(a) cout << ((a) ? "second" : "first") << "\n"; #define Fs(a) cout << ((a) ? "Second" : "First") << "\n"; #define FS(a) cout << ((a) ? "SECOND" : "FIRST") << "\n"; // #define say(a) cout <<(a); // #define sal(a) cout <<(a)<<"\n"; #define sak cout << "\n"; #define sas cout << " "; #define sat cout << "\t"; #define dbg(a) cerr << (#a) << ": " << (a) << "\n"; #define dbgs(...) \ dal(#__VA_ARGS__); \ dal(__VA_ARGS__); #define c2l(a) ((ll)(a - 48)) #define a2l(a) ((ll)(a - 97)) #define A2l(a) ((ll)(a - 65)) #define l2c(a) ((char)(a + 48)) #define l2a(a) ((char)(a + 97)) #define l2A(a) ((char)(a + 65)) #define DigN2(a) ((llabs(a) == 0) ? (1) : ((ll)(log2(double(llabs(a)))) + 1)) #define DigN10(a) ((llabs(a) == 0) ? (1) : ((ll)(log10(double(llabs(a)))) + 1)) #define Dig2(a, b) (((a) >> (b)) & 1) #define Dig10(a, b) (ll)(((a) / ((ll)(pow(10.0, (double)(b))))) % 10) #define Pow2(a) ((ll)(1) << (a)) #define Pow10(a) ((ll)(pow(10.0, double(a)))) #define LSB(a) ((a) & (-(a))) /*#define llin(a) ll (a);cin >>(a); #define llin2(a,b) ll (a),(b);cin >>(a)>>(b); #define llin3(a,b,c) ll (a),(b),(c);cin >>(a)>>(b)>>(c); #define stin(a) string (a);cin >>(a);*/ #define vin(v) \ ROR((v), (i)) { cin >> (i); }; #define vllin(N, v) \ vll(v)((N)); \ vin(v); #define vllin2(N, a, b) \ vll(a)(N), (b)(N); \ REP(i, N) { cin >> (a)[i] >> (b)[i]; }; #define vsin(N, v) \ vs(v)((N)); \ vin(v); #define rdn(a, b) ((a) / (b)) #define rou(a, b) \ ((((double(a) / double(b)) - ((a) / (b))) < 0.5) ? ((a) / (b)) \ : (((a) / (b)) + 1)) #define rup(a, b) ((((a) % (b)) == 0) ? ((a) / (b)) : (((a) / (b)) + 1)) #define powll(a, b) (ll)(pow((double)(a), (double)(b))) #define Triangle(x1, y1, x2, y2, x3, y3) \ (((x1) - (x2)) * ((y1) - (y3)) - ((x1) - (x3)) * ((y1) - (y2))) #define tg(t, i) get<i>(t) #define Id(x) get<0>(x) #define Act(x) get<1>(x) #define InvAct(x) get<2>(x) #define mg(id, act) mt(id, act, lam(l)) // #define MonoidSet(T) TP<T, function<T(T, T)>> #define GroupSet(T) TP<T, function<T(T, T)>, function<T(T, T)>> #define CompareSet(T) TP<T, function<bool(T, T)>> #define lam(lr) ([](auto l, auto r) { return (lr); }) #define elam(lr) ([=](auto l, auto r) { return (lr); }) #define clam(lr) ([&](auto l, auto r) { return (lr); }) #define lamr(lr) ([](auto l, auto r) { lr }) #define elamr(lr) ([=](auto l, auto r) { lr }) #define clamr(lr) ([&](auto l, auto r) { lr }) #define min(...) Operation(MIN, __VA_ARGS__) #define max(...) Operation(MAX, __VA_ARGS__) #define gcd(...) Operation(GCD, __VA_ARGS__) #define lcm(...) Operation(LCM, __VA_ARGS__) #define vmin(...) VOperation(MIN, __VA_ARGS__) #define vmax(...) VOperation(MAX, __VA_ARGS__) #define vgcd(...) VOperation(GCD, __VA_ARGS__) #define vlcm(...) VOperation(LCM, __VA_ARGS__) #define vsum(...) VOperation(ADD, __VA_ARGS__) #define vpro(...) VOperation(MUL, __VA_ARGS__) #define emin(a, ...) ((a) = min((a), __VA_ARGS__)) #define emax(a, ...) ((a) = max((a), __VA_ARGS__)) #define egcd(a, ...) ((a) = gcd((a), __VA_ARGS__)) #define elcm(a, ...) ((a) = lcm((a), __VA_ARGS__)) #define ope Operation #define vope VOperation #define svll SumV<ll> #define svvll SumV2<ll> #define li(...) \ ll __VA_ARGS__; \ Input(__VA_ARGS__); #define si(...) \ str __VA_ARGS__; \ Input(__VA_ARGS__); // #define vli(size, ...) vll __VA_ARGS__;vInitInput(size,__VA_ARGS__); #define vlr(size, ...) \ vll __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vlc(size, ...) \ vll __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vli(size, ...) \ vll __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vsr(size, ...) \ vs __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vsc(size, ...) \ vs __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vsi(size, ...) \ vs __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vli2(rowSize, columnSize, ...) \ vvll __VA_ARGS__; \ vInitInput2(rowSize, columnSize, __VA_ARGS__); #define vplr(size, ...) \ vpl __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vplc(size, ...) \ vpl __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vpli(size, ...) \ vpl __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; // const ll MOD = 924844033; // const ll MOD = 9007199254740881; const ll INF = 1LL << 60; // 1.15e18 const double PI = acos(-1.0); const vll DX = {0, -1, 0, 1, 0, -1, 1, 1, -1}; const vll DY = {0, 0, -1, 0, 1, -1, -1, 1, 1}; const str alp = "abcdefghijklmnopqrstuvwxyz"; const str ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; template <class T> auto GetVectorValueType(T v) { return v; } template <class T> auto GetVectorValueType(V<T> v) { return GetVectorValueType(T()); } template <class S, class T> istream &operator>>(istream &in, P<S, T> &p) { return in >> p.fi >> p.se; } template <class T> istream &operator>>(istream &in, V<T> &v) { REP(i, v.sz) in >> v[i]; return in; } void Input() {} template <class Var, class... Args> void Input(Var &var, Args &...args) { cin >> var; Input(args...); } void vInit(ll size) {} template <class T, class... Args> void vInit(ll size, V<T> &v, Args &...args) { v.res(size); vInit(size, args...); } void vInput(ll size) {} template <class T, class... Args> void vInput(ll size, V<T> &v, Args &...args) { REP(i, size) cin >> v[i]; vInput(size, args...); } void vInputR(ll size) {} template <class T, class... Args> void vInputR(ll size, V<T> &v, Args &...args) { REP(i, size) cin >> v[i]; vInputR(size, args...); } void vInputNumC(ll num) {} template <class T, class... Args> void vInputNumC(ll num, V<T> &v, Args &...args) { cin >> v[num]; vInputNumC(num, args...); } void vInputC(ll size) {} template <class... Args> void vInputC(ll size, Args &...args) { REP(i, size) vInputNumC(i, args...); } void vInitInputR(ll size) {} template <class... Args> void vInitInputR(ll size, Args &...args) { vInit(size, args...); vInputR(size, args...); } void vInitInputC(ll size) {} template <class... Args> void vInitInputC(ll size, Args &...args) { vInit(size, args...); vInputC(size, args...); } void vInit2(ll rowSize, ll columnSize) {} template <class T, class... Args> void vInit2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) { v.asn(rowSize, V<T>(columnSize)); vInit2(rowSize, columnSize, args...); } void vInput2(ll rowSize, ll columnSize) {} template <class T, class... Args> void vInput2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) { REP(r, rowSize) { REP(c, columnSize) { cin >> v[r][c]; } } vInput2(rowSize, columnSize, args...); } void vInitInput2(ll rowSize, ll columnSize) {} template <class... Args> void vInitInput2(ll rowSize, ll columnSize, Args &...args) { vInit2(rowSize, columnSize, args...); vInput2(rowSize, columnSize, args...); } template <class S, class T> ostream &operator<<(ostream &out, const P<S, T> &p) { return out << "[" << p.fi << ", " << p.se << "]"; } template <class T> ostream &operator<<(ostream &out, V<T> &v) { if (v.emp) return out << "{}"; else { auto itr = v.bgn; out << "{" << *itr; itr++; while (itr != v.en) { out << ", " << *itr; itr++; } out << "}"; return out; } } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> &m) { if (m.emp) return out << "<[]>"; else { auto itr = m.bgn; out << "< [" << (itr->fi) << ": " << (itr->se); itr++; while (itr != m.en) { out << "], [" << (itr->fi) << ": " << (itr->se); itr++; } out << "] >"; return out; } } template <class T> ostream &operator<<(ostream &out, const set<T> &s) { if (s.emp) return out << "<>"; else { auto itr = s.bgn; out << "<" << *itr; itr++; while (itr != s.en) { out << ", " << *itr; itr++; } out << ">"; return out; } } void say() {} template <class T> void say(T t) { cout << t; } template <class Head, class... Body> void say(Head head, Body... body) { cout << head << " "; say(body...); } void sal() { cout << "\n"; } template <class... Args> void sal(Args... args) { say(args...); cout << "\n"; } void day() {} template <class T> void day(T t) { cerr << t; } template <class Head, class... Body> void day(Head head, Body... body) { cerr << head << " "; day(body...); } void dal() { cerr << "\n"; } template <class... Args> void dal(Args... args) { day(args...); cerr << "\n"; } void salv() {} template <class T> void salv(V<T> v) { if (v.emp) sal(); else { auto itr = v.bgn; say(*itr); itr++; while (itr != v.en) { sas; say(*itr); itr++; } sak; } } template <class T> void salv(VV<T> v) { if (v.emp) sal(); else { ROR(v, i) salv(i); } } template <class T, class... Args> void salv(T v, Args... args) { salv(v); salv(args...); } template <class L, class R> auto Gcd(L l, R r) -> decltype(l + r) { if (l < r) swap(l, r); return r ? Gcd(r, l % r) : l; } template <class L, class R> auto Lcm(L l, R r) { if (!l || !r) return 0; return l / Gcd(l, r) * r; } /* auto LES = mp(INF, lam(return l < r;)); auto GRT = mp(-INF, lam(return l > r;)); auto EQ = mp(0, lam(return l == r;)); auto ADD = mp(0, lam(return l + r;)); auto SUB = mp(0, lam(return l - r;)); auto MUL = mp(1, lam(return l * r;)); auto DIV = mp(1, lam(return l / r;)); auto MDL = mp(1, lam(return l % r;)); auto XOR = mp(0, lam(return l ^ r;)); auto OR = mp(0, lam(return l | r;)); auto AND = mp(((ll)(1) << 63) - 1, lam(return l & r;)); auto MIN = mp(INF, lam(return (l < r) ? l : r;)); auto MAX = mp(-INF, lam(return (l > r) ? l : r;)); auto GCD = mp(0, lam(return Gcd(l, r);)); auto LCM = mp(1, lam(return Lcm(l, r);)); */ auto LES = mp(INF, lam(l < r)); auto GRT = mp(-INF, lam(l > r)); auto EQ = mp(0, lam(l == r)); auto ADD = mt(0, lam(l + r), lam(l - r)); auto MUL = mt(1, lam(l *r), lam(l / r)); auto XOR = mt(0, lam(l ^ r), lam(l ^ r)); auto OR = mg(0, lam(l | r)); auto AND = mg(((ll)(1) << 63) - 1, lam(l &r)); auto MIN = mg(0, lam((l < r) ? l : r)); auto MAX = mg(0, lam((l > r) ? l : r)); auto GCD = mg(0, lam(Gcd(l, r))); auto LCM = mg(0, lam(Lcm(l, r))); template <class OperationType> auto Operation(OperationType A) { return Id(A); } template <class OperationType, class T> auto Operation(OperationType A, T x) { return x; } template <class OperationType, class T, class... Args> auto Operation(OperationType A, T x, Args... args) { auto tmp = Operation(A, args...); return Act(A)(x, tmp); } template <class OperationType> auto VOperation(OperationType A) { return Id(A); } template <class OperationType, class T> auto VOperation(OperationType A, T x) { return x; } template <class OperationType, class T> auto VOperation(OperationType A, V<T> v) { if (v.emp) { decltype(GetVectorValueType(T())) tmp = Id(A); return tmp; } auto tmp = VOperation(A, v[0]); FOR(i, 1, v.sz - 1) tmp = Act(A)(tmp, VOperation(A, v[i])); return tmp; } template <class OperationType, class T, class... Args> auto VOperation(OperationType A, T x, Args... args) { auto xResult = VOperation(A, x); auto tmp = VOperation(A, args...); return Act(A)(xResult, tmp); } ll Bset(ll a, ll b, ll c) { if (c) a |= b; else a &= ~b; return a; } struct UFT { public: ll tsize; ll mode; vll par; vll rank; UFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); } ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } } ll size(ll x) { return -par[root(x)]; } }; template <class T> struct pUFT { public: ll tsize; ll now; vll par; vll rank; vll mtime; vvll sizepi; VV<T> sizepv; V<T> elm; GroupSet(T) Add; pUFT(ll tsize, GroupSet(T) Add = ADD) : tsize(tsize), Add(Add) { init(); } void init() { now = 0; par.asn(tsize, -1); rank.asn(tsize, 0); mtime.asn(tsize, INF); sizepi.asn(tsize, {0}); sizepv.asn(tsize, {}); } void set(ll x, T s) { elm[x] = s; sizepv[x] = {s}; } ll root(ll x, ll t) { return (mtime[x] > t) ? x : root(par[x], t); } bool same(ll x, ll y, ll t) { return root(x, t) == root(y, t); } ll merge(ll x, ll y) { now++; x = root(x, now); y = root(y, now); if (x != y) { if (rank[x] < rank[y]) { elm[y] = Act(Add)(elm[x], elm[y]); sizepi[y].pb(now); sizepv[y].pb(elm[y]); par[x] = y; mtime[x] = now; } else { elm[x] = Act(Add)(elm[x], elm[y]); sizepi[x].pb(now); sizepv[x].pb(elm[x]); par[y] = x; mtime[y] = now; if (rank[x] == rank[y]) rank[x]++; } } return now; } T size(ll x, ll t) { x = root(x, t); return sizepv[x][UPB(sizepi[x], t) - 1]; } }; struct wUFT { public: ll tsize; ll mode; vll par; vll rank; vll dweight; wUFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); dweight.asn(tsize, 0); } ll root(ll x) { if (par[x] < 0) return x; else { ll r = root(par[x]); dweight[x] += dweight[par[x]]; return par[x] = r; } } ll weight(ll x) { root(x); return dweight[x]; } ll diff(ll x, ll y) { return weight(y) - weight(x); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y, ll w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; dweight[x] = -w; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; dweight[y] = w; } } } ll size(ll x) { return -par[root(x)]; } }; template <class T> struct sUFT { public: ll tsize; ll mode; vll par; vll rank; GroupSet(T) Add; V<T> elm; sUFT(ll tsize, GroupSet(T) Add = ADD, ll mode = 0) : tsize(tsize), Add(Add), mode(mode) { init(); } void init() { par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); elm.asn(tsize, Id(Add)); } ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (mode) { elm[x] = Act(Add)(elm[x], elm[y]); par[y] = x; } else { if (rank[x] < rank[y]) { elm[y] = Act(Add)(elm[x], elm[y]); par[x] = y; } else { elm[x] = Act(Add)(elm[x], elm[y]); par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } } T size(ll x) { return elm[root(x)]; } T &operator[](ll x) { return elm[x]; } }; template <typename valtype> class SegT { public: ll size; vector<valtype> v; valtype initv; function<valtype(valtype x, valtype y)> calc; SegT() {} SegT(const SegT &segt) {} SegT(ll sizeget, ll modeget = 0) { sizeset(sizeget); modeset(modeget); init(); } SegT(vector<valtype> cpyvec, ll modeget = 0) { sizeset(cpyvec.sz); modeset(modeget); init(); copy(cpyvec); } SegT(ll sizeget, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(sizeget); initv = initvget; calc = calcget; init(); } SegT(vector<valtype> cpyvec, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(cpyvec.sz); initv = initvget; calc = calcget; init(); copy(cpyvec); } void sizeset(ll rsize) { size = DigN2(rsize); if (rsize == Pow2(size - 1)) size--; return; } void modeset(ll mode) { switch (mode) { case 0: initv = 0; calc = [](valtype x, valtype y) { return x + y; }; break; case 1: initv = INF; calc = [](valtype x, valtype y) { return min(x, y); }; break; case 2: initv = -INF; calc = [](valtype x, valtype y) { return max(x, y); }; break; } return; } void init() { v.asn(Pow2(size + 1) - 1, initv); } void copy(vector<valtype> cpyvec) { REP(i, min(cpyvec.sz, Pow2(size))) set(i, cpyvec[i]); } ll i2v(ll i) const { if (i < 0 || i >= Pow2(size)) return -1; return Pow2(size) + i - 1; } ll top(ll i) const { if (i == 0) return -1; return (i - 1) / 2; } pl bot(ll i) const { if (i + 1 >= Pow2(size)) return mp(-1, -1); return mp(2 * i + 1, 2 * i + 2); } void set(ll i, valtype x) { i = i2v(i); v[i] = x; while (i > 0) { i = top(i); v[i] = calc(v[bot(i).fi], v[bot(i).se]); } return; } void add(ll i, valtype x) { set(i, v[i2v(i)] + x); return; } valtype operator[](const ll &i) const { return v[i2v(i)]; } // valtype que(ll a = 0, ll b = Pow2(size) - 1) { valtype que(ll a, ll b) { if (a == b) return v[i2v(a)]; if (a > b) return initv; // swap(a, b); valtype ans = initv; ll ai = i2v(a); ll bi = i2v(b); FOR(i, 1, size + 1) { if (a > b) break; if (a % Pow2(i)) { ans = calc(ans, v[ai]); a += Pow2(i - 1); ai = top(ai) + 1; } else { ai = top(ai); } if (a > b) break; if ((b + 1) % Pow2(i)) { ans = calc(ans, v[bi]); b -= Pow2(i - 1); bi = top(bi) - 1; } else { bi = top(bi); } if (a > b) break; } return ans; } valtype que(ll b) { return que(0, b); } valtype que() { return que(0, Pow2(size) - 1); } }; /*template <class Type> class DP { public: vector<Type> v; Type initv; vll size, block; DP() {} DP(const DP &dp) {} template<class... Args> DP(Args... args) { block.asn(1, 1); Initialize(args...); } void Initialize(Type initv_) { initv = initv_; v.asn(block.bk, initv); } template<class... Args> void Initialize(ll val, Args... args) { size.pb(val); block.pb(block.bk*val); Initialize(args...); } };*/ pl Bezout(ll a, ll b) { if (b != 0) { pl xy; xy = Bezout(b, a % b); return mp(xy.se, xy.fi - ((a / b) * xy.se)); } else { return mp(1, 0); } } pl Bez(ll a, ll b, ll c) { pl xy; ll x, y, z, gc; xy = Bezout(a, b); gc = gcd(a, b); if (c % gc != 0) return mp(-1, -1); x = xy.fi * (c / gc); y = xy.se * (c / gc); if (x < 0) z = rup(-x, (b / gc)); if (x >= 0) z = -x / (b / gc); x += z * (b / gc); y -= z * (a / gc); return mp(x, y); } ll DigS10(ll n) { ll ans = 0; while (1) { ans += n % 10; n /= 10; if (!n) break; } return ans; } ll isP(ll n) { if (n <= 1) return 0; FOR(i, 2, (ll)sqrt(n) + 1) { if (n % i == 0) return 0; } return 1; } ll Tot(ll n) { if (n <= 0) return 0; ll ans = n, x = 2; while (x * x <= n) { if (n % x == 0) { ans -= ans / x; while (n % x == 0) n /= x; } x++; } if (n > 1) ans -= ans / n; return ans; } template <class T> struct Graph { public: ll vSize; ll eMode; ll mapMode; GroupSet(T) Add; CompareSet(T) Less; CompareSet(T) Equal; VV<P<T, ll>> adj; map<pl, T> len; Graph(ll vSize, ll eMode = 0, ll mapMode = 0, GroupSet(T) Add = ADD, CompareSet(T) Less = LES, CompareSet(T) Equal = EQ) : vSize(vSize), eMode(eMode), mapMode(mapMode), Add(Add), Less(Less), Equal(Equal) {} void Init() { adj.asn(vSize, V<P<T, ll>>()); } void AddE(ll x, ll y, T cost) { iAddE(x, y, cost); if (!eMode) iAddE(y, x, cost); } void iAddE(ll x, ll y, T cost) { adj[x].pb(mp(cost, y)); if (mapMode) len[mp(x, y)] = cost; } P<bool, T> getE(ll x, ll y) { if (!len.count(mp(x, y))) return mp(false, Id(Less)); return mp(true, len[mp(x, y)]); } V<T> Dijk(ll x) { V<T> ans(vSize, Id(Less)); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans[now.se] = segt[now.se].fi; segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se)); continue; } if (Act(Less)(Act(Add)(ans[now.se], i.fi), segt[i.se].fi)) { segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se)); continue; } } } return ans; } V<P<T, vll>> rDijk(ll x) { V<P<T, vll>> ans(vSize, mp(Id(Less), vll())); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans[now.se].fi = segt[now.se].fi; segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se)); ans[i.se].se = {now.se}; continue; } if (Act(Less)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) { segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se)); ans[i.se].se = {now.se}; continue; } if (Act(Equal)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) { ans[i.se].se.pb(now.se); continue; } } } return ans; } T Prim(ll x = 0) { T ans = Id(Add); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans = Act(Add)(ans, segt[now.se].fi); segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, i); continue; } if (Act(Less)(i.fi, segt[i.se].fi)) { segt.set(i.se, i); continue; } } } return ans; } P<T, V<P<T, vll>>> rPrim(ll x = 0) { P<T, V<P<T, vll>>> ans = mp(Id(Add), V<P<T, vll>>(vSize, mp(Id(Less), vll()))); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans.se[now.se].fi = segt[now.se].fi; ans.fi = Act(Add)(ans.fi, ans.se[now.se].fi); segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, i); ans.se[i.se].se = {now.se}; continue; } if (Act(Less)(i.fi, segt[i.se].fi)) { segt.set(i.se, i); ans.se[i.se].se = {now.se}; continue; } if (Act(Equal)(i.fi, segt[i.se].fi)) { ans.se[i.se].se.pb(now.se); continue; } } } return ans; } }; template <class T> struct Sum { public: V<T> v, s; ll size; GroupSet(T) Add; Sum(V<T> v, GroupSet(T) Add = ADD) : v(v), size(v.sz), Add(Add) { Init(); Calc(); } void Init() { s.asn(size + 1, Id(Add)); } void Calc() { REP(i, size) s[i + 1] = Act(Add)(s[i], v[i]); } T operator()(ll x) { if (x < -1) x = -1; if (x > size - 1) x = size - 1; return s[x + 1]; } T operator()(ll l, ll r) { if (l < 0) l = 0; if (r >= size) r = size - 1; if (l > r) return Id(Add); return InvAct(Add)(s[r + 1], s[l]); } }; using sumll = Sum<ll>; template <class T> struct Sum2 { public: VV<T> v, s; ll RowSize, ColumnSize; GroupSet(T) Add; Sum2(VV<T> v, GroupSet(T) Add = ADD) : v(v), RowSize(v.sz), ColumnSize(v.sz ? v[0].sz : 0), Add(Add) { Init(); Calc(); } void Init() { s.asn(RowSize + 1, V<T>(ColumnSize + 1, Id(Add))); } void Calc() { REP1(r, RowSize) { REP1(c, ColumnSize) { // s[r][c] = InvAct(Add)(Act(Add)(Act(Add)(v[r - //1][c - 1], operator()(r - 1, c - 2)), operator()(r - 2, c - 1)), //operator()(r - 2, c - 2)); s[r][c] = Act(Add)(s[r][c - 1], v[r - 1][c - 1]); } } REP1(r, RowSize) { REP1(c, ColumnSize) { s[r][c] = Act(Add)(s[r - 1][c], s[r][c]); } } } T operator()(ll r, ll c) { if (r < -1) r = -1; if (c < -1) c = -1; if (r > RowSize - 1) r = RowSize - 1; if (c > ColumnSize - 1) c = ColumnSize - 1; return s[r + 1][c + 1]; } T operator()(ll r1, ll c1, ll r2, ll c2) { if (r1 < 0) r1 = 0; if (c1 < 0) c1 = 0; if (r2 >= RowSize) r2 = RowSize - 1; if (c2 >= ColumnSize) c2 = ColumnSize - 1; if (r1 > r2) return Id(Add); if (c1 > c2) return Id(Add); return InvAct(Add)(Act(Add)(s[r2 + 1][c2 + 1], s[r1][c1]), Act(Add)(s[r2 + 1][c1], s[r1][c2 + 1])); } }; using sumll2 = Sum2<ll>; template <class T> struct Point2 { public: VV<T> v; ll h, w; Point2() : h(0), w(0) {} Point2(ll h, ll w) : h(h), w(w) { asn(h, w); } Point2(ll h, ll w, T val) : h(h), w(w) { asn(h, w, val); } Point2(VV<T> cv) : h(cv.sz), w(cv.sz ? cv[0].sz : 0) { asn(h, w); copy(cv); } void assign(ll h, ll w) { v.asn(h, V<T>(w)); } void assign(ll h, ll w, ll val) { v.asn(h, V<T>(w, val)); } void copy(VV<T> cv) { REP(_h, h) REP(_w, w) v[_h][_w] = cv[_h][_w]; } T &operator()(ll h, ll w) { return v[h][w]; } T &operator()(pl p) { return v[p.fi][p.se]; } T &operator[](pl p) { return v[p.fi][p.se]; } }; template <class T> using P2 = Point2<T>; template <ll Mod> struct Modll { public: ll v; Modll() : v(0) {} Modll(ll _v) { set(_v % Mod + Mod); } Modll &set(ll _v) { v = (_v < Mod) ? _v : (_v - Mod); return *this; } Modll pow(ll n) const { Modll x = *this, ans = 1; while (n) { if (n & 1) ans *= x; x *= x; n >>= 1; } return ans; } Modll inverse() const { return (*this).pow(Mod - 2); } Modll operator+(const Modll &m) const { return Modll().set(v + m.v); } Modll operator-(const Modll &m) const { return Modll().set(Mod + v - m.v); } Modll operator*(const Modll &m) const { return Modll().set((ull(v) * m.v) % Mod); } Modll operator/(const Modll &m) const { return *this * m.inv; } Modll &operator+=(const Modll &m) { return *this = *this + m; } Modll &operator-=(const Modll &m) { return *this = *this - m; } Modll &operator*=(const Modll &m) { return *this = *this * m; } Modll &operator/=(const Modll &m) { return *this = *this / m; } Modll operator-() const { return Modll(0) - *this; } explicit operator bool() const { return v != 0; } friend istream &operator>>(istream &in, Modll &m) { return in >> m.v; } friend ostream &operator<<(ostream &out, const Modll &m) { return out << m.v; } }; using mll = Modll<MOD>; using vmll = V<mll>; using vvmll = V<vmll>; using vvvmll = V<vvmll>; vmll MFactMemo(2, 1); vmll MIFactMemo(2, 1); mll mFact(ll n) { if (MFactMemo.sz <= n) { ll oldsize = MFactMemo.sz; MFactMemo.res(n + 1, 1); FOR(i, oldsize, n) MFactMemo[i] = MFactMemo[i - 1] * i; } return MFactMemo[n]; } mll miFact(ll n) { if (MIFactMemo.sz <= n) { ll oldsize = MIFactMemo.sz; MIFactMemo.res(n + 1, 1); MIFactMemo.bk = mFact(n).inv; rFOR(i, oldsize + 1, n) MIFactMemo[i - 1] = MIFactMemo[i] * i; } return MIFactMemo[n]; } mll mComb(ll n, ll k) { if (n < 0 || k < 0 || n < k) return 0; return mFact(n) * miFact(k) * miFact(n - k); } ll LIS(vll v, ll m = 0) { if (v.sz > 0) { ll ans = 0; vll dp(v.sz, INF); FOR(i, 0, v.sz - 1) { dp[m ? UPB(dp, v[i]) : LOWB(dp, v[i])] = v[i]; } FOR(i, 0, v.sz - 1) { if (dp[i] == INF) break; ans++; } return ans; } else { return 0; } } ll Bsrch(function<bool(ll x)> f, ll mi, ll ma) { ll ng = mi - 1, ok = ma, mid; while (ok - ng > 1) { mid = (ng + ok) / 2; (f(mid) ? ok : ng) = mid; } return ok; } template <class T, class M = decltype(MUL), class S = decltype(ADD)> VV<T> MultiMatrix(VV<T> A, VV<T> B, M Mul = MUL, S Add = ADD) { VV<T> ans; ll ii = A.sz; if (!ii) return ans; ll jj = A[0].sz; if (!jj) return ans; ll jj2 = B.sz; if (!jj2) return ans; if (jj != jj2) return ans; ll kk = B[0].sz; if (!kk) return ans; ans.asn(ii, V<T>(kk, 0)); REP(i, ii) { REP(k, kk) { REP(j, jj) { ans[i][k] = Act(Add)(ans[i][k], Act(Mul)(A[i][j], B[j][k])); } } } return ans; } vvll CombMemo(1000, vll(1000, -1)); ll Comb(ll n, ll k) { if ((n < 0) || (k < 0)) return 0; if (CombMemo[n][k] == -1) { if (n < k) CombMemo[n][k] = 0; else { if (n == 0) CombMemo[n][k] = 1; else if (k == 0) CombMemo[n][k] = 1; else if (n == k) CombMemo[n][k] = 1; else CombMemo[n][k] = Comb(n - 1, k - 1) + Comb(n - 1, k); } } return CombMemo[n][k]; } template <class T> map<T, ll> Dict(V<T> v) { map<T, ll> m; if (!v.sz) return m; SORT(v); UNIQUE(v); REP(i, v.sz) { m[v[i]] = i; } return m; } template <class T> vll Cmprs(V<T> v) { auto m = Dict(v); vll ans(v.sz); REP(i, v.sz) { ans[i] = m[v[i]]; } return ans; } template <class T> vll PCmprs(V<T> v) { if (v.sz == 0) return V<T>(); vll tmp(v.sz); vll ans(v.sz); IOTA(tmp, 0); IOTA(ans, 0); sort(tmp.bgn, tmp.en, clam(v[l] < v[r])); sort(ans.bgn, ans.en, clam(tmp[l] < tmp[r])); return ans; } ll BblCnt(vll rv) { vll v = PCmprs(rv); SegT<ll> b(v.sz, 0); ll ans = 0; REP(i, v.sz) { ans += (i - b.que(0, v[i])); b.add(v[i], 1); } return ans; } pl NGrid(pl p, ll i, ll H, ll W) { p = mp(p.fi + DX[i], p.se + DY[i]); if (p.fi < 0 || p.fi >= H || p.se < 0 || p.se >= W) return mp(INF, INF); return p; } vvll llGrid(vs v) { vvll r(v.sz, vll(v.sz ? v[0].sz : 0, 0)); REP(h, v.sz) REP(w, v.sz ? v[0].sz : 0) r[h][w] = (v[h][w] == '#'); return r; } template <class T> auto ven(T val) { return val; } template <> auto ven<int>(int val) { return (ll)val; } template <class T, class... Args> auto ven(T val, Args... args) { auto tmp = ven(args...); return V<decltype(tmp)>(val, tmp); } template <class T> void zind(T &v) { v--; } template <class T> void zind(V<T> &v) { ROR(v, i) zind(i); } template <class T, class... Args> void zind(T &v, Args &...args) { zind(v); zind(args...); } void Solve(); signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20) << fixed; Solve(); } ll N; vll a; vll b; vvll dp; ll rec(ll x, ll y) { if (x == y) return dp[x][y] = 0; ll tmp = INF; FOR(i, x, y - 1) { emin(tmp, rec(x, i) + rec(i + 1, y) + b[y + 1] - b[x]); } return dp[x][y] = tmp; } void Solve() { cin >> N; a = ven(N, 0); b = ven(N + 1, 0); dp = ven(N, N, -1); vInput(N, a); REP1(i, N) b[i] = b[i - 1] + a[i - 1]; sal(rec(0, N - 1)); }
#include <bits/stdc++.h> using namespace std; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = V<VV<T>>; template <class S, class T> using P = pair<S, T>; template <class... T> using TP = tuple<T...>; using ll = long long; using ull = unsigned long long; using dbl = double; using str = string; using vll = V<ll>; using vvll = V<vll>; using vvvll = V<vvll>; using pl = P<ll, ll>; using tl = TP<ll, ll, ll>; using vpl = V<pl>; using vvpl = V<vpl>; using vtl = V<tl>; using vvtl = V<vtl>; using vs = V<str>; using vvs = V<vs>; using vd = V<dbl>; using vvd = V<vd>; using vvvd = V<vvd>; using qll = queue<ll>; using qpl = queue<pl>; using stll = stack<ll>; using stpl = stack<pl>; using mapll = map<ll, ll>; using setll = set<ll>; using pqll = priority_queue<ll>; // #define int ll #define fi first #define se second #define mp make_pair #define mt make_tuple #define pb push_back #define pob pop_back() #define pf push_front #define pof pop_front() #define sz size() #define bgn begin() #define en end() #define asn assign #define emp empty() #define fr front() #define bk back() #define clr clear() #define ins insert #define ers erase #define res resize #define tp top() #define p_q priority_queue #define inv inverse() #define FOR(i, a, b) for (ll i = (a); i <= (ll)(b); i++) #define rFOR(i, a, b) for (ll i = (b); i >= (ll)(a); i--) #define REP(i, a) FOR((i), 0, (ll)(a)-1) #define REP0(i, a) FOR((i), 0, (ll)(a)) #define REP1(i, a) FOR((i), 1, (ll)(a)) #define rREP(i, a) rFOR((i), 0, (ll)(a)-1) #define rREP0(i, a) rFOR((i), 0, (ll)(a)) #define rREP1(i, a) rFOR((i), 1, (ll)(a)) #define ROR(v, i) for (auto &(i) : (v)) #define IOTA(a, n) iota((a).bgn, (a).en, (n)) #define SORT(a) sort((a).bgn, (a).en) #define rSORT(a) sort((a).rbegin(), (a).rend()) #define UNIQUE(a) (a).erase(unique((a).bgn, (a).en), (a).en) #define PREVP(a) prev_permutation((a).bgn, (a).en) #define NEXTP(a) next_permutation((a).bgn, (a).en) #define BINS(a, b) binary_search((a).bgn, (a).en, (b)) #define LOWB(a, b) (lower_bound((a).bgn, (a).en, (b)) - (a).bgn) #define UPB(a, b) (upper_bound((a).bgn, (a).en, (b)) - (a).bgn) #define CNT(a, b) count((a).bgn, (a).en, b) #define SUM(a) accumulate((a).bgn, (a).en, 0) #define REV(a) reverse((a).bgn, (a).en) #define REGS(a, b) regex_search((a), regex(b)) #define REGM(a, b) regex_match((a), regex(b)) #define yn(a) cout << ((a) ? "yes" : "no") << "\n"; #define Yn(a) cout << ((a) ? "Yes" : "No") << "\n"; #define YN(a) cout << ((a) ? "YES" : "NO") << "\n"; #define imp(a) cout << ((a) ? "possible" : "impossible") << "\n"; #define Imp(a) cout << ((a) ? "Possible" : "Impossible") << "\n"; #define IMP(a) cout << ((a) ? "POSSIBLE" : "IMPOSSIBLE") << "\n"; #define fs(a) cout << ((a) ? "second" : "first") << "\n"; #define Fs(a) cout << ((a) ? "Second" : "First") << "\n"; #define FS(a) cout << ((a) ? "SECOND" : "FIRST") << "\n"; // #define say(a) cout <<(a); // #define sal(a) cout <<(a)<<"\n"; #define sak cout << "\n"; #define sas cout << " "; #define sat cout << "\t"; #define dbg(a) cerr << (#a) << ": " << (a) << "\n"; #define dbgs(...) \ dal(#__VA_ARGS__); \ dal(__VA_ARGS__); #define c2l(a) ((ll)(a - 48)) #define a2l(a) ((ll)(a - 97)) #define A2l(a) ((ll)(a - 65)) #define l2c(a) ((char)(a + 48)) #define l2a(a) ((char)(a + 97)) #define l2A(a) ((char)(a + 65)) #define DigN2(a) ((llabs(a) == 0) ? (1) : ((ll)(log2(double(llabs(a)))) + 1)) #define DigN10(a) ((llabs(a) == 0) ? (1) : ((ll)(log10(double(llabs(a)))) + 1)) #define Dig2(a, b) (((a) >> (b)) & 1) #define Dig10(a, b) (ll)(((a) / ((ll)(pow(10.0, (double)(b))))) % 10) #define Pow2(a) ((ll)(1) << (a)) #define Pow10(a) ((ll)(pow(10.0, double(a)))) #define LSB(a) ((a) & (-(a))) /*#define llin(a) ll (a);cin >>(a); #define llin2(a,b) ll (a),(b);cin >>(a)>>(b); #define llin3(a,b,c) ll (a),(b),(c);cin >>(a)>>(b)>>(c); #define stin(a) string (a);cin >>(a);*/ #define vin(v) \ ROR((v), (i)) { cin >> (i); }; #define vllin(N, v) \ vll(v)((N)); \ vin(v); #define vllin2(N, a, b) \ vll(a)(N), (b)(N); \ REP(i, N) { cin >> (a)[i] >> (b)[i]; }; #define vsin(N, v) \ vs(v)((N)); \ vin(v); #define rdn(a, b) ((a) / (b)) #define rou(a, b) \ ((((double(a) / double(b)) - ((a) / (b))) < 0.5) ? ((a) / (b)) \ : (((a) / (b)) + 1)) #define rup(a, b) ((((a) % (b)) == 0) ? ((a) / (b)) : (((a) / (b)) + 1)) #define powll(a, b) (ll)(pow((double)(a), (double)(b))) #define Triangle(x1, y1, x2, y2, x3, y3) \ (((x1) - (x2)) * ((y1) - (y3)) - ((x1) - (x3)) * ((y1) - (y2))) #define tg(t, i) get<i>(t) #define Id(x) get<0>(x) #define Act(x) get<1>(x) #define InvAct(x) get<2>(x) #define mg(id, act) mt(id, act, lam(l)) // #define MonoidSet(T) TP<T, function<T(T, T)>> #define GroupSet(T) TP<T, function<T(T, T)>, function<T(T, T)>> #define CompareSet(T) TP<T, function<bool(T, T)>> #define lam(lr) ([](auto l, auto r) { return (lr); }) #define elam(lr) ([=](auto l, auto r) { return (lr); }) #define clam(lr) ([&](auto l, auto r) { return (lr); }) #define lamr(lr) ([](auto l, auto r) { lr }) #define elamr(lr) ([=](auto l, auto r) { lr }) #define clamr(lr) ([&](auto l, auto r) { lr }) #define min(...) Operation(MIN, __VA_ARGS__) #define max(...) Operation(MAX, __VA_ARGS__) #define gcd(...) Operation(GCD, __VA_ARGS__) #define lcm(...) Operation(LCM, __VA_ARGS__) #define vmin(...) VOperation(MIN, __VA_ARGS__) #define vmax(...) VOperation(MAX, __VA_ARGS__) #define vgcd(...) VOperation(GCD, __VA_ARGS__) #define vlcm(...) VOperation(LCM, __VA_ARGS__) #define vsum(...) VOperation(ADD, __VA_ARGS__) #define vpro(...) VOperation(MUL, __VA_ARGS__) #define emin(a, ...) ((a) = min((a), __VA_ARGS__)) #define emax(a, ...) ((a) = max((a), __VA_ARGS__)) #define egcd(a, ...) ((a) = gcd((a), __VA_ARGS__)) #define elcm(a, ...) ((a) = lcm((a), __VA_ARGS__)) #define ope Operation #define vope VOperation #define svll SumV<ll> #define svvll SumV2<ll> #define li(...) \ ll __VA_ARGS__; \ Input(__VA_ARGS__); #define si(...) \ str __VA_ARGS__; \ Input(__VA_ARGS__); // #define vli(size, ...) vll __VA_ARGS__;vInitInput(size,__VA_ARGS__); #define vlr(size, ...) \ vll __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vlc(size, ...) \ vll __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vli(size, ...) \ vll __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vsr(size, ...) \ vs __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vsc(size, ...) \ vs __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vsi(size, ...) \ vs __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vli2(rowSize, columnSize, ...) \ vvll __VA_ARGS__; \ vInitInput2(rowSize, columnSize, __VA_ARGS__); #define vplr(size, ...) \ vpl __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); #define vplc(size, ...) \ vpl __VA_ARGS__; \ vInitInputC(size, __VA_ARGS__); #define vpli(size, ...) \ vpl __VA_ARGS__; \ vInitInputR(size, __VA_ARGS__); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; // const ll MOD = 924844033; // const ll MOD = 9007199254740881; const ll INF = 1LL << 60; // 1.15e18 const double PI = acos(-1.0); const vll DX = {0, -1, 0, 1, 0, -1, 1, 1, -1}; const vll DY = {0, 0, -1, 0, 1, -1, -1, 1, 1}; const str alp = "abcdefghijklmnopqrstuvwxyz"; const str ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; template <class T> auto GetVectorValueType(T v) { return v; } template <class T> auto GetVectorValueType(V<T> v) { return GetVectorValueType(T()); } template <class S, class T> istream &operator>>(istream &in, P<S, T> &p) { return in >> p.fi >> p.se; } template <class T> istream &operator>>(istream &in, V<T> &v) { REP(i, v.sz) in >> v[i]; return in; } void Input() {} template <class Var, class... Args> void Input(Var &var, Args &...args) { cin >> var; Input(args...); } void vInit(ll size) {} template <class T, class... Args> void vInit(ll size, V<T> &v, Args &...args) { v.res(size); vInit(size, args...); } void vInput(ll size) {} template <class T, class... Args> void vInput(ll size, V<T> &v, Args &...args) { REP(i, size) cin >> v[i]; vInput(size, args...); } void vInputR(ll size) {} template <class T, class... Args> void vInputR(ll size, V<T> &v, Args &...args) { REP(i, size) cin >> v[i]; vInputR(size, args...); } void vInputNumC(ll num) {} template <class T, class... Args> void vInputNumC(ll num, V<T> &v, Args &...args) { cin >> v[num]; vInputNumC(num, args...); } void vInputC(ll size) {} template <class... Args> void vInputC(ll size, Args &...args) { REP(i, size) vInputNumC(i, args...); } void vInitInputR(ll size) {} template <class... Args> void vInitInputR(ll size, Args &...args) { vInit(size, args...); vInputR(size, args...); } void vInitInputC(ll size) {} template <class... Args> void vInitInputC(ll size, Args &...args) { vInit(size, args...); vInputC(size, args...); } void vInit2(ll rowSize, ll columnSize) {} template <class T, class... Args> void vInit2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) { v.asn(rowSize, V<T>(columnSize)); vInit2(rowSize, columnSize, args...); } void vInput2(ll rowSize, ll columnSize) {} template <class T, class... Args> void vInput2(ll rowSize, ll columnSize, VV<T> &v, Args &...args) { REP(r, rowSize) { REP(c, columnSize) { cin >> v[r][c]; } } vInput2(rowSize, columnSize, args...); } void vInitInput2(ll rowSize, ll columnSize) {} template <class... Args> void vInitInput2(ll rowSize, ll columnSize, Args &...args) { vInit2(rowSize, columnSize, args...); vInput2(rowSize, columnSize, args...); } template <class S, class T> ostream &operator<<(ostream &out, const P<S, T> &p) { return out << "[" << p.fi << ", " << p.se << "]"; } template <class T> ostream &operator<<(ostream &out, V<T> &v) { if (v.emp) return out << "{}"; else { auto itr = v.bgn; out << "{" << *itr; itr++; while (itr != v.en) { out << ", " << *itr; itr++; } out << "}"; return out; } } template <class S, class T> ostream &operator<<(ostream &out, const map<S, T> &m) { if (m.emp) return out << "<[]>"; else { auto itr = m.bgn; out << "< [" << (itr->fi) << ": " << (itr->se); itr++; while (itr != m.en) { out << "], [" << (itr->fi) << ": " << (itr->se); itr++; } out << "] >"; return out; } } template <class T> ostream &operator<<(ostream &out, const set<T> &s) { if (s.emp) return out << "<>"; else { auto itr = s.bgn; out << "<" << *itr; itr++; while (itr != s.en) { out << ", " << *itr; itr++; } out << ">"; return out; } } void say() {} template <class T> void say(T t) { cout << t; } template <class Head, class... Body> void say(Head head, Body... body) { cout << head << " "; say(body...); } void sal() { cout << "\n"; } template <class... Args> void sal(Args... args) { say(args...); cout << "\n"; } void day() {} template <class T> void day(T t) { cerr << t; } template <class Head, class... Body> void day(Head head, Body... body) { cerr << head << " "; day(body...); } void dal() { cerr << "\n"; } template <class... Args> void dal(Args... args) { day(args...); cerr << "\n"; } void salv() {} template <class T> void salv(V<T> v) { if (v.emp) sal(); else { auto itr = v.bgn; say(*itr); itr++; while (itr != v.en) { sas; say(*itr); itr++; } sak; } } template <class T> void salv(VV<T> v) { if (v.emp) sal(); else { ROR(v, i) salv(i); } } template <class T, class... Args> void salv(T v, Args... args) { salv(v); salv(args...); } template <class L, class R> auto Gcd(L l, R r) -> decltype(l + r) { if (l < r) swap(l, r); return r ? Gcd(r, l % r) : l; } template <class L, class R> auto Lcm(L l, R r) { if (!l || !r) return 0; return l / Gcd(l, r) * r; } /* auto LES = mp(INF, lam(return l < r;)); auto GRT = mp(-INF, lam(return l > r;)); auto EQ = mp(0, lam(return l == r;)); auto ADD = mp(0, lam(return l + r;)); auto SUB = mp(0, lam(return l - r;)); auto MUL = mp(1, lam(return l * r;)); auto DIV = mp(1, lam(return l / r;)); auto MDL = mp(1, lam(return l % r;)); auto XOR = mp(0, lam(return l ^ r;)); auto OR = mp(0, lam(return l | r;)); auto AND = mp(((ll)(1) << 63) - 1, lam(return l & r;)); auto MIN = mp(INF, lam(return (l < r) ? l : r;)); auto MAX = mp(-INF, lam(return (l > r) ? l : r;)); auto GCD = mp(0, lam(return Gcd(l, r);)); auto LCM = mp(1, lam(return Lcm(l, r);)); */ auto LES = mp(INF, lam(l < r)); auto GRT = mp(-INF, lam(l > r)); auto EQ = mp(0, lam(l == r)); auto ADD = mt(0, lam(l + r), lam(l - r)); auto MUL = mt(1, lam(l *r), lam(l / r)); auto XOR = mt(0, lam(l ^ r), lam(l ^ r)); auto OR = mg(0, lam(l | r)); auto AND = mg(((ll)(1) << 63) - 1, lam(l &r)); auto MIN = mg(0, lam((l < r) ? l : r)); auto MAX = mg(0, lam((l > r) ? l : r)); auto GCD = mg(0, lam(Gcd(l, r))); auto LCM = mg(0, lam(Lcm(l, r))); template <class OperationType> auto Operation(OperationType A) { return Id(A); } template <class OperationType, class T> auto Operation(OperationType A, T x) { return x; } template <class OperationType, class T, class... Args> auto Operation(OperationType A, T x, Args... args) { auto tmp = Operation(A, args...); return Act(A)(x, tmp); } template <class OperationType> auto VOperation(OperationType A) { return Id(A); } template <class OperationType, class T> auto VOperation(OperationType A, T x) { return x; } template <class OperationType, class T> auto VOperation(OperationType A, V<T> v) { if (v.emp) { decltype(GetVectorValueType(T())) tmp = Id(A); return tmp; } auto tmp = VOperation(A, v[0]); FOR(i, 1, v.sz - 1) tmp = Act(A)(tmp, VOperation(A, v[i])); return tmp; } template <class OperationType, class T, class... Args> auto VOperation(OperationType A, T x, Args... args) { auto xResult = VOperation(A, x); auto tmp = VOperation(A, args...); return Act(A)(xResult, tmp); } ll Bset(ll a, ll b, ll c) { if (c) a |= b; else a &= ~b; return a; } struct UFT { public: ll tsize; ll mode; vll par; vll rank; UFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); } ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } } ll size(ll x) { return -par[root(x)]; } }; template <class T> struct pUFT { public: ll tsize; ll now; vll par; vll rank; vll mtime; vvll sizepi; VV<T> sizepv; V<T> elm; GroupSet(T) Add; pUFT(ll tsize, GroupSet(T) Add = ADD) : tsize(tsize), Add(Add) { init(); } void init() { now = 0; par.asn(tsize, -1); rank.asn(tsize, 0); mtime.asn(tsize, INF); sizepi.asn(tsize, {0}); sizepv.asn(tsize, {}); } void set(ll x, T s) { elm[x] = s; sizepv[x] = {s}; } ll root(ll x, ll t) { return (mtime[x] > t) ? x : root(par[x], t); } bool same(ll x, ll y, ll t) { return root(x, t) == root(y, t); } ll merge(ll x, ll y) { now++; x = root(x, now); y = root(y, now); if (x != y) { if (rank[x] < rank[y]) { elm[y] = Act(Add)(elm[x], elm[y]); sizepi[y].pb(now); sizepv[y].pb(elm[y]); par[x] = y; mtime[x] = now; } else { elm[x] = Act(Add)(elm[x], elm[y]); sizepi[x].pb(now); sizepv[x].pb(elm[x]); par[y] = x; mtime[y] = now; if (rank[x] == rank[y]) rank[x]++; } } return now; } T size(ll x, ll t) { x = root(x, t); return sizepv[x][UPB(sizepi[x], t) - 1]; } }; struct wUFT { public: ll tsize; ll mode; vll par; vll rank; vll dweight; wUFT(ll tsizeget, ll modeget = 0) { tsize = tsizeget; mode = modeget; par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); dweight.asn(tsize, 0); } ll root(ll x) { if (par[x] < 0) return x; else { ll r = root(par[x]); dweight[x] += dweight[par[x]]; return par[x] = r; } } ll weight(ll x) { root(x); return dweight[x]; } ll diff(ll x, ll y) { return weight(y) - weight(x); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y, ll w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return; if (mode) { par[x] += par[y]; par[y] = x; } else { if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; dweight[x] = -w; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; dweight[y] = w; } } } ll size(ll x) { return -par[root(x)]; } }; template <class T> struct sUFT { public: ll tsize; ll mode; vll par; vll rank; GroupSet(T) Add; V<T> elm; sUFT(ll tsize, GroupSet(T) Add = ADD, ll mode = 0) : tsize(tsize), Add(Add), mode(mode) { init(); } void init() { par.asn(tsize, -1); if (!mode) rank.res(tsize, 0); elm.asn(tsize, Id(Add)); } ll root(ll x) { return par[x] < 0 ? x : par[x] = root(par[x]); } bool isRoot(ll x) { return x == root(x); } bool same(ll x, ll y) { return root(x) == root(y); } void merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return; if (mode) { elm[x] = Act(Add)(elm[x], elm[y]); par[y] = x; } else { if (rank[x] < rank[y]) { elm[y] = Act(Add)(elm[x], elm[y]); par[x] = y; } else { elm[x] = Act(Add)(elm[x], elm[y]); par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } } T size(ll x) { return elm[root(x)]; } T &operator[](ll x) { return elm[x]; } }; template <typename valtype> class SegT { public: ll size; vector<valtype> v; valtype initv; function<valtype(valtype x, valtype y)> calc; SegT() {} SegT(const SegT &segt) {} SegT(ll sizeget, ll modeget = 0) { sizeset(sizeget); modeset(modeget); init(); } SegT(vector<valtype> cpyvec, ll modeget = 0) { sizeset(cpyvec.sz); modeset(modeget); init(); copy(cpyvec); } SegT(ll sizeget, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(sizeget); initv = initvget; calc = calcget; init(); } SegT(vector<valtype> cpyvec, valtype initvget, function<valtype(valtype x, valtype y)> calcget) { sizeset(cpyvec.sz); initv = initvget; calc = calcget; init(); copy(cpyvec); } void sizeset(ll rsize) { size = DigN2(rsize); if (rsize == Pow2(size - 1)) size--; return; } void modeset(ll mode) { switch (mode) { case 0: initv = 0; calc = [](valtype x, valtype y) { return x + y; }; break; case 1: initv = INF; calc = [](valtype x, valtype y) { return min(x, y); }; break; case 2: initv = -INF; calc = [](valtype x, valtype y) { return max(x, y); }; break; } return; } void init() { v.asn(Pow2(size + 1) - 1, initv); } void copy(vector<valtype> cpyvec) { REP(i, min(cpyvec.sz, Pow2(size))) set(i, cpyvec[i]); } ll i2v(ll i) const { if (i < 0 || i >= Pow2(size)) return -1; return Pow2(size) + i - 1; } ll top(ll i) const { if (i == 0) return -1; return (i - 1) / 2; } pl bot(ll i) const { if (i + 1 >= Pow2(size)) return mp(-1, -1); return mp(2 * i + 1, 2 * i + 2); } void set(ll i, valtype x) { i = i2v(i); v[i] = x; while (i > 0) { i = top(i); v[i] = calc(v[bot(i).fi], v[bot(i).se]); } return; } void add(ll i, valtype x) { set(i, v[i2v(i)] + x); return; } valtype operator[](const ll &i) const { return v[i2v(i)]; } // valtype que(ll a = 0, ll b = Pow2(size) - 1) { valtype que(ll a, ll b) { if (a == b) return v[i2v(a)]; if (a > b) return initv; // swap(a, b); valtype ans = initv; ll ai = i2v(a); ll bi = i2v(b); FOR(i, 1, size + 1) { if (a > b) break; if (a % Pow2(i)) { ans = calc(ans, v[ai]); a += Pow2(i - 1); ai = top(ai) + 1; } else { ai = top(ai); } if (a > b) break; if ((b + 1) % Pow2(i)) { ans = calc(ans, v[bi]); b -= Pow2(i - 1); bi = top(bi) - 1; } else { bi = top(bi); } if (a > b) break; } return ans; } valtype que(ll b) { return que(0, b); } valtype que() { return que(0, Pow2(size) - 1); } }; /*template <class Type> class DP { public: vector<Type> v; Type initv; vll size, block; DP() {} DP(const DP &dp) {} template<class... Args> DP(Args... args) { block.asn(1, 1); Initialize(args...); } void Initialize(Type initv_) { initv = initv_; v.asn(block.bk, initv); } template<class... Args> void Initialize(ll val, Args... args) { size.pb(val); block.pb(block.bk*val); Initialize(args...); } };*/ pl Bezout(ll a, ll b) { if (b != 0) { pl xy; xy = Bezout(b, a % b); return mp(xy.se, xy.fi - ((a / b) * xy.se)); } else { return mp(1, 0); } } pl Bez(ll a, ll b, ll c) { pl xy; ll x, y, z, gc; xy = Bezout(a, b); gc = gcd(a, b); if (c % gc != 0) return mp(-1, -1); x = xy.fi * (c / gc); y = xy.se * (c / gc); if (x < 0) z = rup(-x, (b / gc)); if (x >= 0) z = -x / (b / gc); x += z * (b / gc); y -= z * (a / gc); return mp(x, y); } ll DigS10(ll n) { ll ans = 0; while (1) { ans += n % 10; n /= 10; if (!n) break; } return ans; } ll isP(ll n) { if (n <= 1) return 0; FOR(i, 2, (ll)sqrt(n) + 1) { if (n % i == 0) return 0; } return 1; } ll Tot(ll n) { if (n <= 0) return 0; ll ans = n, x = 2; while (x * x <= n) { if (n % x == 0) { ans -= ans / x; while (n % x == 0) n /= x; } x++; } if (n > 1) ans -= ans / n; return ans; } template <class T> struct Graph { public: ll vSize; ll eMode; ll mapMode; GroupSet(T) Add; CompareSet(T) Less; CompareSet(T) Equal; VV<P<T, ll>> adj; map<pl, T> len; Graph(ll vSize, ll eMode = 0, ll mapMode = 0, GroupSet(T) Add = ADD, CompareSet(T) Less = LES, CompareSet(T) Equal = EQ) : vSize(vSize), eMode(eMode), mapMode(mapMode), Add(Add), Less(Less), Equal(Equal) {} void Init() { adj.asn(vSize, V<P<T, ll>>()); } void AddE(ll x, ll y, T cost) { iAddE(x, y, cost); if (!eMode) iAddE(y, x, cost); } void iAddE(ll x, ll y, T cost) { adj[x].pb(mp(cost, y)); if (mapMode) len[mp(x, y)] = cost; } P<bool, T> getE(ll x, ll y) { if (!len.count(mp(x, y))) return mp(false, Id(Less)); return mp(true, len[mp(x, y)]); } V<T> Dijk(ll x) { V<T> ans(vSize, Id(Less)); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans[now.se] = segt[now.se].fi; segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se)); continue; } if (Act(Less)(Act(Add)(ans[now.se], i.fi), segt[i.se].fi)) { segt.set(i.se, mp(Act(Add)(ans[now.se], i.fi), i.se)); continue; } } } return ans; } V<P<T, vll>> rDijk(ll x) { V<P<T, vll>> ans(vSize, mp(Id(Less), vll())); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans[now.se].fi = segt[now.se].fi; segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se)); ans[i.se].se = {now.se}; continue; } if (Act(Less)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) { segt.set(i.se, mp(Act(Add)(ans[now.se].fi, i.fi), i.se)); ans[i.se].se = {now.se}; continue; } if (Act(Equal)(Act(Add)(ans[now.se].fi, i.fi), segt[i.se].fi)) { ans[i.se].se.pb(now.se); continue; } } } return ans; } T Prim(ll x = 0) { T ans = Id(Add); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans = Act(Add)(ans, segt[now.se].fi); segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, i); continue; } if (Act(Less)(i.fi, segt[i.se].fi)) { segt.set(i.se, i); continue; } } } return ans; } P<T, V<P<T, vll>>> rPrim(ll x = 0) { P<T, V<P<T, vll>>> ans = mp(Id(Add), V<P<T, vll>>(vSize, mp(Id(Less), vll()))); if (x < 0 || x >= vSize) return ans; SegT<P<T, ll>> segt(vSize, mp(Id(Less), -1), clamr(if (l.se < 0) return r; if (r.se < 0) return l; return Act(Less)(l.fi, r.fi) ? l : r;)); segt.set(x, mp(Id(Add), x)); P<T, ll> now; while ((now = segt.que(0, vSize - 1)).se >= 0) { ans.se[now.se].fi = segt[now.se].fi; ans.fi = Act(Add)(ans.fi, ans.se[now.se].fi); segt.set(now.se, mp(Id(Less), -2)); ROR(adj[now.se], i) { if (segt[i.se].se == -2) continue; if (segt[i.se].se == -1) { segt.set(i.se, i); ans.se[i.se].se = {now.se}; continue; } if (Act(Less)(i.fi, segt[i.se].fi)) { segt.set(i.se, i); ans.se[i.se].se = {now.se}; continue; } if (Act(Equal)(i.fi, segt[i.se].fi)) { ans.se[i.se].se.pb(now.se); continue; } } } return ans; } }; template <class T> struct Sum { public: V<T> v, s; ll size; GroupSet(T) Add; Sum(V<T> v, GroupSet(T) Add = ADD) : v(v), size(v.sz), Add(Add) { Init(); Calc(); } void Init() { s.asn(size + 1, Id(Add)); } void Calc() { REP(i, size) s[i + 1] = Act(Add)(s[i], v[i]); } T operator()(ll x) { if (x < -1) x = -1; if (x > size - 1) x = size - 1; return s[x + 1]; } T operator()(ll l, ll r) { if (l < 0) l = 0; if (r >= size) r = size - 1; if (l > r) return Id(Add); return InvAct(Add)(s[r + 1], s[l]); } }; using sumll = Sum<ll>; template <class T> struct Sum2 { public: VV<T> v, s; ll RowSize, ColumnSize; GroupSet(T) Add; Sum2(VV<T> v, GroupSet(T) Add = ADD) : v(v), RowSize(v.sz), ColumnSize(v.sz ? v[0].sz : 0), Add(Add) { Init(); Calc(); } void Init() { s.asn(RowSize + 1, V<T>(ColumnSize + 1, Id(Add))); } void Calc() { REP1(r, RowSize) { REP1(c, ColumnSize) { // s[r][c] = InvAct(Add)(Act(Add)(Act(Add)(v[r - //1][c - 1], operator()(r - 1, c - 2)), operator()(r - 2, c - 1)), //operator()(r - 2, c - 2)); s[r][c] = Act(Add)(s[r][c - 1], v[r - 1][c - 1]); } } REP1(r, RowSize) { REP1(c, ColumnSize) { s[r][c] = Act(Add)(s[r - 1][c], s[r][c]); } } } T operator()(ll r, ll c) { if (r < -1) r = -1; if (c < -1) c = -1; if (r > RowSize - 1) r = RowSize - 1; if (c > ColumnSize - 1) c = ColumnSize - 1; return s[r + 1][c + 1]; } T operator()(ll r1, ll c1, ll r2, ll c2) { if (r1 < 0) r1 = 0; if (c1 < 0) c1 = 0; if (r2 >= RowSize) r2 = RowSize - 1; if (c2 >= ColumnSize) c2 = ColumnSize - 1; if (r1 > r2) return Id(Add); if (c1 > c2) return Id(Add); return InvAct(Add)(Act(Add)(s[r2 + 1][c2 + 1], s[r1][c1]), Act(Add)(s[r2 + 1][c1], s[r1][c2 + 1])); } }; using sumll2 = Sum2<ll>; template <class T> struct Point2 { public: VV<T> v; ll h, w; Point2() : h(0), w(0) {} Point2(ll h, ll w) : h(h), w(w) { asn(h, w); } Point2(ll h, ll w, T val) : h(h), w(w) { asn(h, w, val); } Point2(VV<T> cv) : h(cv.sz), w(cv.sz ? cv[0].sz : 0) { asn(h, w); copy(cv); } void assign(ll h, ll w) { v.asn(h, V<T>(w)); } void assign(ll h, ll w, ll val) { v.asn(h, V<T>(w, val)); } void copy(VV<T> cv) { REP(_h, h) REP(_w, w) v[_h][_w] = cv[_h][_w]; } T &operator()(ll h, ll w) { return v[h][w]; } T &operator()(pl p) { return v[p.fi][p.se]; } T &operator[](pl p) { return v[p.fi][p.se]; } }; template <class T> using P2 = Point2<T>; template <ll Mod> struct Modll { public: ll v; Modll() : v(0) {} Modll(ll _v) { set(_v % Mod + Mod); } Modll &set(ll _v) { v = (_v < Mod) ? _v : (_v - Mod); return *this; } Modll pow(ll n) const { Modll x = *this, ans = 1; while (n) { if (n & 1) ans *= x; x *= x; n >>= 1; } return ans; } Modll inverse() const { return (*this).pow(Mod - 2); } Modll operator+(const Modll &m) const { return Modll().set(v + m.v); } Modll operator-(const Modll &m) const { return Modll().set(Mod + v - m.v); } Modll operator*(const Modll &m) const { return Modll().set((ull(v) * m.v) % Mod); } Modll operator/(const Modll &m) const { return *this * m.inv; } Modll &operator+=(const Modll &m) { return *this = *this + m; } Modll &operator-=(const Modll &m) { return *this = *this - m; } Modll &operator*=(const Modll &m) { return *this = *this * m; } Modll &operator/=(const Modll &m) { return *this = *this / m; } Modll operator-() const { return Modll(0) - *this; } explicit operator bool() const { return v != 0; } friend istream &operator>>(istream &in, Modll &m) { return in >> m.v; } friend ostream &operator<<(ostream &out, const Modll &m) { return out << m.v; } }; using mll = Modll<MOD>; using vmll = V<mll>; using vvmll = V<vmll>; using vvvmll = V<vvmll>; vmll MFactMemo(2, 1); vmll MIFactMemo(2, 1); mll mFact(ll n) { if (MFactMemo.sz <= n) { ll oldsize = MFactMemo.sz; MFactMemo.res(n + 1, 1); FOR(i, oldsize, n) MFactMemo[i] = MFactMemo[i - 1] * i; } return MFactMemo[n]; } mll miFact(ll n) { if (MIFactMemo.sz <= n) { ll oldsize = MIFactMemo.sz; MIFactMemo.res(n + 1, 1); MIFactMemo.bk = mFact(n).inv; rFOR(i, oldsize + 1, n) MIFactMemo[i - 1] = MIFactMemo[i] * i; } return MIFactMemo[n]; } mll mComb(ll n, ll k) { if (n < 0 || k < 0 || n < k) return 0; return mFact(n) * miFact(k) * miFact(n - k); } ll LIS(vll v, ll m = 0) { if (v.sz > 0) { ll ans = 0; vll dp(v.sz, INF); FOR(i, 0, v.sz - 1) { dp[m ? UPB(dp, v[i]) : LOWB(dp, v[i])] = v[i]; } FOR(i, 0, v.sz - 1) { if (dp[i] == INF) break; ans++; } return ans; } else { return 0; } } ll Bsrch(function<bool(ll x)> f, ll mi, ll ma) { ll ng = mi - 1, ok = ma, mid; while (ok - ng > 1) { mid = (ng + ok) / 2; (f(mid) ? ok : ng) = mid; } return ok; } template <class T, class M = decltype(MUL), class S = decltype(ADD)> VV<T> MultiMatrix(VV<T> A, VV<T> B, M Mul = MUL, S Add = ADD) { VV<T> ans; ll ii = A.sz; if (!ii) return ans; ll jj = A[0].sz; if (!jj) return ans; ll jj2 = B.sz; if (!jj2) return ans; if (jj != jj2) return ans; ll kk = B[0].sz; if (!kk) return ans; ans.asn(ii, V<T>(kk, 0)); REP(i, ii) { REP(k, kk) { REP(j, jj) { ans[i][k] = Act(Add)(ans[i][k], Act(Mul)(A[i][j], B[j][k])); } } } return ans; } vvll CombMemo(1000, vll(1000, -1)); ll Comb(ll n, ll k) { if ((n < 0) || (k < 0)) return 0; if (CombMemo[n][k] == -1) { if (n < k) CombMemo[n][k] = 0; else { if (n == 0) CombMemo[n][k] = 1; else if (k == 0) CombMemo[n][k] = 1; else if (n == k) CombMemo[n][k] = 1; else CombMemo[n][k] = Comb(n - 1, k - 1) + Comb(n - 1, k); } } return CombMemo[n][k]; } template <class T> map<T, ll> Dict(V<T> v) { map<T, ll> m; if (!v.sz) return m; SORT(v); UNIQUE(v); REP(i, v.sz) { m[v[i]] = i; } return m; } template <class T> vll Cmprs(V<T> v) { auto m = Dict(v); vll ans(v.sz); REP(i, v.sz) { ans[i] = m[v[i]]; } return ans; } template <class T> vll PCmprs(V<T> v) { if (v.sz == 0) return V<T>(); vll tmp(v.sz); vll ans(v.sz); IOTA(tmp, 0); IOTA(ans, 0); sort(tmp.bgn, tmp.en, clam(v[l] < v[r])); sort(ans.bgn, ans.en, clam(tmp[l] < tmp[r])); return ans; } ll BblCnt(vll rv) { vll v = PCmprs(rv); SegT<ll> b(v.sz, 0); ll ans = 0; REP(i, v.sz) { ans += (i - b.que(0, v[i])); b.add(v[i], 1); } return ans; } pl NGrid(pl p, ll i, ll H, ll W) { p = mp(p.fi + DX[i], p.se + DY[i]); if (p.fi < 0 || p.fi >= H || p.se < 0 || p.se >= W) return mp(INF, INF); return p; } vvll llGrid(vs v) { vvll r(v.sz, vll(v.sz ? v[0].sz : 0, 0)); REP(h, v.sz) REP(w, v.sz ? v[0].sz : 0) r[h][w] = (v[h][w] == '#'); return r; } template <class T> auto ven(T val) { return val; } template <> auto ven<int>(int val) { return (ll)val; } template <class T, class... Args> auto ven(T val, Args... args) { auto tmp = ven(args...); return V<decltype(tmp)>(val, tmp); } template <class T> void zind(T &v) { v--; } template <class T> void zind(V<T> &v) { ROR(v, i) zind(i); } template <class T, class... Args> void zind(T &v, Args &...args) { zind(v); zind(args...); } void Solve(); signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20) << fixed; Solve(); } ll N; vll a; vll b; vvll dp; ll rec(ll x, ll y) { if (dp[x][y] != -1) return dp[x][y]; if (x == y) return dp[x][y] = 0; ll tmp = INF; FOR(i, x, y - 1) { emin(tmp, rec(x, i) + rec(i + 1, y) + b[y + 1] - b[x]); } return dp[x][y] = tmp; } void Solve() { cin >> N; a = ven(N, 0); b = ven(N + 1, 0); dp = ven(N, N, -1); vInput(N, a); REP1(i, N) b[i] = b[i - 1] + a[i - 1]; sal(rec(0, N - 1)); }
insert
1,397
1,397
1,397
1,399
TLE
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, a[401]; long long b[401], dp[401][401], mod = 1e9 + 7; long long func(int l, int r) { if (l == r) return dp[l][r] = 0; if (l + 1 == r) return dp[l][r] = a[l]; long long res = 1e18; for (int i = l + 1; i < r; ++i) res = min(res, func(l, i) + func(i, r) - b[l - 1] + b[r - 1]); return dp[l][r] = res; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i + 1]; b[i + 1] = a[i + 1]; b[i + 1] += b[i]; } cout << func(1, n + 1) - b[n] << "\n"; }
#include <bits/stdc++.h> using namespace std; int n, a[401]; long long b[401], dp[401][401], mod = 1e9 + 7; long long func(int l, int r) { if (l == r) return dp[l][r] = 0; if (l + 1 == r) return dp[l][r] = a[l]; if (dp[l][r] != 0) return dp[l][r]; long long res = 1e18; for (int i = l + 1; i < r; ++i) res = min(res, func(l, i) + func(i, r) - b[l - 1] + b[r - 1]); return dp[l][r] = res; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i + 1]; b[i + 1] = a[i + 1]; b[i + 1] += b[i]; } cout << func(1, n + 1) - b[n] << "\n"; }
insert
11
11
11
13
TLE
p03173
C++
Runtime Error
#include "bits/stdc++.h" #define all(x) (x).begin(), (x).end() using namespace std; const int MSIZE = 404; long long dp[MSIZE][MSIZE]; long long arr[MSIZE]; long long pref[MSIZE]; long long Sum(int l, int r) { l = max(l, 0); return pref[r] - (l ? pref[l - 1] : 0LL); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); memset(dp, 0, sizeof dp); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } memcpy(pref, arr, sizeof arr); for (int i = 1; i < n; i++) { pref[i] += pref[i - 1]; } for (int len = 1; len <= n; len++) { for (int l = 0; l < n; l++) { int r = l + len - 1; if (len == 1) { dp[l][r] = 0; } else { dp[l][r] = 1e18; for (int m = l; m < r; m++) { dp[l][r] = min(dp[l][r], dp[l][m] + dp[m + 1][r] + Sum(l, r)); } } } } cout << dp[0][n - 1] << endl; }
#include "bits/stdc++.h" #define all(x) (x).begin(), (x).end() using namespace std; const int MSIZE = 404; long long dp[MSIZE][MSIZE]; long long arr[MSIZE]; long long pref[MSIZE]; long long Sum(int l, int r) { l = max(l, 0); return pref[r] - (l ? pref[l - 1] : 0LL); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); memset(dp, 0, sizeof dp); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } memcpy(pref, arr, sizeof arr); for (int i = 1; i < n; i++) { pref[i] += pref[i - 1]; } for (int len = 1; len <= n; len++) { for (int l = 0; l + len <= n; l++) { int r = l + len - 1; if (len == 1) { dp[l][r] = 0; } else { dp[l][r] = 1e18; for (int m = l; m < r; m++) { dp[l][r] = min(dp[l][r], dp[l][m] + dp[m + 1][r] + Sum(l, r)); } } } } cout << dp[0][n - 1] << endl; }
replace
32
33
32
33
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long const ll inf = 1e16; int n, a[410]; ll f[410][410], s[410]; int main() { scanf("%d", &n); for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { f[i][j] = inf; } } for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); s[i] = s[i - 1] + a[i]; f[i][i] = 0; } for (int l = 1; l < n; l++) { for (int i = 1; i <= n; i++) { int j = i + l; for (int k = i; k < j; k++) { f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j]); } f[i][j] += s[j] - s[i - 1]; } } /* for (int i=1;i<=n;i++) { for (int j=i;j<=n;j++) { cerr<<f[i][j]<<" "; } cerr<<endl; } */ cout << f[1][n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long const ll inf = 1e16; int n, a[410]; ll f[410][410], s[410]; int main() { scanf("%d", &n); for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { f[i][j] = inf; } } for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); s[i] = s[i - 1] + a[i]; f[i][i] = 0; } for (int l = 1; l < n; l++) { for (int i = 1; i + l <= n; i++) { int j = i + l; for (int k = i; k < j; k++) { f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j]); } f[i][j] += s[j] - s[i - 1]; } } /* for (int i=1;i<=n;i++) { for (int j=i;j<=n;j++) { cerr<<f[i][j]<<" "; } cerr<<endl; } */ cout << f[1][n] << endl; return 0; }
replace
19
20
19
20
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long int; using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } ll mod = 1e9 + 7; ll dp[410][410] = {}; int main() { int n; cin >> n; ll a[n]; rep(i, n) cin >> a[i]; for (int i = 1; i < n; i++) { for (int j = 0; j < n - 1; j++) { ll tmp = 1000000000000000; ll sum = 0; for (int k = j; k < j + i; k++) { tmp = min(tmp, dp[j][k] + dp[k + 1][j + i]); sum += a[k]; } sum += a[j + i]; // a[j]~a[j+i]まで足す dp[j][j + i] = tmp + sum; } } cout << dp[0][n - 1] << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long int; using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } ll mod = 1e9 + 7; ll dp[410][410] = {}; int main() { int n; cin >> n; ll a[n]; rep(i, n) cin >> a[i]; for (int i = 1; i < n; i++) { for (int j = 0; j < n - i; j++) { ll tmp = 1000000000000000; ll sum = 0; for (int k = j; k < j + i; k++) { tmp = min(tmp, dp[j][k] + dp[k + 1][j + i]); sum += a[k]; } sum += a[j + i]; // a[j]~a[j+i]まで足す dp[j][j + i] = tmp + sum; } } cout << dp[0][n - 1] << endl; }
replace
29
30
29
30
0
p03173
C++
Runtime Error
#include <cstring> #include <iostream> #include <vector> #define INF (1LL << 60) using namespace std; long long int dp[3001][3001]; long long int sum[3001][3001]; long long int slimes(vector<int> &v, int i, int j) { if (i == j) return 0; if (dp[i][j] != -1) return dp[i][j]; // Look-Up long long int minCost = INF; for (int k = i; k < j; k++) { minCost = min(minCost, sum[i][j] + slimes(v, i, (i + k)) + slimes(v, (k + 1), j)); } return (dp[i][j] = minCost); } void preprocess(vector<int> &v, int n) { for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { sum[i][j] = v[j] + ((j == i) ? 0 : sum[i][j - 1]); } } } int main() { int n; cin >> n; memset(dp, -1, sizeof(dp)); vector<int> v(n + 1); for (int i = 1; i <= n; i++) cin >> v[i]; preprocess(v, n); cout << slimes(v, 1, n) << endl; return 0; }
#include <cstring> #include <iostream> #include <vector> #define INF (1LL << 60) using namespace std; long long int dp[3001][3001]; long long int sum[3001][3001]; long long int slimes(vector<int> &v, int i, int j) { if (i == j) return 0; if (dp[i][j] != -1) return dp[i][j]; // Look-Up long long int minCost = INF; for (int k = i; k < j; k++) { minCost = min(minCost, (sum[i][j] + slimes(v, i, k) + slimes(v, (k + 1), j))); } return (dp[i][j] = minCost); } void preprocess(vector<int> &v, int n) { for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { sum[i][j] = v[j] + ((j == i) ? 0 : sum[i][j - 1]); } } } int main() { int n; cin >> n; memset(dp, -1, sizeof(dp)); vector<int> v(n + 1); for (int i = 1; i <= n; i++) cin >> v[i]; preprocess(v, n); cout << slimes(v, 1, n) << endl; return 0; }
replace
17
18
17
18
-11
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) #define reprev(i, n) reprrev(i, 0, n) typedef long long ll; typedef unsigned long long ull; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } /* attention long longのシフト演算には気をつけよう タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも sizeは(int)とキャストしよう ごちゃごちゃ場合分けを考える前に全探索は考えましたか? */ const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } ll power(ll x, ll n) { if (n == 0) return 1; ll res = power(x, n / 2); if (n % 2 == 0) return res * res; else return res * res * x; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; /* <--------------------------------------------> */ typedef tuple<ll, ll, ll> T; // get<K>(tuple型の変数) const ll inf = 1LL << 60; int n; vector<ll> a(500, 0), cum(500, 0); vector<vector<ll>> memo(500, vector<ll>(500, -1)); ll rec(int l, int r) { if (memo[l][r] != -1) return memo[l][r]; if (l == r) return memo[l][r] = 0; ll res = inf; repr(i, l, r) { chmin(res, cum[r] - cum[l - 1] + rec(l, i) + rec(i + 1, r)); } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; repr(i, 1, n + 1) cin >> a[i]; repr(i, 1, n + 1) cum[i] = a[i] + cum[i - 1]; cout << rec(1, n) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) #define reprev(i, n) reprrev(i, 0, n) typedef long long ll; typedef unsigned long long ull; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } /* attention long longのシフト演算には気をつけよう タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも sizeは(int)とキャストしよう ごちゃごちゃ場合分けを考える前に全探索は考えましたか? */ const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } ll power(ll x, ll n) { if (n == 0) return 1; ll res = power(x, n / 2); if (n % 2 == 0) return res * res; else return res * res * x; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; /* <--------------------------------------------> */ typedef tuple<ll, ll, ll> T; // get<K>(tuple型の変数) const ll inf = 1LL << 60; int n; vector<ll> a(500, 0), cum(500, 0); vector<vector<ll>> memo(500, vector<ll>(500, -1)); ll rec(int l, int r) { if (memo[l][r] != -1) return memo[l][r]; if (l == r) return memo[l][r] = 0; ll res = inf; repr(i, l, r) { chmin(res, cum[r] - cum[l - 1] + rec(l, i) + rec(i + 1, r)); } return memo[l][r] = res; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; repr(i, 1, n + 1) cin >> a[i]; repr(i, 1, n + 1) cum[i] = a[i] + cum[i - 1]; cout << rec(1, n) << endl; return 0; }
replace
84
85
84
85
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MXN = 4e0 + 20; long long n, a[MXN], ps[MXN], dp[MXN][MXN]; void npt() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; ps[i] = ps[i - 1] + a[i]; } for (int i = 0; i < MXN; i++) for (int j = 0; j < MXN; j++) dp[i][j] = 1e15 + 20; } void prnt() { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) cerr << dp[i][j] << ' '; cerr << endl; } cerr << "-----------------------\n"; } void slv() { for (int i = 0; i <= n; ++i) { for (int l = 0; l <= n - i; ++l) { int r = l + i; if (r - l < 2) { dp[l][r] = 0; continue; } long long tmp = ps[r] - ps[l]; for (int mid = l; mid <= r; ++mid) dp[l][r] = min(dp[l][r], dp[l][mid] + dp[mid][r] + tmp); } } cout << dp[0][n] << endl; } int main() { npt(); slv(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MXN = 4e2 + 20; long long n, a[MXN], ps[MXN], dp[MXN][MXN]; void npt() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; ps[i] = ps[i - 1] + a[i]; } for (int i = 0; i < MXN; i++) for (int j = 0; j < MXN; j++) dp[i][j] = 1e15 + 20; } void prnt() { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) cerr << dp[i][j] << ' '; cerr << endl; } cerr << "-----------------------\n"; } void slv() { for (int i = 0; i <= n; ++i) { for (int l = 0; l <= n - i; ++l) { int r = l + i; if (r - l < 2) { dp[l][r] = 0; continue; } long long tmp = ps[r] - ps[l]; for (int mid = l; mid <= r; ++mid) dp[l][r] = min(dp[l][r], dp[l][mid] + dp[mid][r] + tmp); } } cout << dp[0][n] << endl; } int main() { npt(); slv(); return 0; }
replace
3
4
3
4
0
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; ll dp[405][405]; int l[405]; int n; ll solve(int lhs, int rhs) { if (lhs == rhs) return 0; ll total = 0; for (int i = lhs; i <= rhs; i++) { total += l[i]; } dp[lhs][rhs] = 1e18; for (int i = lhs; i < rhs; i++) { dp[lhs][rhs] = min(dp[lhs][rhs], solve(lhs, i) + solve(i + 1, rhs)); } dp[lhs][rhs] += total; return dp[lhs][rhs]; } void solve() { cin >> n; for (int i = 0; i < n; i++) { cin >> l[i]; } cout << solve(0, n - 1) << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; ll dp[405][405]; int l[405]; int n; ll solve(int lhs, int rhs) { if (lhs == rhs) return 0; if (dp[lhs][rhs]) return dp[lhs][rhs]; ll total = 0; for (int i = lhs; i <= rhs; i++) { total += l[i]; } dp[lhs][rhs] = 1e18; for (int i = lhs; i < rhs; i++) { dp[lhs][rhs] = min(dp[lhs][rhs], solve(lhs, i) + solve(i + 1, rhs)); } dp[lhs][rhs] += total; return dp[lhs][rhs]; } void solve() { cin >> n; for (int i = 0; i < n; i++) { cin >> l[i]; } cout << solve(0, n - 1) << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); }
insert
14
14
14
16
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mxN = 1e9; const ll mod = 1e9 + 7; ll pow(ll a, ll b) { ll r = 1; while (b) { if (b % 2 != 0) r = (r * a) % mod; a = (a * a) % mod; b /= 2; } return r; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<ll> v(n); for (int i = 0; i < n; i++) cin >> v[i]; vector<ll> pref(n + 1); pref[0] = 0; for (int i = 1; i <= n; i++) { pref[i] = pref[i - 1] + v[i - 1]; } ll dp[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j + i < n; j++) { if (i + j == j) dp[j][j + i] = 0; else { dp[j][i + j] = 1e18L + 5; for (int k = j; k <= j + i; k++) { dp[j][i + j] = min(dp[j][i + j], dp[j][k] + dp[k + 1][i + j] + (pref[i + j + 1] - pref[j])); } } } } cout << dp[0][n - 1]; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mxN = 1e9; const ll mod = 1e9 + 7; ll pow(ll a, ll b) { ll r = 1; while (b) { if (b % 2 != 0) r = (r * a) % mod; a = (a * a) % mod; b /= 2; } return r; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<ll> v(n); for (int i = 0; i < n; i++) cin >> v[i]; vector<ll> pref(n + 1); pref[0] = 0; for (int i = 1; i <= n; i++) { pref[i] = pref[i - 1] + v[i - 1]; } ll dp[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j + i < n; j++) { if (i + j == j) dp[j][j + i] = 0; else { dp[j][i + j] = 1e18L + 5; for (int k = j; k <= j + i - 1; k++) { dp[j][i + j] = min(dp[j][i + j], dp[j][k] + dp[k + 1][i + j] + (pref[i + j + 1] - pref[j])); } } } } cout << dp[0][n - 1]; }
replace
45
46
45
46
0
p03173
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; const ll INF = 1145141919810893; int N; ll dp[400][400]; ll cost[400]; ll ruisekiwa[401]; // idx i ~ idx j (i < j)は [i, j) int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> cost[i]; ruisekiwa[i + 1] = cost[i] + ruisekiwa[i]; } for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) dp[i][j] = INF; for (int i = 0; i < N; i++) dp[i][i] = 0; for (int i = 1; i < N; i++) { for (int j = 0; j + i < N; j++) { ll ans = INF; if (i == 1) { dp[j][j + i] = ruisekiwa[j + i + 1] - ruisekiwa[j]; } else { for (int k = j; k <= j + i; k++) { int ll = j, lr = k, rl = k + 1, rr = j + i; ans = min(ans, dp[ll][lr] + dp[rl][rr]); } ans += ruisekiwa[j + i + 1] - ruisekiwa[j]; dp[j][j + i] = ans; } } } cout << dp[0][N - 1] << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; const ll INF = 1145141919810893; int N; ll dp[400][400]; ll cost[400]; ll ruisekiwa[401]; // idx i ~ idx j (i < j)は [i, j) int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> cost[i]; ruisekiwa[i + 1] = cost[i] + ruisekiwa[i]; } for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) dp[i][j] = INF; for (int i = 0; i < N; i++) dp[i][i] = 0; for (int i = 1; i < N; i++) { for (int j = 0; j + i < N; j++) { ll ans = INF; if (i == 1) { dp[j][j + i] = ruisekiwa[j + i + 1] - ruisekiwa[j]; } else { for (int k = j; k <= j + i - 1; k++) { int ll = j, lr = k, rl = k + 1, rr = j + i; ans = min(ans, dp[ll][lr] + dp[rl][rr]); } ans += ruisekiwa[j + i + 1] - ruisekiwa[j]; dp[j][j + i] = ans; } } } cout << dp[0][N - 1] << endl; return 0; }
replace
32
33
32
33
0
p03173
C++
Runtime Error
#include <iostream> #define nmax 405 using namespace std; int n, a[nmax]; long long s[nmax][nmax]; long long dp[nmax][nmax]; int main() { int i, j, k, l; cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 1; i <= n; i++) for (j = i; j <= n; j++) s[i][j] = s[i][j - 1] + a[j]; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) dp[i][j] = 1LL << 60; for (i = 1; i <= n; i++) dp[i][i] = 0; for (l = 2; l <= n; l++) for (i = 1; i <= n; i++) { j = i + l - 1; for (k = i; k < j; k++) { long long val = dp[i][k] + s[i][k] + dp[k + 1][j] + s[k + 1][j]; if (dp[i][j] > val) dp[i][j] = val; } } cout << dp[1][n] << '\n'; return 0; }
#include <iostream> #define nmax 405 using namespace std; int n, a[nmax]; long long s[nmax][nmax]; long long dp[nmax][nmax]; int main() { int i, j, k, l; cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 1; i <= n; i++) for (j = i; j <= n; j++) s[i][j] = s[i][j - 1] + a[j]; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) dp[i][j] = 1LL << 60; for (i = 1; i <= n; i++) dp[i][i] = 0; for (l = 2; l <= n; l++) for (i = 1; i + l - 1 <= n; i++) { j = i + l - 1; for (k = i; k < j; k++) { long long val = dp[i][k] + s[i][k] + dp[k + 1][j] + s[k + 1][j]; if (dp[i][j] > val) dp[i][j] = val; } } cout << dp[1][n] << '\n'; return 0; }
replace
24
25
24
25
0
p03173
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define INF (1 << 29) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define uniq(v) v.erase(unique(all(v)), v.end()) int n; int a[400]; long long dp[400][400]; long long sum[401]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; rep(i, n) cin >> a[i]; rep(i, n) sum[i + 1] = sum[i] + a[i]; for (int len = 2; len <= n; len++) { rep(i, n) { auto ret = 1LL << 60; for (int j = 1; j < len; j++) { ret = min(ret, dp[i][i + j - 1] + dp[i + j][i + len - 1]); } dp[i][i + len - 1] = ret + sum[i + len] - sum[i]; } } cout << dp[0][n - 1] << endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define INF (1 << 29) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define uniq(v) v.erase(unique(all(v)), v.end()) int n; int a[400]; long long dp[400][400]; long long sum[401]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; rep(i, n) cin >> a[i]; rep(i, n) sum[i + 1] = sum[i] + a[i]; for (int len = 2; len <= n; len++) { rep(i, n - len + 1) { auto ret = 1LL << 60; for (int j = 1; j < len; j++) { ret = min(ret, dp[i][i + j - 1] + dp[i + j][i + len - 1]); } dp[i][i + len - 1] = ret + sum[i + len] - sum[i]; } } cout << dp[0][n - 1] << endl; return 0; }
replace
44
45
44
45
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define ll long long int #define P pair<ll, ll> #define vii vector<P> #define vvi vector<vector<ll>> #define P_B push_back #define MOD 1000000007 #define pi 3.141592653589793238 int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // ll T; cin>>T; while(T--) { ll n; cin >> n; vector<ll> v(n); for (int i = 0; i < n; i++) cin >> v[i]; ll DP[n][n]; for (ll l = n - 1; l >= 0; l--) { for (int r = l; r < n; r++) { if (r == l) { DP[l][r] = 0; continue; } ll sum = 0; for (int i = l; i <= r; i++) sum += v[i]; DP[l][r] = 1e18L; for (int p = l; p <= r; p++) { DP[l][r] = min(DP[l][r], DP[l][p] + DP[p + 1][r] + sum); } } } cout << DP[0][n - 1]; } return 0; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define ll long long int #define P pair<ll, ll> #define vii vector<P> #define vvi vector<vector<ll>> #define P_B push_back #define MOD 1000000007 #define pi 3.141592653589793238 int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // ll T; cin>>T; while(T--) { ll n; cin >> n; vector<ll> v(n); for (int i = 0; i < n; i++) cin >> v[i]; ll DP[n][n]; for (ll l = n - 1; l >= 0; l--) { for (int r = l; r < n; r++) { if (r == l) { DP[l][r] = 0; continue; } ll sum = 0; for (int i = l; i <= r; i++) sum += v[i]; DP[l][r] = 1e18L; for (int p = l; p < r; p++) { DP[l][r] = min(DP[l][r], DP[l][p] + DP[p + 1][r] + sum); } } } cout << DP[0][n - 1]; } return 0; }
replace
45
46
45
46
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 401; int dp[maxn][maxn]; int pref[maxn]; signed main() { int n; cin >> n; vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; pref[0] = 0; for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i]; for (int i = 0; i < maxn; i++) { for (int j = 0; j < maxn; j++) dp[i][j] = LLONG_MAX; } for (int i = 0; i < maxn; i++) dp[i][i] = 0; for (int i = 1; i <= n - 1; i++) { for (int l = 1; l <= n; l++) { int r = l + i; for (int m = l; m < r; m++) { dp[l][r] = min(dp[l][r], dp[l][m] + dp[m + 1][r] + pref[r] - pref[l - 1]); } } } cout << dp[1][n]; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 401; int dp[maxn][maxn]; int pref[maxn]; signed main() { int n; cin >> n; vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; pref[0] = 0; for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i]; for (int i = 0; i < maxn; i++) { for (int j = 0; j < maxn; j++) dp[i][j] = LLONG_MAX; } for (int i = 0; i < maxn; i++) dp[i][i] = 0; for (int i = 1; i <= n - 1; i++) { for (int l = 1; l <= n; l++) { if (l + i >= maxn) continue; int r = l + i; for (int m = l; m < r; m++) { dp[l][r] = min(dp[l][r], dp[l][m] + dp[m + 1][r] + pref[r] - pref[l - 1]); } } } cout << dp[1][n]; return 0; }
insert
23
23
23
25
0
p03173
C++
Time Limit Exceeded
#include <algorithm> #include <iomanip> #include <iostream> #include <string.h> #include <vector> using namespace std; #define mod (long)(1e9 + 7) #define all(x) (x).begin(), (x).end() #define bitcount(n) __builtin_popcountl(long(n)) #define fcout cout << fixed << setprecision(15) #define highest(x) (63 - __builtin_clzl(x)) #define long long long template <class T> inline void YES(T condition) { if (condition) cout << "YES" << endl; else cout << "NO" << endl; } template <class T> inline void Yes(T condition) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; } template <class T> inline void POSS(T condition) { if (condition) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; } template <class T> inline void Poss(T condition) { if (condition) cout << "Possible" << endl; else cout << "Impossible" << endl; } template <class T> inline void First(T condition) { if (condition) cout << "First" << endl; else cout << "Second" << endl; } template <class T = string, class U = char> int character_count(T text, U character) { int ans = 0; for (U i : text) { ans += (i == character); } return ans; } long power(long base, long exponent, long module) { if (exponent % 2) { return power(base, exponent - 1, module) * base % module; } else if (exponent) { long root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; } else { return 1; } } struct position { int y, x; }; position move_pattern[4] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // double euclidean(position first, position second){ return // sqrt((second.x - first.x) * (second.x - first.x) + (second.y - // first.y) * (second.y - first.y)); } template <class T, class U> string to_string(pair<T, U> x) { return to_string(x.first) + "," + to_string(x.second); } template <class itr> void array_output(itr start, itr goal) { string ans; for (auto i = start; i != goal; i++) { ans += to_string(*i) + " "; } ans.pop_back(); cout << ans << endl; } template <class itr> void cins(itr start, itr goal) { for (auto i = start; i != goal; i++) { cin >> (*i); } } template <class T> T gcd(T a, T b) { if (a && b) { return gcd(min(a, b), max(a, b) % min(a, b)); } else { return a; } } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } struct combination { vector<long> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1) { fact[0] = 1; for (int i = 1; i <= sz; i++) { fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for (int i = sz - 1; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } long C(int p, int q) const { if (q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } }; int N; vector<long> a_sum; long dp[404][404]; long decompose(int l, int r) { if (l + 1 == r) { return 0; } long ans = 1e18; for (int i = l + 1; i < r; i++) { ans = min(ans, decompose(l, i) + decompose(i, r)); } return dp[l][r] = ans + a_sum[r] - a_sum[l]; } int main() { cin >> N; a_sum.resize(N + 1, 0); for (int i = 0; i < N; i++) { int a; cin >> a; a_sum[i + 1] = a_sum[i] + a; } memset(dp, -1, sizeof(dp)); cout << decompose(0, N) << endl; }
#include <algorithm> #include <iomanip> #include <iostream> #include <string.h> #include <vector> using namespace std; #define mod (long)(1e9 + 7) #define all(x) (x).begin(), (x).end() #define bitcount(n) __builtin_popcountl(long(n)) #define fcout cout << fixed << setprecision(15) #define highest(x) (63 - __builtin_clzl(x)) #define long long long template <class T> inline void YES(T condition) { if (condition) cout << "YES" << endl; else cout << "NO" << endl; } template <class T> inline void Yes(T condition) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; } template <class T> inline void POSS(T condition) { if (condition) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; } template <class T> inline void Poss(T condition) { if (condition) cout << "Possible" << endl; else cout << "Impossible" << endl; } template <class T> inline void First(T condition) { if (condition) cout << "First" << endl; else cout << "Second" << endl; } template <class T = string, class U = char> int character_count(T text, U character) { int ans = 0; for (U i : text) { ans += (i == character); } return ans; } long power(long base, long exponent, long module) { if (exponent % 2) { return power(base, exponent - 1, module) * base % module; } else if (exponent) { long root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; } else { return 1; } } struct position { int y, x; }; position move_pattern[4] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // double euclidean(position first, position second){ return // sqrt((second.x - first.x) * (second.x - first.x) + (second.y - // first.y) * (second.y - first.y)); } template <class T, class U> string to_string(pair<T, U> x) { return to_string(x.first) + "," + to_string(x.second); } template <class itr> void array_output(itr start, itr goal) { string ans; for (auto i = start; i != goal; i++) { ans += to_string(*i) + " "; } ans.pop_back(); cout << ans << endl; } template <class itr> void cins(itr start, itr goal) { for (auto i = start; i != goal; i++) { cin >> (*i); } } template <class T> T gcd(T a, T b) { if (a && b) { return gcd(min(a, b), max(a, b) % min(a, b)); } else { return a; } } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } struct combination { vector<long> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1) { fact[0] = 1; for (int i = 1; i <= sz; i++) { fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for (int i = sz - 1; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } long C(int p, int q) const { if (q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } }; int N; vector<long> a_sum; long dp[404][404]; long decompose(int l, int r) { if (l + 1 == r) { return 0; } else if (dp[l][r] != -1) { return dp[l][r]; } long ans = 1e18; for (int i = l + 1; i < r; i++) { ans = min(ans, decompose(l, i) + decompose(i, r)); } return dp[l][r] = ans + a_sum[r] - a_sum[l]; } int main() { cin >> N; a_sum.resize(N + 1, 0); for (int i = 0; i < N; i++) { int a; cin >> a; a_sum[i + 1] = a_sum[i] + a; } memset(dp, -1, sizeof(dp)); cout << decompose(0, N) << endl; }
insert
121
121
121
123
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long const int MAXN = 4e2 + 5; const int INF = 1e16 + 7; int dp[MAXN][MAXN]; int sum[MAXN]; int arr[MAXN]; int32_t main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> arr[i]; sum[i] = sum[i - 1] + arr[i]; } for (int l = 2; l <= n; l++) { for (int i = 1; i <= n; i++) { dp[i][l] = INF; int j = i + l - 1; for (int idx = i; idx < j; idx++) { dp[i][l] = min(dp[i][l], dp[i][idx - i + 1] + dp[idx + 1][j - idx] + (sum[j] - sum[i - 1])); } } } cout << dp[1][n] << '\n'; }
#include <bits/stdc++.h> using namespace std; #define int long long const int MAXN = 4e2 + 5; const int INF = 1e16 + 7; int dp[MAXN][MAXN]; int sum[MAXN]; int arr[MAXN]; int32_t main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> arr[i]; sum[i] = sum[i - 1] + arr[i]; } for (int l = 2; l <= n; l++) { for (int i = 1; i <= n; i++) { dp[i][l] = INF; if (i + l - 1 > n) continue; int j = i + l - 1; for (int idx = i; idx < j; idx++) { dp[i][l] = min(dp[i][l], dp[i][idx - i + 1] + dp[idx + 1][j - idx] + (sum[j] - sum[i - 1])); } } } cout << dp[1][n] << '\n'; }
insert
18
18
18
20
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long f[102][102]; long long a[102]; long long sum[102]; int main() { int n, i, j, k; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%lld", &a[i]); sum[i] = sum[i - 1] + a[i]; } for (j = 1; j <= n - 1; j++) { for (i = 1; i + j <= n; i++) { f[i][i + j] = std::numeric_limits<long long>::max(); for (k = i; k < i + j; k++) { f[i][i + j] = min(f[i][i + j], f[i][k] + f[k + 1][i + j] + sum[i + j] - sum[i - 1]); } } } printf("%lld", f[1][n]); return 0; }
#include <bits/stdc++.h> using namespace std; long long f[402][402]; long long a[402]; long long sum[402]; int main() { int n, i, j, k; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%lld", &a[i]); sum[i] = sum[i - 1] + a[i]; } for (j = 1; j <= n - 1; j++) { for (i = 1; i + j <= n; i++) { f[i][i + j] = std::numeric_limits<long long>::max(); for (k = i; k < i + j; k++) { f[i][i + j] = min(f[i][i + j], f[i][k] + f[k + 1][i + j] + sum[i + j] - sum[i - 1]); } } } printf("%lld", f[1][n]); return 0; }
replace
2
5
2
5
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/hash_map> #include <ext/numeric> using namespace std; using namespace __gnu_cxx; #define REP(i, n) for ((i) = 0; (i) < (n); (i)++) #define rep(i, x, n) for ((i) = (x); (i) < (n); (i)++) #define REV(i, n) for ((i) = (n); (i) >= 0; (i)--) #define FORIT(it, x) for ((it) = (x).begin(); (it) != (x).end(); (it)++) #define foreach(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define rforeach(it, c) \ for (__typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it) #define foreach2d(i, j, v) \ foreach (i, v) \ foreach (j, *i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define SZ(x) ((int)(x).size()) #define MMS(x, n) memset(x, n, sizeof(x)) #define mms(x, n, s) memset(x, n, sizeof(x) * s) #define pb push_back #define mp make_pair #define NX next_permutation #define UN(x) sort(all(x)), x.erase(unique(all(x)), x.end()) #define CV(x, n) count(all(x), (n)) #define FIND(x, n) find(all(x), (n)) - (x).begin() #define ACC(x) accumulate(all(x), 0) #define PPC(x) __builtin_popcountll(x) #define LZ(x) __builtin_clz(x) #define TZ(x) __builtin_ctz(x) #define mxe(x) *max_element(all(x)) #define mne(x) *min_element(all(x)) #define low(x, i) lower_bound(all(x), i) #define upp(x, i) upper_bound(all(x), i) #define NXPOW2(x) (1ll << ((int)log2(x) + 1)) #define PR(x) cout << #x << " = " << (x) << endl; typedef unsigned long long ull; typedef long long ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef pair<int, int> pii; const int OO = (int)2e9; const double eps = 1e-9; const int N = 402; int n; int a[N]; ll sum[N]; ll dp[N][N]; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE // freopen("in.txt", "rt", stdin); // freopen("out.txt", "wt", stdout); #endif cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; sum[i + 1] = sum[i] + a[i]; } MMS(dp, '?'); for (int i = 0; i < n; i++) dp[1][i] = 0; for (int len = 2; len <= n; len++) { for (int st = 0; st < n; st++) { for (int i = 1; st + i < n && i < len; i++) { dp[len][st] = min(dp[len][st], dp[i][st] + dp[len - i][st + i] + sum[st + len] - sum[st]); } } } cout << dp[n][0] << endl; return 0; }
#include <bits/stdc++.h> #include <ext/hash_map> #include <ext/numeric> using namespace std; using namespace __gnu_cxx; #define REP(i, n) for ((i) = 0; (i) < (n); (i)++) #define rep(i, x, n) for ((i) = (x); (i) < (n); (i)++) #define REV(i, n) for ((i) = (n); (i) >= 0; (i)--) #define FORIT(it, x) for ((it) = (x).begin(); (it) != (x).end(); (it)++) #define foreach(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define rforeach(it, c) \ for (__typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it) #define foreach2d(i, j, v) \ foreach (i, v) \ foreach (j, *i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define SZ(x) ((int)(x).size()) #define MMS(x, n) memset(x, n, sizeof(x)) #define mms(x, n, s) memset(x, n, sizeof(x) * s) #define pb push_back #define mp make_pair #define NX next_permutation #define UN(x) sort(all(x)), x.erase(unique(all(x)), x.end()) #define CV(x, n) count(all(x), (n)) #define FIND(x, n) find(all(x), (n)) - (x).begin() #define ACC(x) accumulate(all(x), 0) #define PPC(x) __builtin_popcountll(x) #define LZ(x) __builtin_clz(x) #define TZ(x) __builtin_ctz(x) #define mxe(x) *max_element(all(x)) #define mne(x) *min_element(all(x)) #define low(x, i) lower_bound(all(x), i) #define upp(x, i) upper_bound(all(x), i) #define NXPOW2(x) (1ll << ((int)log2(x) + 1)) #define PR(x) cout << #x << " = " << (x) << endl; typedef unsigned long long ull; typedef long long ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef pair<int, int> pii; const int OO = (int)2e9; const double eps = 1e-9; const int N = 402; int n; int a[N]; ll sum[N]; ll dp[N][N]; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE // freopen("in.txt", "rt", stdin); // freopen("out.txt", "wt", stdout); #endif cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; sum[i + 1] = sum[i] + a[i]; } MMS(dp, '?'); for (int i = 0; i < n; i++) dp[1][i] = 0; for (int len = 2; len <= n; len++) { for (int st = 0; st < n && st + len <= n; st++) { for (int i = 1; st + i < n && i < len; i++) { dp[len][st] = min(dp[len][st], dp[i][st] + dp[len - i][st + i] + sum[st + len] - sum[st]); } } } cout << dp[n][0] << endl; return 0; }
replace
73
74
73
74
0
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll inf = (ll)1 << 60; ll n; vector<ll> v; vector<vector<ll>> memo; vector<vector<ll>> size; // Find the cheapest cost to combine the range [lo,hi] ll solve(ll lo, ll hi) { if (memo[lo][hi] != inf) return memo[lo][hi]; if (lo == hi) { memo[lo][hi] = 0; return 0; } ll best = inf; for (ll i = lo; i < hi; i++) { ll here = solve(lo, i) + solve(i + 1, hi); best = min(best, here); } best += size[lo][hi]; memo[lo][hi] = best; return best; } void sizes(ll lo, ll hi) { if (lo == hi) { size[lo][hi] = v[lo]; return; } ll best = inf; for (int i = lo; i < hi; i++) { sizes(lo, i); sizes(i + 1, hi); ll here = size[lo][i] + size[i + 1][hi]; best = min(best, here); } size[lo][hi] = best; } int main() { ll n; cin >> n; v.resize(n); for (auto &i : v) { cin >> i; } memo.resize(n + 1, vector<ll>(n + 1, inf)); size.resize(n + 1, vector<ll>(n + 1, inf)); sizes(0, n - 1); cout << solve(0, n - 1) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll inf = (ll)1 << 60; ll n; vector<ll> v; vector<vector<ll>> memo; vector<vector<ll>> size; // Find the cheapest cost to combine the range [lo,hi] ll solve(ll lo, ll hi) { if (memo[lo][hi] != inf) return memo[lo][hi]; if (lo == hi) { memo[lo][hi] = 0; return 0; } ll best = inf; for (ll i = lo; i < hi; i++) { ll here = solve(lo, i) + solve(i + 1, hi); best = min(best, here); } best += size[lo][hi]; memo[lo][hi] = best; return best; } void sizes(ll lo, ll hi) { if (size[lo][hi] != inf) return; if (lo == hi) { size[lo][hi] = v[lo]; return; } ll best = inf; for (int i = lo; i < hi; i++) { sizes(lo, i); sizes(i + 1, hi); ll here = size[lo][i] + size[i + 1][hi]; best = min(best, here); } size[lo][hi] = best; } int main() { ll n; cin >> n; v.resize(n); for (auto &i : v) { cin >> i; } memo.resize(n + 1, vector<ll>(n + 1, inf)); size.resize(n + 1, vector<ll>(n + 1, inf)); sizes(0, n - 1); cout << solve(0, n - 1) << endl; }
insert
34
34
34
37
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; /* typedef */ typedef long long ll; typedef pair<int, int> pii; /* constant */ const int MAX = 305; const int NIL = -1; const ll LINF = 1LL << 50; const int mod = 1e+9 + 7; /* global variables */ /* funciton */ /* main */ int main() { int N; cin >> N; vector<ll> a(N); for (int i = 0; i < N; i++) cin >> a[i]; // sum(a[i], ... , a[j]) = cum[j+1] - cum[i]; vector<ll> cum(N + 2); for (int i = 0; i <= N; i++) cum[i + 1] = cum[i] + a[i]; // dp[x][y] = a[x] ~ a[y-1]までを合体させる時の最小コスト vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, LINF)); for (int i = 0; i <= N; i++) { dp[i][i + 1] = 0; } for (int len = 1; len <= N; len++) { for (int x = 0; x <= N - len; x++) { int y = x + len; for (int z = x + 1; z < y; z++) { ll cost = dp[x][z] + dp[z][y] + (cum[z] - cum[x]) + (cum[y] - cum[z]); dp[x][y] = min(dp[x][y], cost); } } } cout << dp[0][N] << '\n'; }
#include <bits/stdc++.h> using namespace std; /* typedef */ typedef long long ll; typedef pair<int, int> pii; /* constant */ const int MAX = 305; const int NIL = -1; const ll LINF = 1LL << 50; const int mod = 1e+9 + 7; /* global variables */ /* funciton */ /* main */ int main() { int N; cin >> N; vector<ll> a(N); for (int i = 0; i < N; i++) cin >> a[i]; // sum(a[i], ... , a[j]) = cum[j+1] - cum[i]; vector<ll> cum(N + 2); for (int i = 0; i <= N; i++) cum[i + 1] = cum[i] + a[i]; // dp[x][y] = a[x] ~ a[y-1]までを合体させる時の最小コスト vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, LINF)); for (int i = 0; i < N; i++) { dp[i][i + 1] = 0; } for (int len = 1; len <= N; len++) { for (int x = 0; x <= N - len; x++) { int y = x + len; for (int z = x + 1; z < y; z++) { ll cost = dp[x][z] + dp[z][y] + (cum[z] - cum[x]) + (cum[y] - cum[z]); dp[x][y] = min(dp[x][y], cost); } } } cout << dp[0][N] << '\n'; }
replace
27
28
27
28
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ffor(n) for (int i = 0; i < n; i++) #define fffor(n) for (int j = 0; j < n; j++) int INF = 1e9 + 7; long long INF2 = 1e16; long long f(int l, int r, vector<long long> &v, vector<vector<long long>> &dp, vector<vector<long long>> &range_sum) { if (l == r) { range_sum[l][r] = v[l]; dp[l][r] = 0; return dp[l][r]; } if (l + 1 == r) { range_sum[l][r] = v[l] + v[r]; dp[l][r] = range_sum[l][r]; return dp[l][r]; } if (l > r) { return 0; } long long ans = INF2; for (int i = l; i < r; i++) { ans = min(ans, f(l, i, v, dp, range_sum) + f(i + 1, r, v, dp, range_sum) + range_sum[l][i] + range_sum[i + 1][r]); } dp[l][r] = ans; range_sum[l][r] = range_sum[l][l + 1] + range_sum[l + 2][r]; return ans; } int main(void) { int n; cin >> n; vector<long long> v(n); ffor(n) cin >> v[i]; vector<vector<long long>> dp(n, vector<long long>(n, -1)); vector<vector<long long>> range_sum(n, vector<long long>(n, -1)); cout << f(0, n - 1, v, dp, range_sum) << '\n'; }
#include <bits/stdc++.h> using namespace std; #define ffor(n) for (int i = 0; i < n; i++) #define fffor(n) for (int j = 0; j < n; j++) int INF = 1e9 + 7; long long INF2 = 1e16; long long f(int l, int r, vector<long long> &v, vector<vector<long long>> &dp, vector<vector<long long>> &range_sum) { if (dp[l][r] != -1) return dp[l][r]; if (l == r) { range_sum[l][r] = v[l]; dp[l][r] = 0; return dp[l][r]; } if (l + 1 == r) { range_sum[l][r] = v[l] + v[r]; dp[l][r] = range_sum[l][r]; return dp[l][r]; } if (l > r) { return 0; } long long ans = INF2; for (int i = l; i < r; i++) { ans = min(ans, f(l, i, v, dp, range_sum) + f(i + 1, r, v, dp, range_sum) + range_sum[l][i] + range_sum[i + 1][r]); } dp[l][r] = ans; range_sum[l][r] = range_sum[l][l + 1] + range_sum[l + 2][r]; return ans; } int main(void) { int n; cin >> n; vector<long long> v(n); ffor(n) cin >> v[i]; vector<vector<long long>> dp(n, vector<long long>(n, -1)); vector<vector<long long>> range_sum(n, vector<long long>(n, -1)); cout << f(0, n - 1, v, dp, range_sum) << '\n'; }
insert
9
9
9
11
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define ll long long #define si short int #define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define pill pair<ll, ll> #define f first #define s second using namespace std; const ll N = 181; const ll big = 1e18; const ll block = 100; const ll mod = 1e6; ll n; ll a[N]; ll dp[N][N]; bool was[N][N]; ll rec(ll l, ll r) { if (was[l][r]) return dp[l][r]; if (l == r) return a[l]; ll ans = big; ll ls = a[r]; for (int j = l; j < r; j++) { ls += a[j]; ll x = rec(l, j) + rec(j + 1, r); ans = min(x, ans); } was[l][r] = 1; dp[l][r] = ans + ls; return dp[l][r]; } int main() { speed; cin >> n; ll ls = 0; for (int i = 0; i < n; i++) cin >> a[i], ls += a[i]; cout << rec(0, n - 1) - ls; } /* Code written by Codeforces : I_Love_Trott mail : [email protected] */
#include <bits/stdc++.h> #define pb push_back #define ll long long #define si short int #define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define pill pair<ll, ll> #define f first #define s second using namespace std; const ll N = 481; const ll big = 1e18; const ll block = 100; const ll mod = 1e6; ll n; ll a[N]; ll dp[N][N]; bool was[N][N]; ll rec(ll l, ll r) { if (was[l][r]) return dp[l][r]; if (l == r) return a[l]; ll ans = big; ll ls = a[r]; for (int j = l; j < r; j++) { ls += a[j]; ll x = rec(l, j) + rec(j + 1, r); ans = min(x, ans); } was[l][r] = 1; dp[l][r] = ans + ls; return dp[l][r]; } int main() { speed; cin >> n; ll ls = 0; for (int i = 0; i < n; i++) cin >> a[i], ls += a[i]; cout << rec(0, n - 1) - ls; } /* Code written by Codeforces : I_Love_Trott mail : [email protected] */
replace
11
12
11
12
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (lli i = 0; i < (n); i++) #define rrep(i, n) for (lli i = (n)-1; i >= 0; i--) using namespace std; using lli = long long int; #define int long long int void YESNO(bool), YesNo(bool); template <class T1, class T2> bool chmin(T1 &l, const T2 &r); template <class T1, class T2> bool chmax(T1 &l, const T2 &r); constexpr int M = 405; lli dp[M][M] = {}; lli sum[M][M] = {}; void solve(long long N, std::vector<long long> a) { rep(i, M) rep(j, M) dp[i][j] = 1e18; rep(i, N) dp[i][i + 1] = 0; rep(i, N) for (int j = i + 1; j <= N; j++) { // sum of a[j, i) sum[i][j] = sum[i][j - 1] + a[j - 1]; } for (int r = 2; r <= N; r++) { rep(i, N) { // [i, i+r) for (int k = i + 1; k < i + r; k++) chmin(dp[i][i + r], dp[i][k] + dp[k][i + r] + sum[i][k] + sum[k][i + r]); } } cout << dp[0][N] << endl; } signed main() { long long N; scanf("%lld", &N); std::vector<long long> a(N); for (int i = 0; i < N; i++) { scanf("%lld", &a[i]); } solve(N, std::move(a)); return 0; } // -- lib void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; } void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; } template <class T1, class T2> bool chmin(T1 &l, const T2 &r) { return (l > r) ? (l = r, true) : false; } template <class T1, class T2> bool chmax(T1 &l, const T2 &r) { return (l < r) ? (l = r, true) : false; }
#include <bits/stdc++.h> #define rep(i, n) for (lli i = 0; i < (n); i++) #define rrep(i, n) for (lli i = (n)-1; i >= 0; i--) using namespace std; using lli = long long int; #define int long long int void YESNO(bool), YesNo(bool); template <class T1, class T2> bool chmin(T1 &l, const T2 &r); template <class T1, class T2> bool chmax(T1 &l, const T2 &r); constexpr int M = 405; lli dp[M][M] = {}; lli sum[M][M] = {}; void solve(long long N, std::vector<long long> a) { rep(i, M) rep(j, M) dp[i][j] = 1e18; rep(i, N) dp[i][i + 1] = 0; rep(i, N) for (int j = i + 1; j <= N; j++) { // sum of a[j, i) sum[i][j] = sum[i][j - 1] + a[j - 1]; } for (int r = 2; r <= N; r++) { rep(i, N) { // [i, i+r) for (int k = i + 1; k < i + r && i + r <= N; k++) chmin(dp[i][i + r], dp[i][k] + dp[k][i + r] + sum[i][k] + sum[k][i + r]); } } cout << dp[0][N] << endl; } signed main() { long long N; scanf("%lld", &N); std::vector<long long> a(N); for (int i = 0; i < N; i++) { scanf("%lld", &a[i]); } solve(N, std::move(a)); return 0; } // -- lib void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; } void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; } template <class T1, class T2> bool chmin(T1 &l, const T2 &r) { return (l > r) ? (l = r, true) : false; } template <class T1, class T2> bool chmax(T1 &l, const T2 &r) { return (l < r) ? (l = r, true) : false; }
replace
22
23
22
23
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> // #include<ext/pb_ds/assoc_container.hpp> // #include<ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; #define pb push_back #define mp make_pair #define x first #define y second typedef long long int ll; // typedef tree< ll, null_type, less_equal< ll >, rb_tree_tag, // tree_order_statistics_node_update> ordered_set; #define pii pair<ll, ll> const ll N = 3e5 + 9; // const ll m = 1e9 + 7; const ll inf = 1e16; const ll mod = 1e9 + 7; ll dp[3005][3005][2]; ll a[3005]; void rec(ll i, ll j) { if (i == j) { dp[i][j][0] = a[i]; dp[i][j][1] = 0; // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n'; return; } else if (dp[i][j][0] != -1) { // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n'; return; } else { ll t = inf, sz; for (int k = i; k < j; k++) { rec(i, k); rec(k + 1, j); ll sum = dp[i][k][0] + dp[i][k][1] + dp[k + 1][j][0] + dp[k + 1][j][1]; if (t > sum) { t = sum; sz = dp[i][k][0] + dp[k + 1][j][0]; } } dp[i][j][0] = sz; dp[i][j][1] = t; // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n'; return; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll t = 1; // cin>>t; while (t--) { ll n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } memset(dp, -1, sizeof(dp)); rec(0, n - 1); cout << dp[0][n - 1][1]; } }
#include <bits/stdc++.h> // #include<ext/pb_ds/assoc_container.hpp> // #include<ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; #define pb push_back #define mp make_pair #define x first #define y second typedef long long int ll; // typedef tree< ll, null_type, less_equal< ll >, rb_tree_tag, // tree_order_statistics_node_update> ordered_set; #define pii pair<ll, ll> const ll N = 3e5 + 9; // const ll m = 1e9 + 7; const ll inf = 1e16; const ll mod = 1e9 + 7; ll dp[3005][3005][2]; ll a[3005]; void rec(ll i, ll j) { if (i == j) { dp[i][j][0] = a[i]; dp[i][j][1] = 0; // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n'; return; } else if (dp[i][j][0] != -1) { // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n'; return; } else { ll t = inf, sz; for (int k = i; k < j; k++) { rec(i, k); rec(k + 1, j); ll sum = dp[i][k][0] + dp[i][k][1] + dp[k + 1][j][0] + dp[k + 1][j][1]; if (t > sum) { t = sum; sz = dp[i][k][0] + dp[k + 1][j][0]; } } dp[i][j][0] = sz; dp[i][j][1] = t; // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n'; return; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t = 1; // cin>>t; while (t--) { ll n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } memset(dp, -1, sizeof(dp)); rec(0, n - 1); cout << dp[0][n - 1][1]; } }
replace
53
57
53
54
-11
p03173
C++
Runtime Error
/* Author: Racer5x *************************************** UNAUTHORISED COPYING OF CODE PROHIBITED ********************************** */ /*#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/ #include <bits/stdc++.h> #define int long long #define double long double #define pb emplace_back #define pf emplace_front #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define x first #define y second #define sz(x) (int)x.size() #define endl '\n' #define hell 998244353 #define PI 3.141592653589 #define tezz \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define MAX 2000000000000000000 #define M 1000000007 using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int power(int x, int n) { int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result % M; } int dp[405][405] = {0}; int tc[405][405] = {0}; signed main() { tezz #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vi v(n); for (int &x : v) cin >> x; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { dp[i][j] = MAX, tc[i][j] = MAX; } for (int size = 0; size < n; size++) { for (int i = 0, j = size; j < n; i++, j++) { if (i == j) { dp[i][j] = v[i]; tc[i][j] = 0; } else if (j - i == 1) { dp[i][j] = v[i] + v[j]; tc[i][j] = v[i] + v[j]; } else { for (int k = i; k < j; k++) { if (tc[i][j] > tc[i][k] + tc[k + 1][j] + dp[i][k] + dp[k + 1][j]) { dp[i][j] = dp[i][k] + dp[k + 1][j]; tc[i][j] = tc[i][k] + tc[k + 1][j] + dp[i][j]; // cout<<i<<' '<<k<<' '<<j<<" ------> "<<tc[i][j]<<endl; } // else cout<<i<<' '<<k<<' '<<j<<"____"<<tc[i][k] + tc[k+1][j] + // dp[i][k] + dp[k+1][j]<<endl; /*dp[i][j] = min(dp[i][j], tc[i][k] + tc[k+1][j] + dp[i][k] + dp[k+1][j]); tc[i][j] = dp[i][j];*/ } } } } /*cout<<endl; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(dp[i][j] == MAX) cout<<0<<' '; else cout<<dp[i][j]<<' '; } cout<<endl; } cout<<endl; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(tc[i][j] == MAX) cout<<0<<' '; else cout<<tc[i][j]<<' '; } cout<<endl; }*/ cout << tc[0][n - 1]; }
/* Author: Racer5x *************************************** UNAUTHORISED COPYING OF CODE PROHIBITED ********************************** */ /*#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/ #include <bits/stdc++.h> #define int long long #define double long double #define pb emplace_back #define pf emplace_front #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define x first #define y second #define sz(x) (int)x.size() #define endl '\n' #define hell 998244353 #define PI 3.141592653589 #define tezz \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define MAX 2000000000000000000 #define M 1000000007 using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int power(int x, int n) { int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result % M; } int dp[405][405] = {0}; int tc[405][405] = {0}; signed main() { tezz int n; cin >> n; vi v(n); for (int &x : v) cin >> x; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { dp[i][j] = MAX, tc[i][j] = MAX; } for (int size = 0; size < n; size++) { for (int i = 0, j = size; j < n; i++, j++) { if (i == j) { dp[i][j] = v[i]; tc[i][j] = 0; } else if (j - i == 1) { dp[i][j] = v[i] + v[j]; tc[i][j] = v[i] + v[j]; } else { for (int k = i; k < j; k++) { if (tc[i][j] > tc[i][k] + tc[k + 1][j] + dp[i][k] + dp[k + 1][j]) { dp[i][j] = dp[i][k] + dp[k + 1][j]; tc[i][j] = tc[i][k] + tc[k + 1][j] + dp[i][j]; // cout<<i<<' '<<k<<' '<<j<<" ------> "<<tc[i][j]<<endl; } // else cout<<i<<' '<<k<<' '<<j<<"____"<<tc[i][k] + tc[k+1][j] + // dp[i][k] + dp[k+1][j]<<endl; /*dp[i][j] = min(dp[i][j], tc[i][k] + tc[k+1][j] + dp[i][k] + dp[k+1][j]); tc[i][j] = dp[i][j];*/ } } } } /*cout<<endl; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(dp[i][j] == MAX) cout<<0<<' '; else cout<<dp[i][j]<<' '; } cout<<endl; } cout<<endl; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(tc[i][j] == MAX) cout<<0<<' '; else cout<<tc[i][j]<<' '; } cout<<endl; }*/ cout << tc[0][n - 1]; }
replace
55
61
55
56
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const long long int maxx = 1e15L; const int sz = 444; long long dp[sz][sz], sum[sz][sz]; vector<long long> v; long long f(int a, int b) { if (a == b) { return 0; } long long ans = maxx; for (int i = a; i < b; ++i) { ans = min(ans, f(a, i) + f(i + 1, b) + sum[a][b]); } dp[a][b] = ans; return ans; } int main(void) { int n; cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; v.push_back(x); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { dp[i][j] = maxx; } } for (int i = 0; i <= n - 1; ++i) { for (int j = i; j <= n - 1; ++j) { if (j == i) { sum[i][j] = v[i]; continue; } sum[i][j] = sum[i][j - 1] + v[j]; } } cout << f(0, n - 1); // cout<<" table "<<endl; // for (int i = 0; i < n; ++i) { // for (int j = 0; j < n; ++j) { // cout<<dp[i][j]<< " "; // } // cout<<endl; // } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int maxx = 1e15L; const int sz = 444; long long dp[sz][sz], sum[sz][sz]; vector<long long> v; long long f(int a, int b) { if (a == b) { return 0; } if (dp[a][b] != maxx) { return dp[a][b]; } long long ans = maxx; for (int i = a; i < b; ++i) { ans = min(ans, f(a, i) + f(i + 1, b) + sum[a][b]); } dp[a][b] = ans; return ans; } int main(void) { int n; cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; v.push_back(x); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { dp[i][j] = maxx; } } for (int i = 0; i <= n - 1; ++i) { for (int j = i; j <= n - 1; ++j) { if (j == i) { sum[i][j] = v[i]; continue; } sum[i][j] = sum[i][j - 1] + v[j]; } } cout << f(0, n - 1); // cout<<" table "<<endl; // for (int i = 0; i < n; ++i) { // for (int j = 0; j < n; ++j) { // cout<<dp[i][j]<< " "; // } // cout<<endl; // } return 0; }
insert
12
12
12
15
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int #define uu first #define vv second #define pii pair<int, int> #define pll pair<ll, ll> #define INF 1e18 #define fastRead \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) using namespace std; const int MAX = 1e5 + 5; const ll MOD = 1e9 + 7; ll dp[300][300]; int main() { fastRead; int n; cin >> n; vector<ll> vec(n + 1), sum(n + 1); for (int i = 1; i <= n; i++) cin >> vec[i], sum[i] = (sum[i - 1] + vec[i]); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) dp[i][j] = INF; } for (int i = 1; i <= n; i++) dp[1][i] = vec[i]; for (int i = 2; i <= n; i++) { for (int j = 1; j + i - 1 <= n; j++) { for (int k = 1; k < i; k++) { int len1 = k, len2 = i - k; int frm1 = j, frm2 = j + len1; int to = j + i - 1; // cout<<i<<" -> "<<j<<" "<<i + j-1<<" -- "<<frm1<<" // "<<frm1 + len1-1<<" -- "<<frm2<<" "<<len2 + frm2-1; // cout<<"\t--"<<dp[len1][frm1]<<" "<<dp[len2][frm2]<<" // "<<sum[to] - sum[frm1-1]<<'\n'; dp[i][j] = min(dp[i][j], dp[len1][frm1] + dp[len2][frm2] + sum[to] - sum[frm1 - 1]); } } } cout << dp[n][1] - sum[n] << '\n'; return 0; }
#include <bits/stdc++.h> #define ll long long int #define uu first #define vv second #define pii pair<int, int> #define pll pair<ll, ll> #define INF 1e18 #define fastRead \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) using namespace std; const int MAX = 1e5 + 5; const ll MOD = 1e9 + 7; ll dp[500][500]; int main() { fastRead; int n; cin >> n; vector<ll> vec(n + 1), sum(n + 1); for (int i = 1; i <= n; i++) cin >> vec[i], sum[i] = (sum[i - 1] + vec[i]); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) dp[i][j] = INF; } for (int i = 1; i <= n; i++) dp[1][i] = vec[i]; for (int i = 2; i <= n; i++) { for (int j = 1; j + i - 1 <= n; j++) { for (int k = 1; k < i; k++) { int len1 = k, len2 = i - k; int frm1 = j, frm2 = j + len1; int to = j + i - 1; // cout<<i<<" -> "<<j<<" "<<i + j-1<<" -- "<<frm1<<" // "<<frm1 + len1-1<<" -- "<<frm2<<" "<<len2 + frm2-1; // cout<<"\t--"<<dp[len1][frm1]<<" "<<dp[len2][frm2]<<" // "<<sum[to] - sum[frm1-1]<<'\n'; dp[i][j] = min(dp[i][j], dp[len1][frm1] + dp[len2][frm2] + sum[to] - sum[frm1 - 1]); } } } cout << dp[n][1] - sum[n] << '\n'; return 0; }
replace
15
16
15
16
0
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second typedef long long ll; typedef long double ld; typedef pair<int, int> pi; const int MOD = 1e9 + 7; const ll INF = 1e18; const double EPS = 1e-6; const int MAX_N = 405; int N; ll a[MAX_N]; ll memo[MAX_N][MAX_N]; ll dp(int l, int r) { if (l == r) return 0; if (memo[l][r] != -1) return memo[l][r]; ll total = 0; for (int i = l; i <= r; ++i) total += a[i]; ll ret = INF, sum = 0; for (int i = l; i < r; ++i) { sum += a[i]; ret = min(ret, dp(l, i) + dp(i + 1, r) + sum + (total - sum)); } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N; for (int i = 0; i < N; ++i) { cin >> a[i]; } memset(memo, -1, sizeof memo); cout << dp(0, N - 1) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second typedef long long ll; typedef long double ld; typedef pair<int, int> pi; const int MOD = 1e9 + 7; const ll INF = 1e18; const double EPS = 1e-6; const int MAX_N = 405; int N; ll a[MAX_N]; ll memo[MAX_N][MAX_N]; ll dp(int l, int r) { if (l == r) return 0; if (memo[l][r] != -1) return memo[l][r]; ll total = 0; for (int i = l; i <= r; ++i) total += a[i]; ll ret = INF, sum = 0; for (int i = l; i < r; ++i) { sum += a[i]; ret = min(ret, dp(l, i) + dp(i + 1, r) + sum + (total - sum)); } return memo[l][r] = ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N; for (int i = 0; i < N; ++i) { cin >> a[i]; } memset(memo, -1, sizeof memo); cout << dp(0, N - 1) << '\n'; return 0; }
replace
33
34
33
34
TLE
p03173
C++
Time Limit Exceeded
//----------------------------templates #pragma GCC optimize("Ofast") #pragma GCC target("tune=native") #pragma GCC target("avx") //---------------------------- #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int ll #define FOR(i, j, n) for (int i = (int)(j); i < (n); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPS(i, n) for (int i = 1; i <= (int)(n); i++) #define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REPNS(i, n) for (int i = (int)(n); i > 0; i--) #define I(n) scanf("%lld", &(n)) #define LL(n) scanf("%lld", &(n)) #define pb(n) push_back((n)) #define mp(i, j) make_pair((i), (j)) #define eb(i, j) emplace_back((i), (j)) #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; typedef vector<vvi> vvvi; const int mod = 1000000007; //-------------------------------------------- int n, a[410], rui[410]; int dp[410][410]; int merge(int l, int r) { if (r - l <= 1) return 0; if (r - l == 2) return a[l] + a[l + 1]; int ret = 1LL << 60; FOR(mid, l + 1, r) { int lsize = rui[mid] - rui[l]; int rsize = rui[r] - rui[mid]; int lcost = merge(l, mid); int rcost = merge(mid, r); ret = min(ret, lsize + rsize + lcost + rcost); } return dp[l][r] = ret; } signed main() { I(n); REP(i, n) I(a[i]); REPS(i, n) rui[i] = rui[i - 1] + a[i - 1]; cout << merge(0, n) << endl; }
//----------------------------templates #pragma GCC optimize("Ofast") #pragma GCC target("tune=native") #pragma GCC target("avx") //---------------------------- #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int ll #define FOR(i, j, n) for (int i = (int)(j); i < (n); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPS(i, n) for (int i = 1; i <= (int)(n); i++) #define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REPNS(i, n) for (int i = (int)(n); i > 0; i--) #define I(n) scanf("%lld", &(n)) #define LL(n) scanf("%lld", &(n)) #define pb(n) push_back((n)) #define mp(i, j) make_pair((i), (j)) #define eb(i, j) emplace_back((i), (j)) #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; typedef vector<vvi> vvvi; const int mod = 1000000007; //-------------------------------------------- int n, a[410], rui[410]; int dp[410][410]; int merge(int l, int r) { if (r - l <= 1) return 0; if (r - l == 2) return a[l] + a[l + 1]; if (dp[l][r]) return dp[l][r]; int ret = 1LL << 60; FOR(mid, l + 1, r) { int lsize = rui[mid] - rui[l]; int rsize = rui[r] - rui[mid]; int lcost = merge(l, mid); int rcost = merge(mid, r); ret = min(ret, lsize + rsize + lcost + rcost); } return dp[l][r] = ret; } signed main() { I(n); REP(i, n) I(a[i]); REPS(i, n) rui[i] = rui[i - 1] + a[i - 1]; cout << merge(0, n) << endl; }
insert
51
51
51
53
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, a, b) for (long long i = a; i <= b; ++i) #define REPN(i, a, b) for (long long i = a; i <= b; --i) #define rep(i, a, b) for (long long i = a; i < b; ++i) #define rep0(i, b) for (long long i = 0; i < b; ++i) #define REP0(i, b) for (long long i = 0; i <= b; ++i) #include <cmath> #include <iomanip> using namespace std; typedef long long ll; int MODE = 1e9 + 7; ll prefix[403]; ll dp[403][403]; ll arr[403]; int n; ll helper(int left, int right) { if (left < 0 || right > n - 1 || left > right || left == right) { return 0; } if (dp[left][right] != -1) { return dp[left][right]; } ll minimum = 1e18 + 5; for (int i = left; i <= right; ++i) { minimum = min(minimum, helper(left, i) + helper(i + 1, right)); } return dp[left][right] = minimum + prefix[right] - prefix[left - 1]; } int main() { cin >> n; memset(dp, -1, sizeof(dp)); rep0(i, n) { cin >> arr[i]; } prefix[0] = 0; rep0(i, n + 1) { prefix[i] = prefix[i - 1] + arr[i]; } cout << helper(0, n - 1) << endl; /*for(int i=0; i<n; ++i) { dp[i][i] = 0; } for(int i=n-2; i>=0; --i) { for(int j=i+1; j<n; ++j) { dp[i][j] = 1e18+5; ll s = prefix[j] - prefix[i-1]; for(int k = i; k<j; ++k) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + s); } } }*/ return 0; }
#include <bits/stdc++.h> #define REP(i, a, b) for (long long i = a; i <= b; ++i) #define REPN(i, a, b) for (long long i = a; i <= b; --i) #define rep(i, a, b) for (long long i = a; i < b; ++i) #define rep0(i, b) for (long long i = 0; i < b; ++i) #define REP0(i, b) for (long long i = 0; i <= b; ++i) #include <cmath> #include <iomanip> using namespace std; typedef long long ll; int MODE = 1e9 + 7; ll prefix[403]; ll dp[403][403]; ll arr[403]; int n; ll helper(int left, int right) { if (left < 0 || right > n - 1 || left > right || left == right) { return 0; } if (dp[left][right] != -1) { return dp[left][right]; } ll minimum = 1e18 + 5; for (int i = left; i < right; ++i) { minimum = min(minimum, helper(left, i) + helper(i + 1, right)); } return dp[left][right] = minimum + prefix[right] - prefix[left - 1]; } int main() { cin >> n; memset(dp, -1, sizeof(dp)); rep0(i, n) { cin >> arr[i]; } prefix[0] = 0; rep0(i, n + 1) { prefix[i] = prefix[i - 1] + arr[i]; } cout << helper(0, n - 1) << endl; /*for(int i=0; i<n; ++i) { dp[i][i] = 0; } for(int i=n-2; i>=0; --i) { for(int j=i+1; j<n; ++j) { dp[i][j] = 1e18+5; ll s = prefix[j] - prefix[i-1]; for(int k = i; k<j; ++k) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + s); } } }*/ return 0; }
replace
29
30
29
30
-11
p03173
C++
Runtime Error
#include <bits/stdc++.h> const int M = 998244353; #define int long long int using namespace std; #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define MAX 1e15 + 1 #define pi pair<int, int> const int mod = 1e9 + 7; const int N = 3e5 + 7; int n, k, a[109]; int dp[109][109], pre[109]; int sum(int aa, int b) { if (aa > b) return 0; if (aa == 0) return pre[b]; return pre[b] - pre[aa - 1]; } int solve(int l, int r) { if (l >= r) { return 0; } if (dp[l][r] != -1) return dp[l][r]; int val = MAX; for (int i = l; i < r; i++) { int x = solve(l, i); int y = solve(i + 1, r); int p = x + y + sum(l, i) + sum(i + 1, r); val = min(val, p); } dp[l][r] = val; return val; } signed main() { fast; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } pre[0] = a[0]; for (int i = 1; i < n; i++) pre[i] = pre[i - 1] + a[i]; memset(dp, -1, sizeof(dp)); cout << solve(0, n - 1); }
#include <bits/stdc++.h> const int M = 998244353; #define int long long int using namespace std; #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define MAX 1e15 + 1 #define pi pair<int, int> const int mod = 1e9 + 7; const int N = 3e5 + 7; int n, k, a[409]; int dp[409][409], pre[409]; int sum(int aa, int b) { if (aa > b) return 0; if (aa == 0) return pre[b]; return pre[b] - pre[aa - 1]; } int solve(int l, int r) { if (l >= r) { return 0; } if (dp[l][r] != -1) return dp[l][r]; int val = MAX; for (int i = l; i < r; i++) { int x = solve(l, i); int y = solve(i + 1, r); int p = x + y + sum(l, i) + sum(i + 1, r); val = min(val, p); } dp[l][r] = val; return val; } signed main() { fast; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } pre[0] = a[0]; for (int i = 1; i < n; i++) pre[i] = pre[i - 1] + a[i]; memset(dp, -1, sizeof(dp)); cout << solve(0, n - 1); }
replace
14
16
14
16
0
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; const int64_t INF = 1e18; int main() { int N; cin >> N; vector<int> A(N); for (auto &e : A) cin >> e; using P = pair<int64_t, int64_t>; map<int, map<int, P>> dp; auto f = [&](auto &&f, int l, int r) -> P { if (dp[l][r] != P(0LL, 0LL)) return dp[l][r]; if (l + 1 == r) return make_pair(0, A[l]); P res = make_pair(INF, INF); for (int i = l + 1; i < r; ++i) { auto left = f(f, l, i); auto right = f(f, i, r); auto cost = left.first + right.first; auto sz = left.second + right.second; if (res.first > cost + sz) { res.first = cost + sz; res.second = sz; } } return dp[l][r] = res; }; cout << f(f, 0, N).first << '\n'; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; const int64_t INF = 1e18; int main() { int N; cin >> N; vector<int> A(N); for (auto &e : A) cin >> e; using P = pair<int64_t, int64_t>; vector<vector<P>> dp(N + 1, vector<P>(N + 1)); auto f = [&](auto &&f, int l, int r) -> P { if (dp[l][r] != P(0LL, 0LL)) return dp[l][r]; if (l + 1 == r) return make_pair(0, A[l]); P res = make_pair(INF, INF); for (int i = l + 1; i < r; ++i) { auto left = f(f, l, i); auto right = f(f, i, r); auto cost = left.first + right.first; auto sz = left.second + right.second; if (res.first > cost + sz) { res.first = cost + sz; res.second = sz; } } return dp[l][r] = res; }; cout << f(f, 0, N).first << '\n'; }
replace
12
13
12
13
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; #define MP make_pair #define PB push_back #define X first #define Y second #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i) #define ITER(it, a) \ for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) #define ALL(a) a.begin(), a.end() #define SZ(a) (int)((a).size()) #define FILL(a, value) memset(a, value, sizeof(a)) #define debug(a) cout << #a << " = " << a << endl; const double PI = acos(-1.0); const LL INF = 1e9; const LL LINF = INF * INF; const int MAX = 404; int n; int a[MAX]; LL dp[MAX][MAX]; LL sum[MAX]; LL get(int l, int r) { LL res = sum[r]; if (l) res -= sum[l - 1]; return res; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // freopen("In.txt", "r", stdin); // freopen("In.txt", "w", stdout); cin >> n; FOR(i, 0, n) cin >> a[i]; sum[0] = a[0]; FOR(i, 1, n) sum[i] = sum[i - 1] + a[i]; FOR(i, 0, n) FOR(j, 0, n) dp[i][j] = LINF; FOR(i, 0, n - 1) dp[i][i + 1] = a[i] + a[i + 1]; FOR(i, 0, n) dp[i][i] = 0; FOR(len, 3, n + 1) { FOR(l, 0, n) { int r = l + len - 1; FOR(k, l, r) { LL bude = dp[l][k] + dp[k + 1][r]; dp[l][r] = min(dp[l][r], bude); } dp[l][r] += get(l, r); } } cout << dp[0][n - 1] << endl; cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; #define MP make_pair #define PB push_back #define X first #define Y second #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i) #define ITER(it, a) \ for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) #define ALL(a) a.begin(), a.end() #define SZ(a) (int)((a).size()) #define FILL(a, value) memset(a, value, sizeof(a)) #define debug(a) cout << #a << " = " << a << endl; const double PI = acos(-1.0); const LL INF = 1e9; const LL LINF = INF * INF; const int MAX = 404; int n; int a[MAX]; LL dp[MAX][MAX]; LL sum[MAX]; LL get(int l, int r) { LL res = sum[r]; if (l) res -= sum[l - 1]; return res; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // freopen("In.txt", "r", stdin); // freopen("In.txt", "w", stdout); cin >> n; FOR(i, 0, n) cin >> a[i]; sum[0] = a[0]; FOR(i, 1, n) sum[i] = sum[i - 1] + a[i]; FOR(i, 0, n) FOR(j, 0, n) dp[i][j] = LINF; FOR(i, 0, n - 1) dp[i][i + 1] = a[i] + a[i + 1]; FOR(i, 0, n) dp[i][i] = 0; FOR(len, 3, n + 1) { FOR(l, 0, n) { int r = l + len - 1; if (r >= n) break; FOR(k, l, r) { LL bude = dp[l][k] + dp[k + 1][r]; dp[l][r] = min(dp[l][r], bude); } dp[l][r] += get(l, r); } } cout << dp[0][n - 1] << endl; cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl; return 0; }
insert
61
61
61
64
0
Time elapsed: 0.034309
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using int64 = long long; template <typename T> vector<vector<T>> Make2DVector(int d1, int d2, T default_value) { return vector<vector<T>>(d1, vector<T>(d2, default_value)); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int64> xs(n); for (int i = 0; i < n; i++) cin >> xs[i]; vector<vector<int64>> dp1 = Make2DVector(n + 1, n + 1, -1LL); vector<vector<int64>> dp2 = Make2DVector(n + 1, n + 1, -1LL); function<void(int, int)> solve = [&](int l, int r) { if (r - l == 1) { dp1[l][r] = 0; dp2[l][r] = xs[l]; return; } for (int m = l + 1; m <= r - 1; m++) { solve(l, m); solve(m, r); if (dp2[l][r] < 0) { dp2[l][r] = dp2[l][m] + dp2[m][r]; } if (dp1[l][r] < 0) { dp1[l][r] = dp1[l][m] + dp1[m][r] + dp2[l][r]; } else { dp1[l][r] = min(dp1[l][r], dp1[l][m] + dp1[m][r] + dp2[l][r]); } } }; solve(0, n); cout << dp1[0][n] << endl; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; template <typename T> vector<vector<T>> Make2DVector(int d1, int d2, T default_value) { return vector<vector<T>>(d1, vector<T>(d2, default_value)); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int64> xs(n); for (int i = 0; i < n; i++) cin >> xs[i]; vector<vector<int64>> dp1 = Make2DVector(n + 1, n + 1, -1LL); vector<vector<int64>> dp2 = Make2DVector(n + 1, n + 1, -1LL); function<void(int, int)> solve = [&](int l, int r) { if (dp1[l][r] >= 0) return; if (r - l == 1) { dp1[l][r] = 0; dp2[l][r] = xs[l]; return; } for (int m = l + 1; m <= r - 1; m++) { solve(l, m); solve(m, r); if (dp2[l][r] < 0) { dp2[l][r] = dp2[l][m] + dp2[m][r]; } if (dp1[l][r] < 0) { dp1[l][r] = dp1[l][m] + dp1[m][r] + dp2[l][r]; } else { dp1[l][r] = min(dp1[l][r], dp1[l][m] + dp1[m][r] + dp2[l][r]); } } }; solve(0, n); cout << dp1[0][n] << endl; }
insert
24
24
24
27
TLE
p03173
C++
Time Limit Exceeded
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; // using namespace __gnu_pbds; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #define F first #define S second #define ll long long #define pb push_back #define db double #define ld long double #define ppb pop_back #define pii pair<int, int> #define pll pair<long long, long long> #define piii pair<int, pii> #define all(x) (x).begin(), (x).end() #define __ \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define watch(x) cout << (#x) << " is " << (x) << endl #define rep(i, a, n) for (int i = a; i <= n; i++) #define TEST \ int T; \ cin >> T; \ while (T--) #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } // #define int long long const int N = 1e5 + 1, inf = 2e9 + 7, mod = 1e9 + 7; const ll INF = 2e18 + 7, MOD = 1e15 + 7; int a[404], n; ll dp[404][404]; ll rec(int l, int r) { if (dp[l][r]) return dp[l][r]; ll ans = INF, sum = 0; for (int i = l; i < r; i++) { sum += a[i]; ans = min(ans, rec(l, i) + rec(i + 1, r)); } sum += a[r]; return ans + sum; } signed main() { __ // freopen("lcm.in", "r", stdin); // freopen("lcm.out", "w", stdout); cin >> n; ll sum = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; dp[i][i] = a[i]; } cout << rec(1, n) - sum; return 0; }
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; // using namespace __gnu_pbds; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #define F first #define S second #define ll long long #define pb push_back #define db double #define ld long double #define ppb pop_back #define pii pair<int, int> #define pll pair<long long, long long> #define piii pair<int, pii> #define all(x) (x).begin(), (x).end() #define __ \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define watch(x) cout << (#x) << " is " << (x) << endl #define rep(i, a, n) for (int i = a; i <= n; i++) #define TEST \ int T; \ cin >> T; \ while (T--) #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } // #define int long long const int N = 1e5 + 1, inf = 2e9 + 7, mod = 1e9 + 7; const ll INF = 2e18 + 7, MOD = 1e15 + 7; int a[404], n; ll dp[404][404]; ll rec(int l, int r) { if (dp[l][r]) return dp[l][r]; ll ans = INF, sum = 0; for (int i = l; i < r; i++) { sum += a[i]; ans = min(ans, rec(l, i) + rec(i + 1, r)); } sum += a[r]; return dp[l][r] = ans + sum; } signed main() { __ // freopen("lcm.in", "r", stdin); // freopen("lcm.out", "w", stdout); cin >> n; ll sum = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; dp[i][i] = a[i]; } cout << rec(1, n) - sum; return 0; }
replace
63
64
63
64
TLE
p03173
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cstdio> #include <cstring> #include <queue> #define qmin(x, y) (x = min(x, y)) #define qmax(x, y) (x = max(x, y)) using namespace std; inline char gc() { // static char buf[100000],*p1,*p2; // return //(p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; return getchar(); } template <class T> int read(T &ans) { ans = 0; char ch = gc(); T f = 1; while (!isdigit(ch)) { if (ch == EOF) return -1; if (ch == '-') f = -1; ch = gc(); } while (isdigit(ch)) ans = ans * 10 + ch - '0', ch = gc(); ans *= f; return 1; } template <class T1, class T2> int read(T1 &a, T2 &b) { return read(a) != EOF && read(b) != EOF ? 2 : EOF; } template <class T1, class T2, class T3> int read(T1 &a, T2 &b, T3 &c) { return read(a, b) != EOF && read(c) != EOF ? 3 : EOF; } typedef long long ll; const int Maxn = 310; const int inf = 0x3f3f3f3f; const int mod = 1000000007; int n; ll f[Maxn][Maxn], a[Maxn]; signed main() { // freopen("test.in","r",stdin); read(n); for (int i = 1; i <= n; i++) read(a[i]), a[i] += a[i - 1]; memset(f, 0x3f, sizeof(f)); for (int i = 1; i <= n; i++) f[i][i] = 0; for (int i = 2; i <= n; i++) for (int j = i; j <= n; j++) { int l = j - i + 1, r = j; for (int k = l; k < r; k++) qmin(f[l][r], f[l][k] + f[k + 1][r] + a[r] - a[l - 1]); } printf("%lld", f[1][n]); return 0; }
#include <algorithm> #include <cctype> #include <cstdio> #include <cstring> #include <queue> #define qmin(x, y) (x = min(x, y)) #define qmax(x, y) (x = max(x, y)) using namespace std; inline char gc() { // static char buf[100000],*p1,*p2; // return //(p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; return getchar(); } template <class T> int read(T &ans) { ans = 0; char ch = gc(); T f = 1; while (!isdigit(ch)) { if (ch == EOF) return -1; if (ch == '-') f = -1; ch = gc(); } while (isdigit(ch)) ans = ans * 10 + ch - '0', ch = gc(); ans *= f; return 1; } template <class T1, class T2> int read(T1 &a, T2 &b) { return read(a) != EOF && read(b) != EOF ? 2 : EOF; } template <class T1, class T2, class T3> int read(T1 &a, T2 &b, T3 &c) { return read(a, b) != EOF && read(c) != EOF ? 3 : EOF; } typedef long long ll; const int Maxn = 410; const int inf = 0x3f3f3f3f; const int mod = 1000000007; int n; ll f[Maxn][Maxn], a[Maxn]; signed main() { // freopen("test.in","r",stdin); read(n); for (int i = 1; i <= n; i++) read(a[i]), a[i] += a[i - 1]; memset(f, 0x3f, sizeof(f)); for (int i = 1; i <= n; i++) f[i][i] = 0; for (int i = 2; i <= n; i++) for (int j = i; j <= n; j++) { int l = j - i + 1, r = j; for (int k = l; k < r; k++) qmin(f[l][r], f[l][k] + f[k + 1][r] + a[r] - a[l - 1]); } printf("%lld", f[1][n]); return 0; }
replace
42
43
42
43
0
p03173
C++
Runtime Error
#include <cmath> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> typedef long long ll; const ll MOD = 1e9 + 7; const long long INF = 1LL << 60; const double PI = 3.14159265358979323846; const int NMAX = 100005; using namespace std; pair<ll, ll> dp[401][401]; int main() { int n; cin >> n; vector<ll> a(n + 1); for (int i = 0; i < n; i++) { cin >> a[i]; } a[n] = INF; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = make_pair(0, 0); if (i + 1 == j) { dp[i][j].second = a[i]; } } } for (int l = 2; l <= n; l++) { for (int i = 0; i < n; i++) { int j = i + l; ll v = INF; ll s = 0; for (int k = 1; k < l; k++) { if (dp[i + k][j].first + dp[i][i + k].first + dp[i + k][j].second + dp[i][i + k].second < v) { v = dp[i + k][j].first + dp[i][i + k].first + dp[i + k][j].second + dp[i][i + k].second; s = dp[i + k][j].second + dp[i][i + k].second; } } dp[i][j].first = v; dp[i][j].second = s; } } cout << dp[0][n].first << endl; return 0; }
#include <cmath> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> typedef long long ll; const ll MOD = 1e9 + 7; const long long INF = 1LL << 60; const double PI = 3.14159265358979323846; const int NMAX = 100005; using namespace std; pair<ll, ll> dp[401][401]; int main() { int n; cin >> n; vector<ll> a(n + 1); for (int i = 0; i < n; i++) { cin >> a[i]; } a[n] = INF; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = make_pair(0, 0); if (i + 1 == j) { dp[i][j].second = a[i]; } } } for (int l = 2; l <= n; l++) { for (int i = 0; i + l <= n; i++) { int j = i + l; ll v = INF; ll s = 0; for (int k = 1; k < l; k++) { if (dp[i + k][j].first + dp[i][i + k].first + dp[i + k][j].second + dp[i][i + k].second < v) { v = dp[i + k][j].first + dp[i][i + k].first + dp[i + k][j].second + dp[i][i + k].second; s = dp[i + k][j].second + dp[i][i + k].second; } } dp[i][j].first = v; dp[i][j].second = s; } } cout << dp[0][n].first << endl; return 0; }
replace
36
37
36
37
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long dp[401][401]; long long a[401]; long long ps[401]; int main() { int n; cin >> n; memset(dp, 0x3f3f3f3f, sizeof dp); for (int i = 1; i <= n; i++) { cin >> a[i]; dp[i][i] = 0; if (i > 0) { dp[i - 1][i] = a[i - 1] + a[i]; } } partial_sum(a, a + n + 1, ps); for (int l = 2; l <= n + 1; l++) { for (int i = 1; i <= n - l + 1; i++) { for (int j = 0; j < l; j++) { dp[i][i + l] = min(dp[i][i + l], dp[i][i + j] + dp[i + j + 1][i + l] + ps[i + j] - ps[i - 1] + ps[i + l] - ps[i + j]); } } } cout << dp[1][n]; }
#include <bits/stdc++.h> using namespace std; long long dp[402][402]; long long a[402]; long long ps[402]; int main() { int n; cin >> n; memset(dp, 0x3f3f3f3f, sizeof dp); for (int i = 1; i <= n; i++) { cin >> a[i]; dp[i][i] = 0; if (i > 0) { dp[i - 1][i] = a[i - 1] + a[i]; } } partial_sum(a, a + n + 1, ps); for (int l = 2; l <= n + 1; l++) { for (int i = 1; i <= n - l + 1; i++) { for (int j = 0; j < l; j++) { dp[i][i + l] = min(dp[i][i + l], dp[i][i + j] + dp[i + j + 1][i + l] + ps[i + j] - ps[i - 1] + ps[i + l] - ps[i + j]); } } } cout << dp[1][n]; }
replace
2
5
2
5
0
p03173
C++
Runtime Error
#include <iostream> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long dp[402][402] = {}, sum[402][402] = {}; for (int i = 0; i < n; i++) for (int j = i + 1; j <= n; j++) { dp[i][j] = 1LL << 60; sum[i][j] = sum[i][j - 1] + a[j - 1]; } for (int i = 0; i < n; i++) dp[i][i + 1] = 0; for (int w = 2; w <= n; w++) for (int i = 0; i < n; i++) { for (int j = i + 1; j < i + w; j++) { dp[i][i + w] = min(dp[i][i + w], dp[i][j] + dp[j][i + w] + sum[i][j] + sum[j][i + w]); } } cout << dp[0][n] << endl; }
#include <iostream> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long dp[402][402] = {}, sum[402][402] = {}; for (int i = 0; i < n; i++) for (int j = i + 1; j <= n; j++) { dp[i][j] = 1LL << 60; sum[i][j] = sum[i][j - 1] + a[j - 1]; } for (int i = 0; i < n; i++) dp[i][i + 1] = 0; for (int w = 2; w <= n; w++) for (int i = 0; i + w <= n; i++) { for (int j = i + 1; j < i + w; j++) { dp[i][i + w] = min(dp[i][i + w], dp[i][j] + dp[j][i + w] + sum[i][j] + sum[j][i + w]); } } cout << dp[0][n] << endl; }
replace
17
18
17
18
0
p03173
C++
Runtime Error
// Radhe Radhe #include <bits/stdc++.h> // #include<boost/multiprecision/cpp_int.hpp> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // using namespace boost::multiprecision; // #include<boost/math/common_factor.hpp> #include <ext/numeric> using namespace __gnu_cxx; using namespace std; #define int long long #define endl "\n" #define F first #define S second #define pb push_back #define mp make_pair #define array(a, n) \ int a[n]; \ for (int i = 0; i < n; i++) \ cin >> a[i]; #define loop(i, a, b, x) for (int i = a; i < b; i += x) const int MOD = 1000000007; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; // cin >> t; while (t--) { int n; cin >> n; int a[n]; int pref[n + 1]; pref[0] = 0; for (int i = 0; i < n; i++) { cin >> a[i]; pref[i + 1] = pref[i] + a[i]; } int dp[n][n]; memset(dp, 0x3f, sizeof(dp)); for (int i = 0; i < n; i++) { dp[i][i] = 0; } for (int l = 2; l <= n; l++) { for (int i = 0; i < n - l + 1; i++) { int j = i + l - 1; for (int k = i; k < j; k++) { dp[i][j] = min(dp[i][j], (dp[i][k] + dp[k + 1][j]) + (pref[k + 1] - pref[i] + pref[j + 1] - pref[k + 1])); } } } cout << dp[0][n - 1] << endl; } return 0; }
// Radhe Radhe #include <bits/stdc++.h> // #include<boost/multiprecision/cpp_int.hpp> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // using namespace boost::multiprecision; // #include<boost/math/common_factor.hpp> #include <ext/numeric> using namespace __gnu_cxx; using namespace std; #define int long long #define endl "\n" #define F first #define S second #define pb push_back #define mp make_pair #define array(a, n) \ int a[n]; \ for (int i = 0; i < n; i++) \ cin >> a[i]; #define loop(i, a, b, x) for (int i = a; i < b; i += x) const int MOD = 1000000007; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; // cin >> t; while (t--) { int n; cin >> n; int a[n]; int pref[n + 1]; pref[0] = 0; for (int i = 0; i < n; i++) { cin >> a[i]; pref[i + 1] = pref[i] + a[i]; } int dp[n][n]; memset(dp, 0x3f, sizeof(dp)); for (int i = 0; i < n; i++) { dp[i][i] = 0; } for (int l = 2; l <= n; l++) { for (int i = 0; i < n - l + 1; i++) { int j = i + l - 1; for (int k = i; k < j; k++) { dp[i][j] = min(dp[i][j], (dp[i][k] + dp[k + 1][j]) + (pref[k + 1] - pref[i] + pref[j + 1] - pref[k + 1])); } } } cout << dp[0][n - 1] << endl; } return 0; }
delete
27
32
27
27
-11
p03173
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define endl '\n' #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); using namespace std; const int N = 300; vector<int> a(N + 1); vector<int> psum(N + 1); vector<vector<int>> dp(N + 1, vector<int>(N + 1, -1)); int n; int solve(int l, int r) { if (l == r) return 0; if (dp[l][r] != -1) return dp[l][r]; dp[l][r] = 1e15; for (int i = l; i < r; i++) dp[l][r] = min(dp[l][r], psum[r] - psum[l - 1] + solve(l, i) + solve(i + 1, r)); return dp[l][r]; } int32_t main() { IOS cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; psum[i] = psum[i - 1] + a[i]; } cout << solve(1, n) << endl; }
#include <bits/stdc++.h> #define int long long #define endl '\n' #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); using namespace std; const int N = 400; vector<int> a(N + 1); vector<int> psum(N + 1); vector<vector<int>> dp(N + 1, vector<int>(N + 1, -1)); int n; int solve(int l, int r) { if (l == r) return 0; if (dp[l][r] != -1) return dp[l][r]; dp[l][r] = 1e15; for (int i = l; i < r; i++) dp[l][r] = min(dp[l][r], psum[r] - psum[l - 1] + solve(l, i) + solve(i + 1, r)); return dp[l][r]; } int32_t main() { IOS cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; psum[i] = psum[i - 1] + a[i]; } cout << solve(1, n) << endl; }
replace
9
10
9
10
0
p03173
C++
Runtime Error
#include <bits/stdc++.h> typedef long long int ll; using namespace std; ll pref_sum[404][404], dp[404][404]; int a[3000]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; if (n == 1) { cout << a[0] << endl; return 0; } memset(pref_sum, 0, sizeof(pref_sum)); memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { pref_sum[i][i] = a[i]; dp[i][i] = a[i]; dp[i][i + 1] = a[i] + a[i + 1]; for (int j = i + 1; j < n; j++) pref_sum[i][j] = pref_sum[i][j - 1] + a[j]; } int i = 0, j = 2; int jstart = 1; while (true) { ll val; ll minval = LONG_LONG_MAX; for (int z = i; z < j; z++) { val = pref_sum[i][j] + dp[i][z] + dp[z + 1][j]; if (z == i) val -= a[i]; if (z + 1 == j) val -= a[j]; minval = min(val, minval); } dp[i][j] = minval; if ((i == 0 && j == n - 1)) break; i++; j++; if (j == n) { i = 0; jstart++; j = jstart; } } cout << dp[0][n - 1] << endl; }
#include <bits/stdc++.h> typedef long long int ll; using namespace std; ll pref_sum[404][404], dp[404][404]; int a[3000]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; if (n == 1) { cout << a[0] << endl; return 0; } if (n == 2) { cout << a[0] + a[1] << endl; return 0; } memset(pref_sum, 0, sizeof(pref_sum)); memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { pref_sum[i][i] = a[i]; dp[i][i] = a[i]; dp[i][i + 1] = a[i] + a[i + 1]; for (int j = i + 1; j < n; j++) pref_sum[i][j] = pref_sum[i][j - 1] + a[j]; } int i = 0, j = 2; int jstart = 1; while (true) { ll val; ll minval = LONG_LONG_MAX; for (int z = i; z < j; z++) { val = pref_sum[i][j] + dp[i][z] + dp[z + 1][j]; if (z == i) val -= a[i]; if (z + 1 == j) val -= a[j]; minval = min(val, minval); } dp[i][j] = minval; if ((i == 0 && j == n - 1)) break; i++; j++; if (j == n) { i = 0; jstart++; j = jstart; } } cout << dp[0][n - 1] << endl; }
insert
12
12
12
16
0
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std::literals::string_literals; using i64 = long long; using std::cin; using std::cout; using std::endl; template <typename T> std::vector<T> make_v(size_t a) { return std::vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } std::vector<i64> a; std::vector<std::vector<i64>> dp; i64 solve(i64 L, i64 R) { if (L >= R) return 0LL; if (L + 1 == R) return a[L]; i64 A = 0, B = 0, ret = 1LL << 60; for (int i = L; i < R; i++) A += a[i]; for (int i = L; i < R - 1; i++) { B += a[i]; A -= a[i]; ret = std::min(ret, solve(L, i + 1) + solve(i + 1, R) + (A + B)); } return dp[L][R] = ret; } int main() { int n; scanf("%d", &n); a.resize(n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); dp = make_v<i64>(n + 1, n + 1); solve(0, n); for (auto v : a) dp[0][n] -= v; printf("%lld\n", dp[0][n]); return 0; }
#include <bits/stdc++.h> using namespace std::literals::string_literals; using i64 = long long; using std::cin; using std::cout; using std::endl; template <typename T> std::vector<T> make_v(size_t a) { return std::vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } std::vector<i64> a; std::vector<std::vector<i64>> dp; i64 solve(i64 L, i64 R) { if (dp[L][R]) return dp[L][R]; if (L >= R) return 0LL; if (L + 1 == R) return a[L]; i64 A = 0, B = 0, ret = 1LL << 60; for (int i = L; i < R; i++) A += a[i]; for (int i = L; i < R - 1; i++) { B += a[i]; A -= a[i]; ret = std::min(ret, solve(L, i + 1) + solve(i + 1, R) + (A + B)); } return dp[L][R] = ret; } int main() { int n; scanf("%d", &n); a.resize(n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); dp = make_v<i64>(n + 1, n + 1); solve(0, n); for (auto v : a) dp[0][n] -= v; printf("%lld\n", dp[0][n]); return 0; }
insert
18
18
18
20
TLE
p03173
C++
Time Limit Exceeded
#define DEBUG 1 #include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using pll = pair<ll, ll>; using tll = tuple<ll, ll, ll>; #define all(v) (v).begin(), (v).end() #define for1(i, n) for (ll i = 0; i < (n); i++) #define for2(i, m, n) for (ll i = (m); i < (n); i++) #define for3(i, m, n, d) for (ll i = (m); i < (n); i += (d)) #define rfor2(i, m, n) for (ll i = (m); i > (n); i--) #define rfor3(i, m, n, d) for (ll i = (m); i > (n); i += (d)) #define INF 1111111111111111111LL #define MOD 1000000007LL // 10**9 + 7 #define print(...) print_1(__VA_ARGS__) #define in(...) in_1(__VA_ARGS__) #if DEBUG #define dump(...) dump_1(#__VA_ARGS__, __VA_ARGS__) #else #define dump(...) #endif template <typename Head> void dump_1(const char *str, Head &&h) { cerr << str << ": " << h << '\n'; } template <typename Head, typename... Tail> void dump_1(const char *str, Head &&h, Tail &&...t) { while (*str != ',') { cerr << *str++; } cerr << ": " << h << ' '; dump_1(str + 1, t...); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &v) { os << '(' << v.first << ", " << v.second << ')'; return os; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &v) { os << '(' << get<0>(v) << ", " << get<1>(v) << ", " << get<2>(v) << ')'; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ", "; } os << it->first << ':' << it->second; } os << '}'; return os; } void Yes(void) { cout << "Yes\n"; } void No(void) { cout << "No\n"; } void YES(void) { cout << "YES\n"; } void NO(void) { cout << "NO\n"; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void vin(vector<T> &v, ll len) { for1(i, len) { cin >> v[i]; } } template <typename Head> void in_1(Head &h) { cin >> h; } template <typename Head, typename... Tail> void in_1(Head &h, Tail &...t) { cin >> h; in_1(t...); } template <typename Head> void print_1(Head &&h) { cout << h << '\n'; } template <typename Head, typename... Tail> void print_1(Head &&h, Tail &&...t) { cout << h << ' '; print_1(t...); } //--------------------------------------------------------- ll memo[400][400]; ll A[400]; ll C[401]; ll calc(ll l, ll r) { if (memo[l][r] != 0) { return memo[l][r]; } if (l == r) { return 0; } ll ret = INF; for2(i, l, r) { ll ret1 = calc(l, i) + calc(i + 1, r) + C[r + 1] - C[l]; chmin(ret, ret1); } return ret; } void solve() { ll N; in(N); for1(i, N) { in(A[i]); } for1(i, N) { C[i + 1] = C[i] + A[i]; } print(calc(0, N - 1)); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(16); cerr << fixed << setprecision(16); solve(); }
#define DEBUG 1 #include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using pll = pair<ll, ll>; using tll = tuple<ll, ll, ll>; #define all(v) (v).begin(), (v).end() #define for1(i, n) for (ll i = 0; i < (n); i++) #define for2(i, m, n) for (ll i = (m); i < (n); i++) #define for3(i, m, n, d) for (ll i = (m); i < (n); i += (d)) #define rfor2(i, m, n) for (ll i = (m); i > (n); i--) #define rfor3(i, m, n, d) for (ll i = (m); i > (n); i += (d)) #define INF 1111111111111111111LL #define MOD 1000000007LL // 10**9 + 7 #define print(...) print_1(__VA_ARGS__) #define in(...) in_1(__VA_ARGS__) #if DEBUG #define dump(...) dump_1(#__VA_ARGS__, __VA_ARGS__) #else #define dump(...) #endif template <typename Head> void dump_1(const char *str, Head &&h) { cerr << str << ": " << h << '\n'; } template <typename Head, typename... Tail> void dump_1(const char *str, Head &&h, Tail &&...t) { while (*str != ',') { cerr << *str++; } cerr << ": " << h << ' '; dump_1(str + 1, t...); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &v) { os << '(' << v.first << ", " << v.second << ')'; return os; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &v) { os << '(' << get<0>(v) << ", " << get<1>(v) << ", " << get<2>(v) << ')'; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ' '; } os << *it; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) { os << ", "; } os << it->first << ':' << it->second; } os << '}'; return os; } void Yes(void) { cout << "Yes\n"; } void No(void) { cout << "No\n"; } void YES(void) { cout << "YES\n"; } void NO(void) { cout << "NO\n"; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void vin(vector<T> &v, ll len) { for1(i, len) { cin >> v[i]; } } template <typename Head> void in_1(Head &h) { cin >> h; } template <typename Head, typename... Tail> void in_1(Head &h, Tail &...t) { cin >> h; in_1(t...); } template <typename Head> void print_1(Head &&h) { cout << h << '\n'; } template <typename Head, typename... Tail> void print_1(Head &&h, Tail &&...t) { cout << h << ' '; print_1(t...); } //--------------------------------------------------------- ll memo[400][400]; ll A[400]; ll C[401]; ll calc(ll l, ll r) { if (memo[l][r] != 0) { return memo[l][r]; } if (l == r) { return 0; } ll ret = INF; for2(i, l, r) { ll ret1 = calc(l, i) + calc(i + 1, r) + C[r + 1] - C[l]; chmin(ret, ret1); } memo[l][r] = ret; return ret; } void solve() { ll N; in(N); for1(i, N) { in(A[i]); } for1(i, N) { C[i + 1] = C[i] + A[i]; } print(calc(0, N - 1)); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(16); cerr << fixed << setprecision(16); solve(); }
insert
145
145
145
146
TLE
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; #define IOS() \ ios_base::sync_with_stdio(0); \ cin.tie(0); typedef long long ll; #define pb push_back typedef pair<ll, ll> pi; ll dp[401][401]; ll sum[401][401]; ll a[500]; int n; ll find(int l, int r) { ll ans = LONG_LONG_MAX; // cout<<"ans is "<<ans<<endl; if (l == r) return dp[l][r] = 0ll; else if (dp[l][r] != -1) return dp[l][r]; else { for (int i = l; i < r; i++) { ans = std::min(ans, sum[l][r] + find(l, i) + find(i + 1, r)); } return ans; } } void preprocess() { for (int i = 1; i <= n; i++) { sum[i][i] = a[i]; for (int j = i + 1; j <= n; j++) { sum[i][j] = sum[i][j - 1] + a[j]; } } } int main() { IOS(); cin >> n; memset(dp, -1ll, sizeof dp); for (int i = 1; i <= n; ++i) { cin >> a[i]; } preprocess(); ll ans = find(1, n); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; #define IOS() \ ios_base::sync_with_stdio(0); \ cin.tie(0); typedef long long ll; #define pb push_back typedef pair<ll, ll> pi; ll dp[401][401]; ll sum[401][401]; ll a[500]; int n; ll find(int l, int r) { ll ans = LONG_LONG_MAX; // cout<<"ans is "<<ans<<endl; if (l == r) return dp[l][r] = 0ll; else if (dp[l][r] != -1) return dp[l][r]; else { for (int i = l; i < r; i++) { ans = std::min(ans, sum[l][r] + find(l, i) + find(i + 1, r)); } return dp[l][r] = ans; } } void preprocess() { for (int i = 1; i <= n; i++) { sum[i][i] = a[i]; for (int j = i + 1; j <= n; j++) { sum[i][j] = sum[i][j - 1] + a[j]; } } } int main() { IOS(); cin >> n; memset(dp, -1ll, sizeof dp); for (int i = 1; i <= n; ++i) { cin >> a[i]; } preprocess(); ll ans = find(1, n); cout << ans << endl; return 0; }
replace
28
29
28
29
TLE
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ALL(obj) begin(obj), end(obj) using namespace std; template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } using ll = long long; using P = pair<ll, ll>; const ll INF = 1e18; int N; vector<ll> a; vector<vector<P>> dp; // first: コストの合計の最小値、second: 1つにまとめた時の値 P rec(int l = 0, int r = N) { // メモ化再帰 if (r - l == 1) return make_pair(0, a[l]); P &ret = dp[l][r]; for (int i = l + 1; i < r; i++) { P L = rec(l, i); P R = rec(i, r); ll sum = L.second + R.second; ll cost = L.first + R.first + sum; chmin(ret, make_pair(cost, sum)); } return ret; } int main() { cin >> N; a.resize(N); for (int i = 0; i < N; i++) { cin >> a.at(i); } dp.assign(N + 2, vector<P>(N + 2, make_pair(INF, 0))); // 初期化 cout << rec().first << endl; return 0; }
#include <bits/stdc++.h> #define ALL(obj) begin(obj), end(obj) using namespace std; template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } using ll = long long; using P = pair<ll, ll>; const ll INF = 1e18; int N; vector<ll> a; vector<vector<P>> dp; // first: コストの合計の最小値、second: 1つにまとめた時の値 P rec(int l = 0, int r = N) { // メモ化再帰 if (r - l == 1) return make_pair(0, a[l]); P &ret = dp[l][r]; if (ret.first != INF) return ret; for (int i = l + 1; i < r; i++) { P L = rec(l, i); P R = rec(i, r); ll sum = L.second + R.second; ll cost = L.first + R.first + sum; chmin(ret, make_pair(cost, sum)); } return ret; } int main() { cin >> N; a.resize(N); for (int i = 0; i < N; i++) { cin >> a.at(i); } dp.assign(N + 2, vector<P>(N + 2, make_pair(INF, 0))); // 初期化 cout << rec().first << endl; return 0; }
insert
24
24
24
26
TLE
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 ll dp[401][401]; vector<ll> v, pre; ll get(ll l, ll r) { return pre[r] - (l > 0 ? pre[l - 1] : 0); } ll f(ll l, ll r) { if (l == r) return 0; if (r - l == 1) return v[l] + v[r]; if (dp[l][r] != -1) dp[l][r]; ll ans = -1; for (ll i = l; i < r; i++) { if (ans == -1) { ans = f(l, i) + f(i + 1, r) + get(l, i) + get(i + 1, r); } else { ans = min(ans, f(l, i) + f(i + 1, r) + get(l, i) + get(i + 1, r)); } } dp[l][r] = ans; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); memset(dp, -1, sizeof(dp)); ll n; cin >> n; v.resize(n); pre.resize(n); for (ll &i : v) { cin >> i; } pre[0] = v[0]; for (ll i = 1; i < n; i++) { pre[i] = v[i]; pre[i] += (i > 0) ? pre[i - 1] : 0; } cout << f(0, n - 1); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 ll dp[401][401]; vector<ll> v, pre; ll get(ll l, ll r) { return pre[r] - (l > 0 ? pre[l - 1] : 0); } ll f(ll l, ll r) { if (l == r) return 0; if (r - l == 1) return v[l] + v[r]; if (dp[l][r] != -1) return dp[l][r]; ll ans = -1; for (ll i = l; i < r; i++) { if (ans == -1) { ans = f(l, i) + f(i + 1, r) + get(l, i) + get(i + 1, r); } else { ans = min(ans, f(l, i) + f(i + 1, r) + get(l, i) + get(i + 1, r)); } } dp[l][r] = ans; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); memset(dp, -1, sizeof(dp)); ll n; cin >> n; v.resize(n); pre.resize(n); for (ll &i : v) { cin >> i; } pre[0] = v[0]; for (ll i = 1; i < n; i++) { pre[i] = v[i]; pre[i] += (i > 0) ? pre[i - 1] : 0; } cout << f(0, n - 1); }
replace
18
19
18
19
TLE
p03173
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define rep(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) typedef long long ll; const ll mod = 1e9 + 7; const ll INF = 1e18 + 7; ll sum(int i, int j, int *a) { ll res = 0; for (auto k = i; k <= j; ++k) res += a[k]; return res; } int main(int argc, const char **argv) { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif int n, i, j, k; cin >> n; int a[n]; for (i = 0; i < n; ++i) cin >> a[i]; ll dp[n][n]; for (i = n - 1; i >= 0; --i) { for (j = i; j < n; ++j) { if (i == j) dp[i][j] = 0; else { dp[i][j] = INF; ll s = sum(i, j, a); for (k = i; k < j; ++k) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + s); } } } } cout << dp[0][n - 1] << "\n"; #ifndef LOCAL_DEFINE cerr << "Time elapsed: " << 1e1 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif // LOCAL_DEFINE return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define rep(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) typedef long long ll; const ll mod = 1e9 + 7; const ll INF = 1e18 + 7; ll sum(int i, int j, int *a) { ll res = 0; for (auto k = i; k <= j; ++k) res += a[k]; return res; } int main(int argc, const char **argv) { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; int n, i, j, k; cin >> n; int a[n]; for (i = 0; i < n; ++i) cin >> a[i]; ll dp[n][n]; for (i = n - 1; i >= 0; --i) { for (j = i; j < n; ++j) { if (i == j) dp[i][j] = 0; else { dp[i][j] = INF; ll s = sum(i, j, a); for (k = i; k < j; ++k) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + s); } } } } cout << dp[0][n - 1] << "\n"; #ifndef LOCAL_DEFINE cerr << "Time elapsed: " << 1e1 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif // LOCAL_DEFINE return 0; }
delete
28
33
28
28
-11
p03173
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i)) using ll = long long; using namespace std; #define INF ((1 << 30) - 1) #define LLINF (1LL << 60) #define EPS (1e-10) // dp[i][r] := l以上r未満のスライムを1体にするときのコストの最小値 // dp[i][i+1] = 0 // dp[l][r] = min(dp[l][m], dp[m][r] + [l, m)スライムと[m, // r)スライムを合致させるコスト) (r-l)の小さい順からやればいい ll dp[310][310]; ll sum[310][310]; int main() { int n; cin >> n; vector<ll> a(n + 1); rep(i, n) cin >> a[i]; rep(l, n) { sum[l][l + 1] = a[l]; for (int r = l + 2; r <= n; ++r) { sum[l][r] = sum[l][r - 1] + a[r - 1]; } } for (int len = 2; len <= n; ++len) { for (int l = 0; l + len <= n; ++l) { int r = l + len; dp[l][r] = LLINF; for (int m = l + 1; m < r; ++m) { dp[l][r] = min(dp[l][r], dp[l][m] + dp[m][r] + sum[l][r]); } } } cout << dp[0][n] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i)) using ll = long long; using namespace std; #define INF ((1 << 30) - 1) #define LLINF (1LL << 60) #define EPS (1e-10) // dp[i][r] := l以上r未満のスライムを1体にするときのコストの最小値 // dp[i][i+1] = 0 // dp[l][r] = min(dp[l][m], dp[m][r] + [l, m)スライムと[m, // r)スライムを合致させるコスト) (r-l)の小さい順からやればいい ll dp[410][410]; ll sum[410][410]; int main() { int n; cin >> n; vector<ll> a(n + 1); rep(i, n) cin >> a[i]; rep(l, n) { sum[l][l + 1] = a[l]; for (int r = l + 2; r <= n; ++r) { sum[l][r] = sum[l][r - 1] + a[r - 1]; } } for (int len = 2; len <= n; ++len) { for (int l = 0; l + len <= n; ++l) { int r = l + len; dp[l][r] = LLINF; for (int m = l + 1; m < r; ++m) { dp[l][r] = min(dp[l][r], dp[l][m] + dp[m][r] + sum[l][r]); } } } cout << dp[0][n] << endl; }
replace
15
17
15
17
0
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long long long helper(vector<long long> &vec, vector<vector<long long>> &dp, long long l, long long r) { if (l == r) return 0; if (l + 1 == r) return dp[l][r] = vec[l] + vec[r]; long long ans = LLONG_MAX, sum = 0; for (long long i = l; i <= r; ++i) { sum += vec[i]; } for (long long i = l; i < r; ++i) { ans = min(ans, sum + helper(vec, dp, l, i) + helper(vec, dp, i + 1, r)); } return dp[l][r] = ans; } int main() { long long n; cin >> n; vector<long long> vec(n, 0); for (long long i = 0; i < n; i++) cin >> vec[i]; vector<vector<long long>> dp(n, vector<long long>(n, -1)); cout << helper(vec, dp, 0, n - 1); }
#include <bits/stdc++.h> using namespace std; #define ll long long long long helper(vector<long long> &vec, vector<vector<long long>> &dp, long long l, long long r) { if (l == r) return 0; if (l + 1 == r) return dp[l][r] = vec[l] + vec[r]; if (dp[l][r] != -1) return dp[l][r]; long long ans = LLONG_MAX, sum = 0; for (long long i = l; i <= r; ++i) { sum += vec[i]; } for (long long i = l; i < r; ++i) { ans = min(ans, sum + helper(vec, dp, l, i) + helper(vec, dp, i + 1, r)); } return dp[l][r] = ans; } int main() { long long n; cin >> n; vector<long long> vec(n, 0); for (long long i = 0; i < n; i++) cin >> vec[i]; vector<vector<long long>> dp(n, vector<long long>(n, -1)); cout << helper(vec, dp, 0, n - 1); }
insert
9
9
9
11
TLE
p03173
C++
Runtime Error
#include <algorithm> #include <iostream> #include <limits> #define MAX_ELEMENTS 400 using namespace std; // I have to create a binary tree where each inner node has the value of its two // sons, I want to minimize the value of the root. long long dp[MAX_ELEMENTS] [MAX_ELEMENTS]; // The weight of having to combine all elements // between interval i and j. It has to be continuous // since the slimes don't change positions long long slimes[MAX_ELEMENTS]; // The slimes themselves long long inf = 1e18L + 5; long long arraySum(int L, int R) { long long ans = 0; for (int i = L; i <= R; i++) ans += slimes[i]; return ans; } int main() { int nSlimes; long long slimeValue; cin >> nSlimes; for (int i = 0; i < nSlimes; i++) { cin >> slimeValue; slimes[i] = slimeValue; } for (int i = nSlimes - 1; i >= 0; i--) { for (int j = i; j < nSlimes; j++) { if (i == j) dp[i][j] = 0; // The cost to combine a slime to itself is none else { dp[i][j] = inf; for (int k = i; k <= j; k++) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + arraySum(i, j)); } } } cout << dp[0][nSlimes - 1]; }
#include <algorithm> #include <iostream> #include <limits> #define MAX_ELEMENTS 400 using namespace std; // I have to create a binary tree where each inner node has the value of its two // sons, I want to minimize the value of the root. long long dp[MAX_ELEMENTS] [MAX_ELEMENTS]; // The weight of having to combine all elements // between interval i and j. It has to be continuous // since the slimes don't change positions long long slimes[MAX_ELEMENTS]; // The slimes themselves long long inf = 1e18L + 5; long long arraySum(int L, int R) { long long ans = 0; for (int i = L; i <= R; i++) ans += slimes[i]; return ans; } int main() { int nSlimes; long long slimeValue; cin >> nSlimes; for (int i = 0; i < nSlimes; i++) { cin >> slimeValue; slimes[i] = slimeValue; } for (int i = nSlimes - 1; i >= 0; i--) { for (int j = i; j < nSlimes; j++) { if (i == j) dp[i][j] = 0; // The cost to combine a slime to itself is none else { dp[i][j] = inf; for (int k = i; k < j; k++) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + arraySum(i, j)); } } } cout << dp[0][nSlimes - 1]; }
replace
39
40
39
40
0
p03173
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; long long dp[409][409]; long long N, A[409], V[409][409]; long long dfs(long long L, long long R) { if (L > R) return 0; if (L == R) return A[L]; if (dp[L][R] != 0) return dp[L][R]; long long minx = (1LL << 60); for (int i = L + 1; i <= R; i++) { minx = min(minx, V[L][R] + dfs(L, i - 1) + dfs(i, R)); } return minx; } int main() { cin >> N; for (int i = 1; i <= N; i++) cin >> A[i]; for (int i = 1; i <= N; i++) { long long sum = 0; for (int j = i; j <= N; j++) { sum += A[j]; V[i][j] = sum; } } cout << dfs(1, N) - V[1][N] << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; long long dp[409][409]; long long N, A[409], V[409][409]; long long dfs(long long L, long long R) { if (L > R) return 0; if (L == R) return A[L]; if (dp[L][R] != 0) return dp[L][R]; long long minx = (1LL << 60); for (int i = L + 1; i <= R; i++) { minx = min(minx, V[L][R] + dfs(L, i - 1) + dfs(i, R)); } dp[L][R] = minx; return minx; } int main() { cin >> N; for (int i = 1; i <= N; i++) cin >> A[i]; for (int i = 1; i <= N; i++) { long long sum = 0; for (int j = i; j <= N; j++) { sum += A[j]; V[i][j] = sum; } } cout << dfs(1, N) - V[1][N] << endl; return 0; }
insert
19
19
19
20
TLE
p03173
C++
Time Limit Exceeded
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #define gc(x) \ do { \ while ((x = getchar()) < '0') \ ; \ for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0') \ ; \ } while (0) #define ms(a, b) memset(a, b, sizeof(a)); char _; using namespace std; typedef long long ll; const ll INF = 0x3f3f3f; ll dp[400][400]; ll pfsum[401]; int arr[400]; ll mincost(int l, int r) { if (dp[l][r] >= INF) { if (l > r) { dp[l][r] = INF; } else if (l == r) { dp[l][r] = 0; } else { for (int i = l; i < r; ++i) { dp[l][r] = min(dp[l][r], mincost(l, i) + mincost(i + 1, r) + pfsum[r + 1] - pfsum[l]); } } } return dp[l][r]; } int main() { ms(dp, INF); pfsum[0] = 0; int n; gc(n); for (int i = 0; i < n; ++i) { int a; gc(a); pfsum[i + 1] = pfsum[i] + a; arr[i] = a; } printf("%llu\n", mincost(0, n - 1)); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #define gc(x) \ do { \ while ((x = getchar()) < '0') \ ; \ for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0') \ ; \ } while (0) #define ms(a, b) memset(a, b, sizeof(a)); char _; using namespace std; typedef long long ll; const ll INF = 0x3f3f3f; ll dp[400][400]; ll pfsum[401]; int arr[400]; ll mincost(int l, int r) { if (dp[l][r] >= INF) { if (l > r) { dp[l][r] = INF; } else if (l == r) { dp[l][r] = 0; } else { for (int i = l; i < r; ++i) { dp[l][r] = min(dp[l][r], mincost(l, i) + mincost(i + 1, r) + pfsum[r + 1] - pfsum[l]); } } } return dp[l][r]; } int main() { ms(dp, INF); pfsum[0] = 0; int n; gc(n); for (int i = 0; i < n; ++i) { int a; gc(a); pfsum[i + 1] = pfsum[i] + a; arr[i] = a; } for (int l = n - 1; l >= 0; --l) { for (int r = l; r < n; ++r) { if (l == r) { dp[l][r] = 0; } else { for (int i = l; i < r; ++i) { dp[l][r] = min(dp[l][r], dp[l][i] + dp[i + 1][r] + pfsum[r + 1] - pfsum[l]); } } } } printf("%llu\n", dp[0][n - 1]); return 0; }
replace
47
48
47
60
TLE
p03173
C++
Runtime Error
/*--ILRS-- sr4saurabh */ #include <bits/stdc++.h> using namespace std; typedef long long int ll; #define pb push_back #define popb pop_back #define mp make_pair #define ve vector #define vii vector<int> #define vll vector<ll> #define pii pair<int, int> #define pll pair<ll, ll> #define vpl vector<pll> #define fi first #define sz size #define len length #define se second // const ll mod=998244353; const ll mod = 1000000000 + 7; const ll N = 10000000 + 6; #define M_PI 3.14159265358979323846 int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("INPUT.txt", "r", stdin); freopen("OUTPUT.txt", "w", stdout); #endif int n; cin >> n; ll arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; ll dp[n + 1][n + 1]; for (int l = n - 1; l >= 0; l--) { for (int r = l; r < n; r++) { dp[l][r] = 1e18l + 5; ll sum = 0; for (int j = l; j <= r; j++) sum = sum + arr[j]; if (l == r) dp[l][r] = 0; else { for (int i = l; i <= r - 1; i++) { dp[l][r] = min(dp[l][r], dp[l][i] + dp[i + 1][r] + sum); } } } } cout << dp[0][n - 1]; return 0; }
/*--ILRS-- sr4saurabh */ #include <bits/stdc++.h> using namespace std; typedef long long int ll; #define pb push_back #define popb pop_back #define mp make_pair #define ve vector #define vii vector<int> #define vll vector<ll> #define pii pair<int, int> #define pll pair<ll, ll> #define vpl vector<pll> #define fi first #define sz size #define len length #define se second // const ll mod=998244353; const ll mod = 1000000000 + 7; const ll N = 10000000 + 6; #define M_PI 3.14159265358979323846 int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); /** #ifndef ONLINE_JUDGE freopen ("INPUT.txt" , "r" , stdin); freopen ("OUTPUT.txt", "w" , stdout); #endif**/ int n; cin >> n; ll arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; ll dp[n + 1][n + 1]; for (int l = n - 1; l >= 0; l--) { for (int r = l; r < n; r++) { dp[l][r] = 1e18l + 5; ll sum = 0; for (int j = l; j <= r; j++) sum = sum + arr[j]; if (l == r) dp[l][r] = 0; else { for (int i = l; i <= r - 1; i++) { dp[l][r] = min(dp[l][r], dp[l][i] + dp[i + 1][r] + sum); } } } } cout << dp[0][n - 1]; return 0; }
replace
25
29
25
29
-11
p03173
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdlib> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; struct edge { ll to; ll cost; }; using graph = vector<vector<edge>>; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep2(i, n, m) for (ll i = n; i <= m; i++) #define rep3(i, n, m) for (ll i = n; i >= m; i--) #define pb push_back #define eb emplace_back #define ppb pop_back #define mpa make_pair #define fi first #define se second #define set20 cout << fixed << setprecision(20); const ll INF = 1e18; inline void chmax(ll &a, ll b) { a = max(a, b); } inline void chmin(ll &a, ll b) { a = min(a, b); } double pi = acos(-1); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll n; vector<ll> A(n); bool flag[405][405]; ll dp[405][405]; ll f(ll a, ll b) { if (flag[a][b]) return dp[a][b]; else { if (a == b) { flag[a][b] = 0; dp[a][b] = 0; return 0; } else if (a == b - 1) { flag[a][b] = 1; dp[a][b] = A[a] + A[b]; return A[a] + A[b]; } else { ll ans = INF; rep2(i, a, b - 1) { chmin(ans, f(a, i) + f(i + 1, b)); } rep2(i, a, b) ans += A[i]; flag[a][b] = 1; dp[a][b] = ans; return ans; } } } int main() { cin >> n; rep(i, n) A[i] = 0; rep(i, n) { cin >> A[i]; } rep(i, 405) { rep(j, 405) { flag[i][j] = 0; dp[i][j] = 0; } } cout << f(0, n - 1) << endl; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdlib> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; struct edge { ll to; ll cost; }; using graph = vector<vector<edge>>; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep2(i, n, m) for (ll i = n; i <= m; i++) #define rep3(i, n, m) for (ll i = n; i >= m; i--) #define pb push_back #define eb emplace_back #define ppb pop_back #define mpa make_pair #define fi first #define se second #define set20 cout << fixed << setprecision(20); const ll INF = 1e18; inline void chmax(ll &a, ll b) { a = max(a, b); } inline void chmin(ll &a, ll b) { a = min(a, b); } double pi = acos(-1); ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll n; vector<ll> A(n); bool flag[405][405]; ll dp[405][405]; ll f(ll a, ll b) { if (flag[a][b]) return dp[a][b]; else { if (a == b) { flag[a][b] = 0; dp[a][b] = 0; return 0; } else if (a == b - 1) { flag[a][b] = 1; dp[a][b] = A[a] + A[b]; return A[a] + A[b]; } else { ll ans = INF; rep2(i, a, b - 1) { chmin(ans, f(a, i) + f(i + 1, b)); } rep2(i, a, b) ans += A[i]; flag[a][b] = 1; dp[a][b] = ans; return ans; } } } int main() { cin >> n; A.assign(n, 0); rep(i, n) { cin >> A[i]; } rep(i, 405) { rep(j, 405) { flag[i][j] = 0; dp[i][j] = 0; } } cout << f(0, n - 1) << endl; return 0; }
replace
59
60
59
60
-11
p03173
C++
Runtime Error
/* https://atcoder.jp/contests/dp/tasks/dp_n */ #include <bits/stdc++.h> using namespace std; long long dp[401][401]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; long long a[401] = {0}; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; } memset(dp, 0x7f, sizeof dp[0][0] * 401 * 401); // for(int i=0;i<n;i++) { // for(int j=0;j<n;j++) { // cerr << dp[i][j] << ' '; // } // cerr << '\n'; // } for (int l = 0; l < n; l++) { for (int i = 0; i < n; i++) { if (l == 0) dp[i][i] = 0; for (int j = i; j < min(i + l, n - 1); j++) { dp[i][i + l] = min(dp[i][i + l], dp[i][j] + dp[j + 1][i + l] + a[i + l + 1] - a[i]); } } } cout << dp[0][n - 1] << '\n'; return 0; }
/* https://atcoder.jp/contests/dp/tasks/dp_n */ #include <bits/stdc++.h> using namespace std; long long dp[401][401]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; long long a[401] = {0}; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; } memset(dp, 0x7f, sizeof dp[0][0] * 401 * 401); // for(int i=0;i<n;i++) { // for(int j=0;j<n;j++) { // cerr << dp[i][j] << ' '; // } // cerr << '\n'; // } for (int l = 0; l < n; l++) { for (int i = 0; i < n; i++) { if (l == 0) dp[i][i] = 0; int limit = min(n - 1, i + l); for (int j = i; j < limit; j++) { dp[i][limit] = min(dp[i][limit], dp[i][j] + dp[j + 1][limit] + a[limit + 1] - a[i]); } } } cout << dp[0][n - 1] << '\n'; return 0; }
replace
28
31
28
32
0
p03173
C++
Time Limit Exceeded
#include <iostream> using namespace std; int a[401]; long long dp[401][401]; long long pref[401]; int main() { long long n, l; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } pref[0] = a[0]; for (int i = 1; i < n; i++) { pref[i] = pref[i - 1] + a[i]; for (int len = 1; len <= n; len++) { for (l = 0; l < n; l++) { int r = l + len - 1; if (r == n) break; else if (len == 1) dp[l][l] = 0; else { dp[l][r] = 2e18; for (int mid = l; mid < r; mid++) { if (l > 0) { long long calc = dp[l][mid] + dp[mid + 1][r] + pref[r] - pref[l - 1]; dp[l][r] = min(dp[l][r], calc); } else { long long calc = dp[l][mid] + dp[mid + 1][r] + pref[r]; dp[l][r] = min(dp[l][r], calc); } } } } } } cout << dp[0][n - 1]; }
#include <iostream> using namespace std; int a[401]; long long dp[401][401]; long long pref[401]; int main() { long long n, l; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } pref[0] = a[0]; for (int i = 1; i < n; i++) { pref[i] = pref[i - 1] + a[i]; } for (int len = 1; len <= n; len++) { for (l = 0; l < n; l++) { int r = l + len - 1; if (r == n) break; else if (len == 1) dp[l][l] = 0; else { dp[l][r] = 2e18; for (int mid = l; mid < r; mid++) { if (l > 0) { long long calc = dp[l][mid] + dp[mid + 1][r] + pref[r] - pref[l - 1]; dp[l][r] = min(dp[l][r], calc); } else { long long calc = dp[l][mid] + dp[mid + 1][r] + pref[r]; dp[l][r] = min(dp[l][r], calc); } } } } } cout << dp[0][n - 1]; }
replace
14
32
14
32
TLE
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; #define rrep(i, m, n) for (int(i) = (int)(m); (i) >= (int)(n); (i)--) #define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++) #define REP(i, n) rep(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() #define M_PI 3.1415926535897932L; typedef pair<ll, ll> P; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 60; constexpr long double EPS = 1e-10; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string &s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge( from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph &G, ll s, ll t) // O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = ((i != j) ? INF : 0); } } REP(i, G.size()) { REP(j, G[i].size()) { chmin(d[i][G[i][j].to], G[i][j].cap); } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, vector<int> &order) { // トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; void visit(const Graph &g, int v, Matrix &scc, stack<ll> &S, Array &inS, Array &low, Array &num, int &time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; FOR(e, g[v]) { int w = e->to; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(Array()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const Graph &g, Matrix &scc) { // 強連結成分分解 O(E+V) const int n = g.size(); Array num(n), low(n); stack<ll> S; Array inS(n); int time = 0; REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { // xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } ll size(int x) { // xの集合のサイズを返す return -data[root(x)]; } ll numSet() { // 集合の数を返す return num; } }; class SumSegTree { private: ll _sum(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return 0; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return s1 + s2; } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) SumSegTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, 0); } // 場所i(0-indexed)にxを足す void add(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] += x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] += x; } } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: ll _find(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return INF; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return min(s1, s2); } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) RmqTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, INF); } // 場所i(0-indexed)をxにする void update(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] = x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } // 区間[a,b)の最小値。ノードk=[l,r)に着目している。 ll find(ll a, ll b) { return _find(a, b, 0, 0, n); } }; // 約数求める //約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void divisor_prime(ll n, vector<P> &ret) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({i, 0}); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1) ret.push_back({n, 1}); } ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } // nCrとか class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } // if n >= mod use lucas ll nCr(ll n, ll r) { if (n < r) return 0; if (n < mod) return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; ll ret = 1; while (n || r) { ll _n = n % mod, _r = r % mod; n /= mod; r /= mod; (ret *= nCr(_n, _r)) %= mod; } return ret; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll _n, ll _mod) { mod = _mod; ll n = min(_n + 1, mod); fact.resize(n); fact[0] = 1; REP(i, n - 1) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n); inv[n - 1] = mod_inv(fact[n - 1]); for (int i = n - 1; i > 0; i--) { inv[i - 1] = inv[i] * i % mod; } } }; ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } // gcd ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll popcount(ll x) { x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F); x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF); x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF); x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF); return x; } ll dp[22][1 << 21]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; Matrix a(n, Array(n)); REP(i, n) REP(j, n) cin >> a[i][j]; dp[0][0] = 1; REP(i, n) { REP(j, 1 << n) { if (dp[i][j] == 0) continue; REP(k, n) { if (a[i][k] == 1) (dp[i + 1][j | (1 << k)] += dp[i][j]) %= MOD; } } } cout << dp[n][(1 << n) - 1] << "\n"; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; #define rrep(i, m, n) for (int(i) = (int)(m); (i) >= (int)(n); (i)--) #define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++) #define REP(i, n) rep(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() #define M_PI 3.1415926535897932L; typedef pair<ll, ll> P; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 60; constexpr long double EPS = 1e-10; typedef vector<ll> Array; typedef vector<Array> Matrix; string operator*(const string &s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge( from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } // 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ ll max_flow(Graph &G, ll s, ll t) // O(V(V+E)) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = ((i != j) ? INF : 0); } } REP(i, G.size()) { REP(j, G[i].size()) { chmin(d[i][G[i][j].to], G[i][j].cap); } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, vector<int> &order) { // トポロジカルソートO(E+V) int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; void visit(const Graph &g, int v, Matrix &scc, stack<ll> &S, Array &inS, Array &low, Array &num, int &time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; FOR(e, g[v]) { int w = e->to; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(Array()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const Graph &g, Matrix &scc) { // 強連結成分分解 O(E+V) const int n = g.size(); Array num(n), low(n); stack<ll> S; Array inS(n); int time = 0; REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { // xとyの集合を統合する x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } ll size(int x) { // xの集合のサイズを返す return -data[root(x)]; } ll numSet() { // 集合の数を返す return num; } }; class SumSegTree { private: ll _sum(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return 0; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return s1 + s2; } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) SumSegTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, 0); } // 場所i(0-indexed)にxを足す void add(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] += x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] += x; } } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: ll _find(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return INF; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { ll s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return min(s1, s2); } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) RmqTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, INF); } // 場所i(0-indexed)をxにする void update(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] = x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } // 区間[a,b)の最小値。ノードk=[l,r)に着目している。 ll find(ll a, ll b) { return _find(a, b, 0, 0, n); } }; // 約数求める //約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void divisor_prime(ll n, vector<P> &ret) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({i, 0}); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1) ret.push_back({n, 1}); } ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); } // nCrとか class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } // if n >= mod use lucas ll nCr(ll n, ll r) { if (n < r) return 0; if (n < mod) return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; ll ret = 1; while (n || r) { ll _n = n % mod, _r = r % mod; n /= mod; r /= mod; (ret *= nCr(_n, _r)) %= mod; } return ret; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { return nCr(r + n - 1, r); } Combination(ll _n, ll _mod) { mod = _mod; ll n = min(_n + 1, mod); fact.resize(n); fact[0] = 1; REP(i, n - 1) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n); inv[n - 1] = mod_inv(fact[n - 1]); for (int i = n - 1; i > 0; i--) { inv[i - 1] = inv[i] * i % mod; } } }; ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } // gcd ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll popcount(ll x) { x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F); x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF); x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF); x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF); return x; } ll dp[22][1 << 21]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin >> n; Matrix a(n, Array(n)); REP(i, n) REP(j, n) cin >> a[i][j]; dp[0][0] = 1; REP(i, n) { REP(j, 1 << n) { if (dp[i][j] == 0 || popcount(j) != i) continue; REP(k, n) { if (a[i][k] == 1) (dp[i + 1][j | (1 << k)] += dp[i][j]) %= MOD; } } } cout << dp[n][(1 << n) - 1] << "\n"; return 0; }
replace
517
518
517
518
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ll long long int #define mp make_pair #define S second #define F first int mod = 1e9 + 7; #define input_from_file freopen("input.txt", "r", stdin); ll dp[1 << 22][25]; ll allmask; ll recursion(ll &n, ll mask, vector<vector<int>> &vec, ll level) { if (level >= n) { if (mask == allmask) return 1; return 0; } if (dp[mask][level] != -1) return dp[mask][level]; ll val = 0; for (int i = 0; i < n; i++) { if (vec[i][level] == 1 && (mask & (1 << i)) == 0) { val = (val % mod + recursion(n, mask | (1 << i), vec, level + 1) % mod) % mod; } } dp[mask][level] = val; return dp[mask][level]; } signed main() { //------------ SEE BELOW ARTICLE--------------- // https://codeforces.com/blog/entry/64250?#comment-484569 input_from_file; ll n; cin >> n; vector<vector<int>> vec(n, vector<int>(n, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> vec[i][j]; } allmask = (1 << n) - 1; memset(dp, -1, sizeof dp); cout << recursion(n, 0, vec, 0); return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ll long long int #define mp make_pair #define S second #define F first int mod = 1e9 + 7; #define input_from_file freopen("input.txt", "r", stdin); ll dp[1 << 22][25]; ll allmask; ll recursion(ll &n, ll mask, vector<vector<int>> &vec, ll level) { if (level >= n) { if (mask == allmask) return 1; return 0; } if (dp[mask][level] != -1) return dp[mask][level]; ll val = 0; for (int i = 0; i < n; i++) { if (vec[i][level] == 1 && (mask & (1 << i)) == 0) { val = (val % mod + recursion(n, mask | (1 << i), vec, level + 1) % mod) % mod; } } dp[mask][level] = val; return dp[mask][level]; } signed main() { //------------ SEE BELOW ARTICLE--------------- // https://codeforces.com/blog/entry/64250?#comment-484569 // input_from_file; ll n; cin >> n; vector<vector<int>> vec(n, vector<int>(n, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> vec[i][j]; } allmask = (1 << n) - 1; memset(dp, -1, sizeof dp); cout << recursion(n, 0, vec, 0); return 0; }
replace
33
34
33
34
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<double> vd; typedef vector<ll> vll; typedef vector<vector<int>> vvi; typedef vector<vector<long long>> vvll; typedef vector<vd> vvd; typedef vector<long long> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pair<int, int>> vpii; const int MOD = 1e9 + 7; int main() { int n; cin >> n; vvi a(n, vi(n)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; vvi state(1 << n, vi(n, 0)); // memset(state, 0, sizeof(state)); for (int i = 0; i < n; i++) if (a[i][0]) state[1 << i][0] = 1; for (int i = 0; i < (1 << n); i++) for (int j = 1; j < n; j++) for (int k = 0; k < n; k++) if (((1 << k) & i) && a[k][j]) state[i][j] = (state[i][j] + state[i ^ (1 << k)][j - 1]) % MOD; cout << state[(1 << n) - 1][n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<double> vd; typedef vector<ll> vll; typedef vector<vector<int>> vvi; typedef vector<vector<long long>> vvll; typedef vector<vd> vvd; typedef vector<long long> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pair<int, int>> vpii; const int MOD = 1e9 + 7; int main() { int n; cin >> n; vvi a(n, vi(n)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; vvi state(1 << n, vi(n, 0)); // memset(state, 0, sizeof(state)); for (int i = 0; i < n; i++) if (a[i][0]) state[1 << i][0] = 1; for (int i = 0; i < (1 << n); i++) for (int j = 1; j < n; j++) if (j == __builtin_popcount(i) - 1) for (int k = 0; k < n; k++) if (((1 << k) & i) && a[k][j]) state[i][j] = (state[i][j] + state[i ^ (1 << k)][j - 1]) % MOD; cout << state[(1 << n) - 1][n - 1] << endl; }
replace
37
40
37
41
TLE
p03174
C++
Time Limit Exceeded
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define MuhammedAly \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define all(x) (x).begin(), (x).end() #define allr(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back #define popc(x) __builtin_popcount(x) #define LLMX 1e18 #define NotinRange(n, m) i < 0 || i >= (n) || j < 0 || j >= (m) #define mem(x, y) memset((x), (y), sizeof(x)); #define sz(a) (int)(a).size() #define inFile(s) freopen((s), "r", stdin) #define outFile(s) freopen((s), "w", stdout) #define biEdge(v, a, b) (v)[(a)].pb((b)), (v)[(b)].pb((a)) #define pi (2 * acos(0)) #define X real() #define Y imag() #define cross(a, b) ((conj(a) * (b)).Y) #define dot(a, b) ((conj(a) * (b)).X) #define vec(a, b) ((b) - (a)) #define ll long long typedef long double ld; typedef complex<double> point; typedef tuple<int, int, int> line; typedef pair<point, point> segment; typedef vector<point> polygon; const int N = 21, M = 100 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7, base = 131, mod2 = 1000136437, mod3 = 998244353; const double EPS = 1e-9; int n, memo[23][(1 << 21) + 2]; bool comp[23][23]; int solve(int i = 0, int mask = 0) { if (mask == (1 << n) - 1) return 1; if (i == n) return 0; int &ret = memo[i][mask]; if (~ret) return ret; ret = solve(i + 1, mask); for (int j = 0; j < n; j++) if (comp[i][j] && !((mask >> j) & 1)) ret = (ret + solve(i + 1, (mask | (1 << j)))) % mod; return ret % mod; } int main() { MuhammedAly mem(memo, -1); cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> comp[i][j]; cout << solve(); return 0; }
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define MuhammedAly \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define all(x) (x).begin(), (x).end() #define allr(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back #define popc(x) __builtin_popcount(x) #define LLMX 1e18 #define NotinRange(n, m) i < 0 || i >= (n) || j < 0 || j >= (m) #define mem(x, y) memset((x), (y), sizeof(x)); #define sz(a) (int)(a).size() #define inFile(s) freopen((s), "r", stdin) #define outFile(s) freopen((s), "w", stdout) #define biEdge(v, a, b) (v)[(a)].pb((b)), (v)[(b)].pb((a)) #define pi (2 * acos(0)) #define X real() #define Y imag() #define cross(a, b) ((conj(a) * (b)).Y) #define dot(a, b) ((conj(a) * (b)).X) #define vec(a, b) ((b) - (a)) #define ll long long typedef long double ld; typedef complex<double> point; typedef tuple<int, int, int> line; typedef pair<point, point> segment; typedef vector<point> polygon; const int N = 21, M = 100 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7, base = 131, mod2 = 1000136437, mod3 = 998244353; const double EPS = 1e-9; int n, memo[23][(1 << 21) + 2]; bool comp[23][23]; int solve(int i = 0, int mask = 0) { if (mask == (1 << n) - 1) return 1; if (i == n) return 0; int &ret = memo[i][mask]; if (~ret) return ret; ret = 0; for (int j = 0; j < n; j++) if (comp[i][j] && !((mask >> j) & 1)) ret = (ret + solve(i + 1, (mask | (1 << j)))) % mod; return ret % mod; } int main() { MuhammedAly mem(memo, -1); cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> comp[i][j]; cout << solve(); return 0; }
replace
46
47
46
47
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using vll = std::vector<long long>; constexpr long long mod{1000000007}; int n; bool edge[21][21]{}; std::vector<vll> mf; long long rc(int, int); int main() { scanf("%d", &n); for (int i{}; i < n; i++) for (int j{}; j < n; j++) { int a; scanf("%d", &a); edge[i][j] = a == 1; } mf.resize(n + 1, vll(1 << n, -1)); mf[n][(1 << n) - 1] = 1; for (int i{}; i + 1 < 1 << n; i++) mf[n][i - 1] = 0; printf("%lld\n", rc(0, 0)); return 0; } long long rc(int male, int set) { long long &combi{mf[male][set]}; if (combi >= 0) return combi; combi = 0; for (int female{}; female < n; female++) { if (!edge[male][female] || ((1 << female) & set)) continue; combi += rc(male + 1, set | (1 << female)); combi %= mod; } return combi; }
#include <bits/stdc++.h> using vll = std::vector<long long>; constexpr long long mod{1000000007}; int n; bool edge[21][21]{}; std::vector<vll> mf; long long rc(int, int); int main() { scanf("%d", &n); for (int i{}; i < n; i++) for (int j{}; j < n; j++) { int a; scanf("%d", &a); edge[i][j] = a == 1; } mf.resize(n + 1, vll(1 << n, -1)); mf[n][(1 << n) - 1] = 1; for (int i{}; i + 1 < 1 << n; i++) mf[n][i] = 0; printf("%lld\n", rc(0, 0)); return 0; } long long rc(int male, int set) { long long &combi{mf[male][set]}; if (combi >= 0) return combi; combi = 0; for (int female{}; female < n; female++) { if (!edge[male][female] || ((1 << female) & set)) continue; combi += rc(male + 1, set | (1 << female)); combi %= mod; } return combi; }
replace
23
24
23
24
-6
free(): invalid pointer
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace ::std; #define all(x) (x).begin(), (x).end() typedef long long ll; typedef array<int, 3> tri; typedef long double ld; template <class T> istream &operator>>(istream &I, vector<T> &v) { for (T &e : v) I >> e; return I; } template <class T> ostream &operator<<(ostream &O, const vector<T> &v) { for (const T &e : v) O << e << ' '; return O; } const int mod = 1e9 + 7; void _main() { int n; cin >> n; vector<vector<int>> match(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> match[i][j]; } } vector<int> dp(1 << n); dp[0] = 1; for (int man = 0; man < n; man++) { vector<int> new_dp(1 << n); for (int mask = 0; mask < (1 << n); mask++) { for (int woman = 0; woman < n; woman++) { if (1 << woman & mask && match[man][woman]) { (new_dp[mask] += dp[mask ^ (1 << woman)]) %= mod; } } } dp = new_dp; } cout << dp.back(); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); int _t = 1; // cin >> _t; while (_t--) _main(); return 0; }
#include <bits/stdc++.h> using namespace ::std; #define all(x) (x).begin(), (x).end() typedef long long ll; typedef array<int, 3> tri; typedef long double ld; template <class T> istream &operator>>(istream &I, vector<T> &v) { for (T &e : v) I >> e; return I; } template <class T> ostream &operator<<(ostream &O, const vector<T> &v) { for (const T &e : v) O << e << ' '; return O; } const int mod = 1e9 + 7; void _main() { int n; cin >> n; vector<vector<int>> match(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> match[i][j]; } } vector<int> dp(1 << n); dp[0] = 1; for (int man = 0; man < n; man++) { vector<int> new_dp(1 << n); for (int mask = 0; mask < (1 << n); mask++) { if (__builtin_popcount(mask) != man + 1) continue; for (int woman = 0; woman < n; woman++) { if (1 << woman & mask && match[man][woman]) { (new_dp[mask] += dp[mask ^ (1 << woman)]) %= mod; } } } dp = new_dp; } cout << dp.back(); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); int _t = 1; // cin >> _t; while (_t--) _main(); return 0; }
insert
36
36
36
38
TLE
p03174
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MAXN 100100 const ll inf = (ll)1e10; const ll MOD = (ll)1e9 + 7; ll dp[21][1 << 21]; bool chk(int pos, int mask) { return mask & (1 << pos); } int on(int pos, int mask) { return mask | (1 << pos); } int n, done; int adj[21][21]; ll solve(ll pos, ll mask) { if (dp[pos][mask] != -1) return dp[pos][mask]; if (pos == n) { if (mask == done - 1) { return 1; } else return 0; } dp[pos][mask] = 0; for (int j = 0; j < n; j++) { if (adj[pos][j] && !(mask & (1 << j))) dp[pos][mask] = (dp[pos][mask] + solve(pos + 1, mask | (1 << j))) % MOD; } return dp[pos][mask]; } int main() { #ifdef __APPLE__ // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> adj[i][j]; } } done = (1 << n); memset(dp, -1, sizeof(dp)); cout << solve(0, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MAXN 100100 const ll inf = (ll)1e10; const ll MOD = (ll)1e9 + 7; ll dp[22][1 << 22]; bool chk(int pos, int mask) { return mask & (1 << pos); } int on(int pos, int mask) { return mask | (1 << pos); } int n, done; int adj[21][21]; ll solve(ll pos, ll mask) { if (dp[pos][mask] != -1) return dp[pos][mask]; if (pos == n) { if (mask == done - 1) { return 1; } else return 0; } dp[pos][mask] = 0; for (int j = 0; j < n; j++) { if (adj[pos][j] && !(mask & (1 << j))) dp[pos][mask] = (dp[pos][mask] + solve(pos + 1, mask | (1 << j))) % MOD; } return dp[pos][mask]; } int main() { #ifdef __APPLE__ // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> adj[i][j]; } } done = (1 << n); memset(dp, -1, sizeof(dp)); cout << solve(0, 0) << endl; return 0; }
replace
7
8
7
8
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long #define MAX 2999 ll mod = pow(10, 9) + 7; ll dp[22][1 << 21]; using namespace std; int main() { int n; cin >> n; int A[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> A[i][j]; } for (int i = 0; i <= n; i++) { for (int j = 0; j < (1 << n); j++) dp[i][j] = 0; } dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int mask = 0; mask < int(1 << n); mask++) { for (int j = 0; j < n; j++) { if (A[i - 1][j] == 1 && int(mask & (1 << j)) != 0) { dp[i][mask] = (dp[i][mask] % mod + dp[i - 1][int(mask ^ (1 << j))] % mod) % mod; } } } } cout << dp[n][(1 << n) - 1]; }
#include <bits/stdc++.h> #define ll long long #define MAX 2999 ll mod = pow(10, 9) + 7; ll dp[22][1 << 21]; using namespace std; int main() { int n; cin >> n; int A[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> A[i][j]; } for (int i = 0; i <= n; i++) { for (int j = 0; j < (1 << n); j++) dp[i][j] = 0; } dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int mask = 0; mask < int(1 << n); mask++) { int k = __builtin_popcount(mask); if (k != i) continue; for (int j = 0; j < n; j++) { if (A[i - 1][j] == 1 && int(mask & (1 << j)) != 0) { dp[i][mask] = (dp[i][mask] % mod + dp[i - 1][int(mask ^ (1 << j))] % mod) % mod; } } } } cout << dp[n][(1 << n) - 1]; }
insert
26
26
26
29
TLE
p03174
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _; #define MOD 1000000007 int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n, 0)); for (int i = 0; i <= n - 1; i++) for (int j = 0; j <= n - 1; j++) cin >> a[i][j]; vector<vector<long long>> dp(n, vector<long long>((1 << n), 0)); long long ans = 0; for (int i = 0; i <= n - 1; i++) { for (int m = 0; m <= (1 << n) - 1; m++) { for (int j = 0; j <= n - 1; j++) { if (a[i][j] == 1 and (m & (1 << j)) != 0) { if (i == 0) dp[i][m] = 1; else { dp[i][m] += dp[i - 1][m - (1 << j)]; if (dp[i][m] >= MOD) dp[i][m] %= MOD; } } } if (i == n - 1) { ans += dp[i][m]; if (ans >= MOD) ans %= MOD; } } } cout << ans; return 0; }
#include <iostream> #include <vector> using namespace std; struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _; #define MOD 1000000007 int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n, 0)); for (int i = 0; i <= n - 1; i++) for (int j = 0; j <= n - 1; j++) cin >> a[i][j]; vector<vector<long long>> dp(n, vector<long long>((1 << n), 0)); long long ans = 0; for (int i = 0; i <= n - 1; i++) { for (int m = 0; m <= (1 << n) - 1; m++) { if (__builtin_popcount(m) != i + 1) continue; for (int j = 0; j <= n - 1; j++) { if (a[i][j] == 1 and (m & (1 << j)) != 0) { if (i == 0) dp[i][m] = 1; else { dp[i][m] += dp[i - 1][m - (1 << j)]; if (dp[i][m] >= MOD) dp[i][m] %= MOD; } } } if (i == n - 1) { ans += dp[i][m]; if (ans >= MOD) ans %= MOD; } } } cout << ans; return 0; }
insert
26
26
26
28
TLE
p03174
C++
Runtime Error
// In The Name Of God #include <bits/stdc++.h> #define F first #define S second #define mp make_pair #define L(x) (2 * (x)) #define R(x) ((2 * (x)) + 1) #define pii pair<int, int> #define pb push_back using namespace std; const int N = 4e2 + 5, Mod = 1e9 + 7, P = 727, Lg = 21, TOF = 1900; int Dp[Lg][(1 << Lg)], A[Lg][Lg]; vector<int> V[Lg]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> A[i][j]; for (int j = 1; j < (1 << n); j++) V[__builtin_popcount(j)].pb(j); for (int i = 0; i < n; i++) { for (int j = 0; j < (int)V[i + 1].size(); j++) { int mask = V[i + 1][j], cur = V[i + 1][j]; while (cur) { int ct = __builtin_ctz(cur); cur ^= (1 << ct); if (A[i][ct]) { if (i) Dp[i][mask] = (Dp[i][mask] + Dp[i - 1][mask ^ (1 << ct)]) % Mod; else Dp[i][mask] = 1; } } } } cout << Dp[n - 1][(1 << n) - 1]; return 0; }
// In The Name Of God #include <bits/stdc++.h> #define F first #define S second #define mp make_pair #define L(x) (2 * (x)) #define R(x) ((2 * (x)) + 1) #define pii pair<int, int> #define pb push_back using namespace std; const int N = 4e2 + 5, Mod = 1e9 + 7, P = 727, Lg = 22, TOF = 1900; int Dp[Lg][(1 << Lg)], A[Lg][Lg]; vector<int> V[Lg]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> A[i][j]; for (int j = 1; j < (1 << n); j++) V[__builtin_popcount(j)].pb(j); for (int i = 0; i < n; i++) { for (int j = 0; j < (int)V[i + 1].size(); j++) { int mask = V[i + 1][j], cur = V[i + 1][j]; while (cur) { int ct = __builtin_ctz(cur); cur ^= (1 << ct); if (A[i][ct]) { if (i) Dp[i][mask] = (Dp[i][mask] + Dp[i - 1][mask ^ (1 << ct)]) % Mod; else Dp[i][mask] = 1; } } } } cout << Dp[n - 1][(1 << n) - 1]; return 0; }
replace
10
11
10
11
-11
p03174
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define fi first #define se second #define mp make_pair #define itrfor(itr, A) for (auto itr = A.begin(); itr != A.end(); itr++) template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>; typedef long long llong; char moji[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char moji2[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; char moji3[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; #define Sort(a) sort(a.begin(), a.end()); #define Reverse(a) reverse(a.begin(), a.end()); #define print(a) cout << a << endl; #define MOD llong(1e9 + 7) #define MAX int(2 * 1e5 + 5) #define debug(x) cout << #x << " = " << (x) << endl; #define pi acos(-1.0) #define int llong #define INF llong(1e17) template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void myprint(int *A, int A_num) { REP(i, A_num) cout << A[i] << " "; cout << endl; } const int N_max = 22; const int n_MAX = 1024 * 1024 * 2 + 5; signed main() { int N; cin >> N; int A[N_max][N_max]; REP(i, N) REP(j, N) cin >> A[i][j]; int n = 1; int joutai[25]; REP(i, N) { joutai[i] = n; n *= 2; } int dp[2][n_MAX]; REP(j, 2) REP(i, n_MAX) dp[j][i] = 0; int woman_id = 0; while (1) { if (A[0][woman_id]) dp[1][joutai[woman_id]] = 1; woman_id++; if (woman_id == N) break; } FOR(man, 1, N) { FOR(j, 1, n) { REP(woman, N) { if (joutai[woman] & j) continue; if (A[man][woman] == 0) continue; dp[(man + 1) % 2][j + joutai[woman]] += dp[man % 2][j]; dp[(man + 1) % 2][j + joutai[woman]] %= MOD; } } } cout << dp[N % 2][n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define fi first #define se second #define mp make_pair #define itrfor(itr, A) for (auto itr = A.begin(); itr != A.end(); itr++) template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>; typedef long long llong; char moji[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char moji2[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; char moji3[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; #define Sort(a) sort(a.begin(), a.end()); #define Reverse(a) reverse(a.begin(), a.end()); #define print(a) cout << a << endl; #define MOD llong(1e9 + 7) #define MAX int(2 * 1e5 + 5) #define debug(x) cout << #x << " = " << (x) << endl; #define pi acos(-1.0) #define int llong #define INF llong(1e17) template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void myprint(int *A, int A_num) { REP(i, A_num) cout << A[i] << " "; cout << endl; } const int N_max = 22; const int n_MAX = 1024 * 1024 * 2 + 5; signed main() { int N; cin >> N; int A[N_max][N_max]; REP(i, N) REP(j, N) cin >> A[i][j]; int n = 1; int joutai[25]; REP(i, N) { joutai[i] = n; n *= 2; } int dp[2][n_MAX]; REP(j, 2) REP(i, n_MAX) dp[j][i] = 0; int woman_id = 0; while (1) { if (A[0][woman_id]) dp[1][joutai[woman_id]] = 1; woman_id++; if (woman_id == N) break; } FOR(man, 1, N) { FOR(j, 1, n) { if (dp[man % 2][j] == 0) continue; REP(woman, N) { if (joutai[woman] & j) continue; if (A[man][woman] == 0) continue; dp[(man + 1) % 2][j + joutai[woman]] += dp[man % 2][j]; dp[(man + 1) % 2][j + joutai[woman]] %= MOD; } } } cout << dp[N % 2][n - 1] << endl; }
insert
76
76
76
78
TLE
p03174
C++
Time Limit Exceeded
#include <iostream> using namespace std; typedef long long ll; ll dp[22][(1 << 21)]; int a[21][21]; const ll MOD = 1e9 + 7; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int s = 0; s < (1 << n); s++) { for (int j = 0; j < n; j++) { if (a[i][j] == 1 && !((s >> j) & 1)) { // cout << "i = "<<i<<" j = "<<j<<endl; dp[i + 1][s | (1 << j)] = (dp[i + 1][s | (1 << j)] + dp[i][s]) % MOD; } } } } /* for(int i = 0; i < n; i++) { for(int j = 0; j < (1 << n); j++) { cout << dp[i][j] << endl; } }*/ cout << dp[n][(1 << n) - 1] << endl; }
#include <iostream> using namespace std; typedef long long ll; ll dp[22][(1 << 21)]; int a[21][21]; const ll MOD = 1e9 + 7; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } dp[0][0] = 1; for (int i = 0; i < n; i++) { for (int s = 0; s < (1 << n); s++) { if (__builtin_popcount(s) != i) continue; for (int j = 0; j < n; j++) { if (a[i][j] == 1 && !((s >> j) & 1)) { // cout << "i = "<<i<<" j = "<<j<<endl; dp[i + 1][s | (1 << j)] = (dp[i + 1][s | (1 << j)] + dp[i][s]) % MOD; } } } } /* for(int i = 0; i < n; i++) { for(int j = 0; j < (1 << n); j++) { cout << dp[i][j] << endl; } }*/ cout << dp[n][(1 << n) - 1] << endl; }
insert
21
21
21
23
TLE
p03174
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; ll MOD = 1000000007; int main() { int N; cin >> N; vector<bitset<21>> a(N, 0); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { int t; cin >> t; if (t == 1) a[i].set(j); } } vector<vector<ll>> r(N + 1, vector<ll>((1 << N))); for (int i = 0; i <= N; i++) { r[i][0] = 1; } for (int i = 1; i <= N; i++) { for (int j = 1; j < (1 << N); j++) { for (int k = 0; k < N; k++) { if (a[i - 1].test(k) && ((j >> k) & 1)) { r[i][j] += r[i - 1][j ^ (1 << k)]; r[i][j] %= MOD; } } // cout << r[i][j] << " "; } // cout << endl; } cout << (r[N][(1 << N) - 1] + MOD) % MOD << endl; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; ll MOD = 1000000007; int main() { int N; cin >> N; vector<bitset<21>> a(N, 0); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { int t; cin >> t; if (t == 1) a[i].set(j); } } vector<vector<ll>> r(N + 1, vector<ll>((1 << N))); for (int i = 0; i <= N; i++) { r[i][0] = 1; } for (int i = 1; i <= N; i++) { for (int j = 1; j < (1 << N); j++) { if (__builtin_popcount(j) != i) continue; for (int k = 0; k < N; k++) { if (a[i - 1].test(k) && ((j >> k) & 1)) { r[i][j] += r[i - 1][j ^ (1 << k)]; r[i][j] %= MOD; } } // cout << r[i][j] << " "; } // cout << endl; } cout << (r[N][(1 << N) - 1] + MOD) % MOD << endl; return 0; }
insert
39
39
39
41
TLE