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
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; vector<int> h(N); for (int i = 0; i < N; i++) cin >> h[i]; vector<int> DP(N, 0); DP[1] = abs(h[1] - h[0]); int cost, min_cost; for (int i = 2; i < N; i++) { min_cost = DP[i - 1] + abs(h[i] - h[i - 1]); for (int j = 2; j <= K; j++) { if (i - j < 0) break; cost = DP[i - K] + abs(h[i] - h[i - K]); if (cost < min_cost) min_cost = cost; } DP[i] = min_cost; } cout << DP[N - 1] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; vector<int> h(N); for (int i = 0; i < N; i++) cin >> h[i]; vector<int> DP(N, 0); DP[1] = abs(h[1] - h[0]); int cost, min_cost; for (int i = 2; i < N; i++) { min_cost = DP[i - 1] + abs(h[i] - h[i - 1]); for (int j = 2; j <= K; j++) { if (i - j < 0) break; cost = DP[i - j] + abs(h[i] - h[i - j]); if (cost < min_cost) min_cost = cost; } DP[i] = min_cost; } cout << DP[N - 1] << endl; }
replace
17
18
17
18
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int j; cin >> j; vector<int> vec; int dp[n + 20]; for (int i = 0; i < n; i++) { int h; cin >> h; vec.push_back(h); dp[i] = 2000000000; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int k = 1; k <= j; k++) if (abs(vec[i] - vec[i + k]) + dp[i] < dp[i + k]) dp[i + k] = abs(vec[i] - vec[i + k]) + dp[i]; } printf("%d\n", dp[n - 1]); }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int j; cin >> j; vector<int> vec; int dp[n + 300]; for (int i = 0; i < n; i++) { int h; cin >> h; vec.push_back(h); dp[i] = 2000000000; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int k = 1; k <= j; k++) if (abs(vec[i] - vec[i + k]) + dp[i] < dp[i + k]) dp[i + k] = abs(vec[i] - vec[i + k]) + dp[i]; } printf("%d\n", dp[n - 1]); }
replace
10
11
10
11
0
p03161
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define int int64_t int MOD = 1000000007; int minCost(int *A, int N, int K, int *dp) { if (N == 0) return 0; if (N == 1) return abs(A[1] - A[0]); if (dp[N] != -1) return dp[N]; int ans = INT_MAX; for (int i = 1; i <= K; i++) ans = min(minCost(A, N - i, K, dp) + abs(A[N] - A[N - i]), ans); return dp[N] = ans; } int32_t main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N, K; cin >> N >> K; int *A = new int[N](); for (int i = 0; i < N; i++) cin >> A[i]; int *dp = new int[N](); for (int i = 0; i < N; i++) dp[i] = -1; int ans = minCost(A, N - 1, K, dp); cout << ans; delete[] dp; delete[] A; return 0; }
#include "bits/stdc++.h" using namespace std; #define int int64_t int MOD = 1000000007; int minCost(int *A, int N, int K, int *dp) { if (N < 0) return INT_MAX; if (N == 0) return 0; if (N == 1) return abs(A[1] - A[0]); if (dp[N] != -1) return dp[N]; int ans = INT_MAX; for (int i = 1; i <= K; i++) ans = min(minCost(A, N - i, K, dp) + abs(A[N] - A[N - i]), ans); return dp[N] = ans; } int32_t main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N, K; cin >> N >> K; int *A = new int[N](); for (int i = 0; i < N; i++) cin >> A[i]; int *dp = new int[N](); for (int i = 0; i < N; i++) dp[i] = -1; int ans = minCost(A, N - 1, K, dp); cout << ans; delete[] dp; delete[] A; return 0; }
insert
8
8
8
11
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int maxN = 100200; int mem[maxN]; int nums[maxN]; int solve(int i, int n, int k) { if (i == n - 1) { return 0; } int res = 2000000000; for (int j = i + 1; j <= i + k; j++) { if (j < n) { res = min(res, abs(nums[i] - nums[j]) + solve(j, n, k)); } } return mem[i] = res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> nums[i]; } memset(mem, -1, sizeof(mem)); cout << solve(0, n, k) << "\n"; }
#include <bits/stdc++.h> using namespace std; const int maxN = 100200; int mem[maxN]; int nums[maxN]; int solve(int i, int n, int k) { if (i == n - 1) { return 0; } if (mem[i] != -1) { return mem[i]; } int res = 2000000000; for (int j = i + 1; j <= i + k; j++) { if (j < n) { res = min(res, abs(nums[i] - nums[j]) + solve(j, n, k)); } } return mem[i] = res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> nums[i]; } memset(mem, -1, sizeof(mem)); cout << solve(0, n, k) << "\n"; }
insert
8
8
8
11
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ar[n]; int dp[n]; int k; cin >> k; for (int i = 0; i < n; i++) cin >> ar[i]; dp[0] = 0; for (int i = 1; i <= k; i++) { int mini = 100000007; for (int j = 1; j <= i; j++) mini = min(mini, (dp[i - j] + abs(ar[i] - ar[i - j]))); dp[i] = mini; } for (int i = k + 1; i < n; i++) { int mini = 10000007; for (int j = 1; j <= k; j++) mini = min(mini, (dp[i - j] + abs(ar[i] - ar[i - j]))); dp[i] = mini; } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ar[n]; int dp[n]; int k; cin >> k; for (int i = 0; i < n; i++) cin >> ar[i]; dp[0] = 0; for (int i = 1; i < n; i++) { int mini = 1000000007; if (i - k < 0) { for (int j = 0; j < i; j++) mini = min(mini, (dp[j] + abs(ar[j] - ar[i]))); dp[i] = mini; } else { for (int j = i - k; j < i; j++) mini = min(mini, (dp[j] + abs(ar[j] - ar[i]))); dp[i] = mini; } } cout << dp[n - 1] << endl; }
replace
13
24
13
24
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; int n, k, v[100010], dp[100010]; int recur(int pos) { if (pos == n - 1) return 0; if (pos >= n) return INF; if (dp[pos] != -1) return dp[pos]; int ret = INF; for (int i = 1; i <= k; ++i) ret = min(ret, recur(pos + i) + abs(v[pos] - v[pos + i])); return ret; } int main() { cin >> n >> k; for (int i = 0; i < n; ++i) cin >> v[i]; memset(dp, -1, sizeof dp); cout << recur(0) << endl; return 0; }
#include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; int n, k, v[100010], dp[100010]; int recur(int pos) { if (pos == n - 1) return 0; if (pos >= n) return INF; if (dp[pos] != -1) return dp[pos]; int ret = INF; for (int i = 1; i <= k; ++i) ret = min(ret, recur(pos + i) + abs(v[pos] - v[pos + i])); dp[pos] = ret; return ret; } int main() { cin >> n >> k; for (int i = 0; i < n; ++i) cin >> v[i]; memset(dp, -1, sizeof dp); cout << recur(0) << endl; return 0; }
insert
19
19
19
20
TLE
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ln '\n'; int lim = 1e6; ll inf = 1e15; ll solve(ll *dp, int n, int i, ll *h, int k) { if (i >= n) { return inf; } if (dp[i] != -1) { return dp[i]; } if (i == n - 1) { return 0; } else { ll ans = inf; for (int j = 1; j <= min(n - i - 1, k); j++) { ll tmp = solve(dp, n, i + j, h, k); tmp += abs(h[i] - h[i + j]); ans = min(ans, tmp); } return ans; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; ll h[n]; int k; cin >> k; for (int i = 0; i < n; i++) { cin >> h[i]; } ll dp[n]; for (int i = 0; i < n; i++) { dp[i] = -1; } cout << solve(dp, n, 0, h, k) << ln; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ln '\n'; int lim = 1e6; ll inf = 1e15; ll solve(ll *dp, int n, int i, ll *h, int k) { if (i >= n) { return inf; } if (dp[i] != -1) { return dp[i]; } if (i == n - 1) { return 0; } else { ll ans = inf; for (int j = 1; j <= min(n - i - 1, k); j++) { ll tmp = solve(dp, n, i + j, h, k); tmp += abs(h[i] - h[i + j]); ans = min(ans, tmp); } dp[i] = ans; return ans; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; ll h[n]; int k; cin >> k; for (int i = 0; i < n; i++) { cin >> h[i]; } ll dp[n]; for (int i = 0; i < n; i++) { dp[i] = -1; } cout << solve(dp, n, 0, h, k) << ln; }
insert
22
22
22
23
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define pii pair<int, int> #define sorted(a_1) sort(a_1.begin(), a_1.end()) #define rsorted(a_1) sort(a_1.rbegin(), a_1.rend()) #define t1(a_1) cout << a_1 << endl; #define t2(a_1) \ for (auto it_test : a_1) \ cout << it_test << " "; #define end cout << endl; const int MAX = 1000001; vector<long long> isprime(MAX, true); vector<long long> prime; vector<long long> SPF(MAX); void Seive(int N) { isprime[0] = isprime[1] = false; for (long long int i = 2; i < N; i++) { if (isprime[i]) { prime.push_back(i); SPF[i] = i; } for (long long int j = 0; j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; j++) { isprime[i * prime[j]] = false; SPF[i * prime[j]] = prime[j]; } } } ll expo(ll a, ll b, ll m) { if (b == 0) return 1; ll p = expo(a, b / 2, m) % m; p = (p * p) % m; return (b % 2 == 0) ? p : (a * p) % m; } void solve() { int n, k; cin >> n >> k; vector<int> arr(n); for (auto &i : arr) cin >> i; vector<int> dp(n, INT_MAX); dp[0] = 0; dp[1] = abs(arr[1] - arr[0]); for (int i = 2; i < n; i++) { for (int j = 1; j <= k; j++) dp[i] = min(dp[i], abs(arr[i] - arr[i - j]) + dp[i - j]); } cout << dp[n - 1] << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define pii pair<int, int> #define sorted(a_1) sort(a_1.begin(), a_1.end()) #define rsorted(a_1) sort(a_1.rbegin(), a_1.rend()) #define t1(a_1) cout << a_1 << endl; #define t2(a_1) \ for (auto it_test : a_1) \ cout << it_test << " "; #define end cout << endl; const int MAX = 1000001; vector<long long> isprime(MAX, true); vector<long long> prime; vector<long long> SPF(MAX); void Seive(int N) { isprime[0] = isprime[1] = false; for (long long int i = 2; i < N; i++) { if (isprime[i]) { prime.push_back(i); SPF[i] = i; } for (long long int j = 0; j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; j++) { isprime[i * prime[j]] = false; SPF[i * prime[j]] = prime[j]; } } } ll expo(ll a, ll b, ll m) { if (b == 0) return 1; ll p = expo(a, b / 2, m) % m; p = (p * p) % m; return (b % 2 == 0) ? p : (a * p) % m; } void solve() { int n, k; cin >> n >> k; vector<int> arr(n); for (auto &i : arr) cin >> i; vector<int> dp(n, INT_MAX); dp[0] = 0; dp[1] = abs(arr[1] - arr[0]); for (int i = 2; i < n; i++) { for (int j = 1; j <= k && j <= i; j++) dp[i] = min(dp[i], abs(arr[i] - arr[i - j]) + dp[i - j]); } cout << dp[n - 1] << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
replace
54
55
54
55
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; typedef unsigned long long int ull; #define pb push_back #define For(ii, aa, bb) for (ll ii = aa; ii < bb; ii++) #define Rof(ii, aa, bb) for (ll ii = aa; ii >= bb; ii--) #define st first #define nd second #define MP make_pair #define MAX 1000000009 #define MOD 1000000007 #define all(v6) v6.begin(), v6.end() #define IT iterator #define ln "\n" #define F(ii) ((abs(ii) + ii) / 2) #define N 500005 using namespace std; ll n, k, m; ll dp[N] = {0}; ll arr[N]; void solve(ll pl) { dp[pl] = MAX; For(i, 1, k + 1) { if (pl - i < 1) { break; } dp[pl] = min(dp[pl], abs(arr[pl] - arr[pl - i]) + dp[pl - i]); } if (dp[pl] == MAX) { dp[pl] = 0; } if (pl != n) { solve(pl + 1); } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; For(i, 1, n + 1) { cin >> arr[i]; } solve(1); cout << dp[n]; return 0; }
#include <bits/stdc++.h> typedef long long ll; typedef unsigned long long int ull; #define pb push_back #define For(ii, aa, bb) for (ll ii = aa; ii < bb; ii++) #define Rof(ii, aa, bb) for (ll ii = aa; ii >= bb; ii--) #define st first #define nd second #define MP make_pair #define MAX 1000000009 #define MOD 1000000007 #define all(v6) v6.begin(), v6.end() #define IT iterator #define ln "\n" #define F(ii) ((abs(ii) + ii) / 2) #define N 500005 using namespace std; ll n, k, m; ll dp[N] = {0}; ll arr[N]; void solve(ll pl) { dp[pl] = MAX; For(i, 1, k + 1) { if (pl - i < 1) { break; } dp[pl] = min(dp[pl], abs(arr[pl] - arr[pl - i]) + dp[pl - i]); } if (dp[pl] == MAX) { dp[pl] = 0; } if (pl != n) { solve(pl + 1); } } int main() { #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; For(i, 1, n + 1) { cin >> arr[i]; } solve(1); cout << dp[n]; return 0; }
replace
37
39
37
39
-11
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int stones[101]; int steps[101]; int main() { int N, K; scanf("%d %d", &N, &K); int pos = 0; for (int i = 0; i < N; ++i) { int stone; scanf("%d", &stone); if (i == 0) { steps[pos] = 0; stones[pos] = stone; } else { int m = 10000000001; for (int j = 1; j <= K || j <= i; j++) { int tmp = abs(stone - stones[(pos - j + K) % K]) + steps[(pos - j + K) % K]; m = min(m, tmp); } steps[pos] = m; stones[pos] = stone; } pos = (pos + 1) % K; } printf("%d\n", steps[(pos - 1 + K) % K]); system("pause"); return 0; }
#include <bits/stdc++.h> using namespace std; int stones[101]; int steps[101]; int main() { int N, K; scanf("%d %d", &N, &K); int pos = 0; for (int i = 0; i < N; ++i) { int stone; scanf("%d", &stone); if (i == 0) { steps[pos] = 0; stones[pos] = stone; } else { int m = 0x7FFFFFFF; for (int j = 1; j <= K && j <= i; j++) { int tmp = abs(stone - stones[(pos - j + K) % K]) + steps[(pos - j + K) % K]; m = min(m, tmp); } steps[pos] = m; stones[pos] = stone; } pos = (pos + 1) % K; } printf("%d\n", steps[(pos - 1 + K) % K]); system("pause"); return 0; }
replace
16
18
16
18
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; int N, K; int flog(vector<int> &dp, vector<int> h, int n) { if (n == 0) { return 0; } if (dp[n] != -1) { return dp[n]; } int mn = 2147483647; for (int i = 1; i <= K; i++) { if (n - i < 0) break; mn = min(mn, abs(h[n] - h[n - i]) + flog(dp, h, n - i)); } dp[n] = mn; return dp[n]; } int main() { cin >> N >> K; vector<int> h(N); rep(i, N) cin >> h[i]; vector<int> dp(N, -1); // dp[0]=0; // for(int i=0; i<N-1; i++){ // for(int j=1; j<=K; j++){ // if(i+j >=N) break; // dp[i+j] = min(dp[i+j], dp[i] + abs(h[i+j]-h[i])); // } // } int ans = flog(dp, h, N - 1); cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; int N, K; int flog(vector<int> &dp, vector<int> &h, int n) { if (n == 0) { return 0; } if (dp[n] != -1) { return dp[n]; } int mn = 2147483647; for (int i = 1; i <= K; i++) { if (n - i < 0) break; mn = min(mn, abs(h[n] - h[n - i]) + flog(dp, h, n - i)); } dp[n] = mn; return dp[n]; } int main() { cin >> N >> K; vector<int> h(N); rep(i, N) cin >> h[i]; vector<int> dp(N, -1); // dp[0]=0; // for(int i=0; i<N-1; i++){ // for(int j=1; j<=K; j++){ // if(i+j >=N) break; // dp[i+j] = min(dp[i+j], dp[i] + abs(h[i+j]-h[i])); // } // } int ans = flog(dp, h, N - 1); cout << ans << endl; }
replace
7
8
7
8
0
p03161
C++
Runtime Error
// motatoes cp snippet #include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define forr(a, b, v) for (int v = a; v <= b; v++) #define ford(a, b, v) for (int v = a; v >= b; v--) // https://www.topcoder.com/community/competitive-programming/tutorials/power-up-c-with-the-standard-template-library-part-1/#map typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<long> vl; typedef vector<long long> vll; typedef pair<int, int> ii; #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()) string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } // get default value from map type template <class K, class V> V get(map<K, V> m, K k, V def) { if (m.find(k) != m.end()) { return m[k]; } else { return def; } } // <geometry> const double PI = acos(-1.0); double toDegreeFromMinutes(double minutes) { return (minutes / 60); } double toRadians(double degree) { return (degree * PI / 180.0); } double toDegree(double radian) { if (radian < 0) radian += 2 * PI; return (radian * 180 / PI); } double fixAngle(double A) { return A > 1 ? 1 : (A < -1 ? -1 : A); } // sin(A)/a = sin(B)/b = sin(C)/c double getSide_a_bAB(double b, double A, double B) { return (sin(A) * b) / sin(B); } double getAngle_A_abB(double a, double b, double B) { return asin(fixAngle((a * sin(B)) / b)); } // a^2 = b^2 + c^2 - 2*b*c*cos(A) double getAngle_A_abc(double a, double b, double c) { return acos(fixAngle((b * b * c * c - a * a) / (2 * b * c))); } // </geometry> #define INF 1e9 + 5 int main() { ios::sync_with_stdio(0); cin.tie(0); int n; int k; cin >> n >> k; int h[n + k + 5]; for (int i = 1; i <= n; i++) { cin >> h[i]; } vector<int> dp(n + k, INF); dp[1] = 0; for (int i = 1; i < n + 1; i++) { for (int kk = 1; kk <= k; kk++) { // cout << dp[i] << " " << abs(h[i] - h[i+kk]) << endl; dp[i + kk] = min(dp[i + kk], dp[i] + abs(h[i] - h[i + kk])); } } // cout << to_string(dp) << endl; cout << dp[n]; return 0; }
// motatoes cp snippet #include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define forr(a, b, v) for (int v = a; v <= b; v++) #define ford(a, b, v) for (int v = a; v >= b; v--) // https://www.topcoder.com/community/competitive-programming/tutorials/power-up-c-with-the-standard-template-library-part-1/#map typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<long> vl; typedef vector<long long> vll; typedef pair<int, int> ii; #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()) string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } // get default value from map type template <class K, class V> V get(map<K, V> m, K k, V def) { if (m.find(k) != m.end()) { return m[k]; } else { return def; } } // <geometry> const double PI = acos(-1.0); double toDegreeFromMinutes(double minutes) { return (minutes / 60); } double toRadians(double degree) { return (degree * PI / 180.0); } double toDegree(double radian) { if (radian < 0) radian += 2 * PI; return (radian * 180 / PI); } double fixAngle(double A) { return A > 1 ? 1 : (A < -1 ? -1 : A); } // sin(A)/a = sin(B)/b = sin(C)/c double getSide_a_bAB(double b, double A, double B) { return (sin(A) * b) / sin(B); } double getAngle_A_abB(double a, double b, double B) { return asin(fixAngle((a * sin(B)) / b)); } // a^2 = b^2 + c^2 - 2*b*c*cos(A) double getAngle_A_abc(double a, double b, double c) { return acos(fixAngle((b * b * c * c - a * a) / (2 * b * c))); } // </geometry> #define INF 1e9 + 5 int main() { ios::sync_with_stdio(0); cin.tie(0); int n; int k; cin >> n >> k; int h[n + k + 5]; for (int i = 1; i <= n; i++) { cin >> h[i]; } vector<ll> dp(n + k + 5, INF); dp[1] = 0; for (int i = 1; i < n + 1; i++) { for (int kk = 1; kk <= k; kk++) { // cout << dp[i] << " " << abs(h[i] - h[i+kk]) << endl; dp[i + kk] = min(dp[i + kk], dp[i] + abs(h[i] - h[i + kk])); } } // cout << to_string(dp) << endl; cout << dp[n]; return 0; }
replace
103
104
103
104
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int #define Mod 1000000007 int main() { ll n, k; cin >> n >> k; vector<ll> v(n), dp(n); for (ll i = 0; i < n; i++) cin >> v[i]; for (ll i = 1; i < n; i++) { ll val = 999999999999; for (ll j = 1; j <= k; j++) val = min(dp[i - j] + abs(v[i] - v[i - j]), val); dp[i] = val; } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define Mod 1000000007 int main() { ll n, k; cin >> n >> k; vector<ll> v(n), dp(n); for (ll i = 0; i < n; i++) cin >> v[i]; for (ll i = 1; i < n; i++) { ll val = 999999999999; for (ll j = 1; j <= k; j++) if (i >= j) val = min(dp[i - j] + abs(v[i] - v[i - j]), val); dp[i] = val; } cout << dp[n - 1] << endl; }
replace
13
14
13
15
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); typedef long long int ll; #define forn(i, p, n) \ ; \ for (long long i = p; i < n; ++i) #define pb push_back void solve() { ll n, k; cin >> n >> k; vector<ll> a(n); forn(i, 0, n) { cin >> a[i]; } ll dp[n]; dp[0] = 0; // dp[n-2] = abs(a[n-2]-a[n-1]); forn(i, 1, n) { dp[i] = INT_MAX; for (int j = 1; j <= k; ++j) { int temp = abs(a[i] - a[i - j]); dp[i] = min(dp[i], dp[i - j] + temp); } } cout << dp[n - 1]; return; } int main() { fast; // int test; // cin >> test; // while(test--){ solve(); // cout<<"\n"; // } }
#include <bits/stdc++.h> using namespace std; #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); typedef long long int ll; #define forn(i, p, n) \ ; \ for (long long i = p; i < n; ++i) #define pb push_back void solve() { ll n, k; cin >> n >> k; vector<ll> a(n); forn(i, 0, n) { cin >> a[i]; } ll dp[n]; dp[0] = 0; // dp[n-2] = abs(a[n-2]-a[n-1]); forn(i, 1, n) { dp[i] = INT_MAX; for (int j = 1; j <= k && (i - j) >= 0; ++j) { int temp = abs(a[i] - a[i - j]); dp[i] = min(dp[i], dp[i - j] + temp); } } cout << dp[n - 1]; return; } int main() { fast; // int test; // cin >> test; // while(test--){ solve(); // cout<<"\n"; // } }
replace
22
23
22
23
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define int long long int #define endl "\n" #define pb push_back #define mp make_pair #define ff first #define ss second const int N = 1e5 + 5; int n, h[N], cache[N], k; int dp(int pos) { if (pos == n) return 0; if (pos == n - 1) return abs(h[pos] - h[n]); int &ans = cache[pos]; if (ans != -1) return ans; ans = 1e15; for (int i = pos + 1; i <= (pos + k); i++) ans = min(dp(i) + abs(h[pos] - h[i]), ans); return ans; } int32_t main() { IOS; memset(cache, -1, sizeof cache); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> h[i]; cout << dp(1); return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define int long long int #define endl "\n" #define pb push_back #define mp make_pair #define ff first #define ss second const int N = 1e5 + 5; int n, h[N], cache[N], k; int dp(int pos) { if (pos == n) return 0; if (pos == n - 1) return abs(h[pos] - h[n]); int &ans = cache[pos]; if (ans != -1) return ans; ans = 1e15; for (int i = pos + 1; i <= min(n, (pos + k)); i++) ans = min(dp(i) + abs(h[pos] - h[i]), ans); return ans; } int32_t main() { IOS; memset(cache, -1, sizeof cache); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> h[i]; cout << dp(1); return 0; }
replace
26
27
26
27
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef vector<long long> vi; typedef long long ll; #define sz(a) int((a).size()) #define pb push_back #define all(c) (c).begin(), (c).end() #define endl "\n" #define rep(i, a, b) for (ll i = a; i < b; i++) #define fr(n) for (ll i = 0; i < n; i++) #define tr(a) for (auto it = a.begin(); it != a.end(); it++) #define N 10001 #define PI 3.1415926535897932384 #define F first #define S second #define elasped_time 1.0 * clock() / CLOCKS_PER_SEC #define FAST \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define mp make_pair int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output1.txt", "w", stdout); #endif ll n, k; cin >> n >> k; ll a[n]; fr(n) cin >> a[i]; ll dp[n]; dp[0] = 0; fr(k - 1) dp[i + 1] = abs(a[0] - a[i]); rep(i, 1, n) { ll mn = 1e10; rep(j, 1, k + 1) { if (j <= i) mn = min(dp[i - j] + abs(a[i] - a[i - j]), mn); } // cout<<x<<" "<<y<<endl; dp[i] = mn; } // fr(n) cout<<dp[i]<<" "; cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<long long> vi; typedef long long ll; #define sz(a) int((a).size()) #define pb push_back #define all(c) (c).begin(), (c).end() #define endl "\n" #define rep(i, a, b) for (ll i = a; i < b; i++) #define fr(n) for (ll i = 0; i < n; i++) #define tr(a) for (auto it = a.begin(); it != a.end(); it++) #define N 10001 #define PI 3.1415926535897932384 #define F first #define S second #define elasped_time 1.0 * clock() / CLOCKS_PER_SEC #define FAST \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define mp make_pair int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output1.txt", "w", stdout); #endif ll n, k; cin >> n >> k; ll a[n]; fr(n) cin >> a[i]; ll dp[n]; dp[0] = 0; // fr(k-1) dp[i+1]=abs(a[0]-a[i]); rep(i, 1, n) { ll mn = 1e10; rep(j, 1, k + 1) { if (j <= i) mn = min(dp[i - j] + abs(a[i] - a[i - j]), mn); } // cout<<x<<" "<<y<<endl; dp[i] = mn; } // fr(n) cout<<dp[i]<<" "; cout << dp[n - 1]; return 0; }
replace
33
34
33
34
-11
p03161
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <list> #include <map> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> h(n, 0); for (int i = 0; i < n; i++) { cin >> h[i]; } if (1 + k <= n) { cout << abs(h[n - 1] - h[0]) << endl; return 0; } vector<long long> dp(n, 0); dp[0] = 0; for (int i = 0; i < k + 1; i++) { dp[i] = abs(h[i] - h[0]); } for (int i = k + 1; i < n; i++) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); for (int j = 2; j <= k; j++) { dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[n - 1] << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <list> #include <map> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> h(n, 0); for (int i = 0; i < n; i++) { cin >> h[i]; } if (n <= 1 + k) { cout << abs(h[n - 1] - h[0]) << endl; return 0; } vector<long long> dp(n, 0); dp[0] = 0; for (int i = 0; i < k + 1; i++) { dp[i] = abs(h[i] - h[0]); } for (int i = k + 1; i < n; i++) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); for (int j = 2; j <= k; j++) { dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[n - 1] << endl; return 0; }
replace
20
21
20
21
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define out(ans) cout << ans << endl #define in(n) cin >> n #define floop(i, N) for (ll i = 0; i < N; i++) #define floop1(i, N) for (ll i = 1; i <= N; i++) #define floopi(i, j, N) for (ll j = i + 1; j < N; j++) 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; } constexpr ll inf = 1e18; void solve(); int main() { solve(); } void solve() { ll n, k; in(n); in(k); ll h[n]; floop(i, n) in(h[i]); ll dp[n]; floop(i, n) dp[i] = inf; dp[0] = 0; floop(i, n) { floop1(j, k) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } } out(dp[n - 1]); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define out(ans) cout << ans << endl #define in(n) cin >> n #define floop(i, N) for (ll i = 0; i < N; i++) #define floop1(i, N) for (ll i = 1; i <= N; i++) #define floopi(i, j, N) for (ll j = i + 1; j < N; j++) 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; } constexpr ll inf = 1e18; void solve(); int main() { solve(); } void solve() { ll n, k; in(n); in(k); ll h[n]; floop(i, n) in(h[i]); ll dp[110000]; floop(i, 110000) dp[i] = inf; dp[0] = 0; floop(i, n) { floop1(j, k) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } } out(dp[n - 1]); }
replace
38
40
38
40
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); /// freopen("input.txt", "r", stdin); int n, k; cin >> n >> k; vector<int> a(n); for (int &x : a) cin >> x; vector<int> dp(n, INF); dp[0] = 0; for (int i = 1; i < n; ++i) { dp[i] = dp[i - 1] + abs(a[i] - a[i - 1]); for (int j = 2; j <= min(k, i); ++j) dp[i] = min(dp[i], abs(a[i] - a[i - j]) + dp[i - k]); } cout << dp[n - 1] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); /// freopen("input.txt", "r", stdin); int n, k; cin >> n >> k; vector<int> a(n); for (int &x : a) cin >> x; vector<int> dp(n, INF); dp[0] = 0; for (int i = 1; i < n; ++i) { dp[i] = dp[i - 1] + abs(a[i] - a[i - 1]); for (int j = 2; j <= min(k, i); ++j) dp[i] = min(dp[i], abs(a[i] - a[i - j]) + dp[i - j]); } cout << dp[n - 1] << '\n'; return 0; }
replace
22
23
22
23
0
p03161
C++
Runtime Error
#include <iostream> #include <vector> #define ma 1000000000 int mod(int a) { if (a >= 0) return a; else return -a; } using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a; a.resize(n); for (int i = 0; i < n; i++) cin >> a[i]; int b[n]; b[0] = 0; b[1] = mod(a[1] - a[0]); for (int i = 2; i <= k; i++) b[i] = mod(a[i] - a[0]); for (int i = k + 1; i < n; i++) { int t = ma; for (int k1 = 1; k1 <= k; k1++) { if (t > b[i - k1] + mod(a[i] - a[i - k1])) t = b[i - k1] + mod(a[i] - a[i - k1]); } b[i] = t; } cout << b[n - 1]; }
#include <iostream> #include <vector> #define ma 1000000000 int mod(int a) { if (a >= 0) return a; else return -a; } using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a; a.resize(n); for (int i = 0; i < n; i++) cin >> a[i]; int b[n]; b[0] = 0; b[1] = mod(a[1] - a[0]); for (int i = 2; i <= k && i < n; i++) b[i] = mod(a[i] - a[0]); for (int i = k + 1; i < n; i++) { int t = ma; for (int k1 = 1; k1 <= k; k1++) { if (t > b[i - k1] + mod(a[i] - a[i - k1])) t = b[i - k1] + mod(a[i] - a[i - k1]); } b[i] = t; } cout << b[n - 1]; }
replace
21
22
21
22
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define max1 1000005 #define siz 100000 #define mod 998244353 #define inf 1e18 + 5 #define ll long long int #define debug(x) cout << #x << " " << x << endl #define jam(x) cout << "Case #" << x << ": " typedef pair<ll, ll> pr; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); /*#ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif*/ ll n, k; cin >> n >> k; vector<ll> a(n); for (ll i = 0; i < n; i++) { cin >> a[i]; } vector<ll> dp(n, inf); ll k1 = k; dp[0] = 0; for (ll i = 0; i < n; i++) { for (ll j = i - 1; j >= 0, k1 > 0; k1--, j--) { dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j])); } k1 = k; } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; #define max1 1000005 #define siz 100000 #define mod 998244353 #define inf 1e18 + 5 #define ll long long int #define debug(x) cout << #x << " " << x << endl #define jam(x) cout << "Case #" << x << ": " typedef pair<ll, ll> pr; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); /*#ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif*/ ll n, k; cin >> n >> k; vector<ll> a(n); for (ll i = 0; i < n; i++) { cin >> a[i]; } vector<ll> dp(n, inf); ll k1 = k; dp[0] = 0; for (ll i = 0; i < n; i++) { for (ll j = i - 1; (j >= 0 && k1 > 0); k1--, j--) { dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j])); } k1 = k; } cout << dp[n - 1] << endl; }
replace
28
29
28
29
0
p03161
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; const int INF = 1e9 + 7; int main() { int n, k; cin >> n >> k; vector<int> h(n); for (int i = 0; i < n; i++) { cin >> h[i]; } vector<int> dp(n, INF); dp[0] = 0; dp[1] = abs(h[0] - h[1]); for (int i = 2; i < n; i++) { if (k == 1) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); } else { for (int j = 1; j <= k; j++) { dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i]); } } } cout << dp[n - 1]; return 0; }
#include <iostream> #include <vector> using namespace std; const int INF = 1e9 + 7; int main() { int n, k; cin >> n >> k; vector<int> h(n); for (int i = 0; i < n; i++) { cin >> h[i]; } vector<int> dp(n, INF); dp[0] = 0; dp[1] = abs(h[0] - h[1]); for (int i = 2; i < n; i++) { if (k == 1) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); } else { for (int j = 1; j <= k; j++) { if (i - j >= 0) { dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i]); } } } } cout << dp[n - 1]; return 0; }
replace
22
23
22
25
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++) #define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--) #define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define fill(ar, val) memset(ar, val, sizeof(ar)) #define fill0(ar) fill((ar), 0) #define debug(x) cout << #x << ": " << x << endl #define ld double #define pb push_back #define mp make_pair #define ff first #define ss second typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef vector<ii> vii; typedef vector<int> vi; #define INF 1000000000000000000 ll n, k; ll h[100005]; ll ans[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> n >> k; fr(i, 1, n) { cin >> h[i]; } ans[1] = 0; fr(i, 2, n) { ans[i] = INF; } fr(i, 2, n) { fr(j, 1, min(i - 1, k)) { ll x = abs(h[i] - h[i - j]); ans[i] = min(ans[i], ans[i - j] + x); } } cout << ans[n] << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++) #define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--) #define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define fill(ar, val) memset(ar, val, sizeof(ar)) #define fill0(ar) fill((ar), 0) #define debug(x) cout << #x << ": " << x << endl #define ld double #define pb push_back #define mp make_pair #define ff first #define ss second typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef vector<ii> vii; typedef vector<int> vi; #define INF 1000000000000000000 ll n, k; ll h[100005]; ll ans[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; fr(i, 1, n) { cin >> h[i]; } ans[1] = 0; fr(i, 2, n) { ans[i] = INF; } fr(i, 2, n) { fr(j, 1, min(i - 1, k)) { ll x = abs(h[i] - h[i - j]); ans[i] = min(ans[i], ans[i - j] + x); } } cout << ans[n] << endl; }
delete
33
37
33
33
0
p03161
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; using ll = long long; int main() { int N, K; cin >> N >> K; vector<int> h(N); for (int i = 0; i < N; ++i) { cin >> h[i]; } vector<int> dp(N); dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < N; ++i) { int candi = dp[i - 1] + abs(h[i] - h[i - 1]); for (int j = 2; j <= K; ++j) { if (i - j < 0) break; candi = min(candi, dp[i - K] + abs(h[i] - h[i - j])); } dp[i] = candi; } cout << dp[N - 1] << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; using ll = long long; int main() { int N, K; cin >> N >> K; vector<int> h(N); for (int i = 0; i < N; ++i) { cin >> h[i]; } vector<int> dp(N); dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < N; ++i) { int candi = dp[i - 1] + abs(h[i] - h[i - 1]); for (int j = 2; j <= K; ++j) { if (i - j < 0) break; candi = min(candi, dp[i - j] + abs(h[i] - h[i - j])); } dp[i] = candi; } cout << dp[N - 1] << endl; return 0; }
replace
22
23
22
23
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int frogCost(int h[], int d[], int n, int k) { if (d[n] != INT_MAX) return d[n]; int cost = INT_MAX; for (int i = 1; i <= k; i++) { if ((n - i) >= 0) { int diff = abs(h[n - i] - h[n]); cost = min(cost, diff + frogCost(h, d, n - i, k)); } } return cost; } int main() { int N, K; cin >> N >> K; int heights[N]; int cost[N]; for (int i = 0; i < N; i++) { int h; cin >> h; heights[i] = h; cost[i] = INT_MAX; } cost[0] = 0; // for(int j=0;j<N;j++){ // for(int k=1;k<=K;k++){ // if(j+k<N){ // int diff = abs(heights[j] - heights[j+k]); // cost[j+k] = min(cost[j+k],cost[j]+diff); // } // } // } cout << frogCost(heights, cost, N - 1, K); return 0; }
#include <bits/stdc++.h> using namespace std; int frogCost(int h[], int d[], int n, int k) { if (d[n] != INT_MAX) return d[n]; int cost = INT_MAX; for (int i = 1; i <= k; i++) { if ((n - i) >= 0) { int diff = abs(h[n - i] - h[n]); cost = min(cost, diff + frogCost(h, d, n - i, k)); } } d[n] = cost; return cost; } int main() { int N, K; cin >> N >> K; int heights[N]; int cost[N]; for (int i = 0; i < N; i++) { int h; cin >> h; heights[i] = h; cost[i] = INT_MAX; } cost[0] = 0; // for(int j=0;j<N;j++){ // for(int k=1;k<=K;k++){ // if(j+k<N){ // int diff = abs(heights[j] - heights[j+k]); // cost[j+k] = min(cost[j+k],cost[j]+diff); // } // } // } cout << frogCost(heights, cost, N - 1, K); return 0; }
insert
14
14
14
15
TLE
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define MAXN 100005 #define INF 1e9 + 5 using namespace std; int n, lake[MAXN], k; int dp[MAXN]; int solve(int v) { if (v == n - 1) return 0; if (dp[v] >= 0) return dp[v]; int temp = INF; for (int i = 1; i <= k; i++) { if (v + i >= n) break; temp = min(temp, abs(lake[v] - lake[v + i]) + solve(v + i)); } return dp[k] = temp; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); memset(dp, -1, sizeof(dp)); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> lake[i]; } cout << solve(0); }
#include <bits/stdc++.h> #define MAXN 100005 #define INF 1e9 + 5 using namespace std; int n, lake[MAXN], k; int dp[MAXN]; int solve(int v) { if (v == n - 1) return 0; if (dp[v] >= 0) return dp[v]; int temp = INF; for (int i = 1; i <= k; i++) { if (v + i >= n) break; temp = min(temp, abs(lake[v] - lake[v + i]) + solve(v + i)); } return dp[v] = temp; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); memset(dp, -1, sizeof(dp)); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> lake[i]; } cout << solve(0); }
replace
20
21
20
21
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; void solve(vector<int> &h, vector<int> &v, int k = 2) { int n = h.size(); for (int curr = 0; curr < n; curr++) { for (int j = curr + 1; j < n and j <= curr + k; j++) { int d1 = abs(h[curr] - h[curr + j]); v[j] = min(v[j], v[curr] + d1); } } } int main() { int n, k; cin >> n >> k; vector<int> h(n), v(n); for (int i = 0; i < n; i++) cin >> h[i]; v[0] = 0; for (int i = 1; i < n; i++) v[i] = INT_MAX; solve(h, v, k); cout << v[n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; void solve(vector<int> &h, vector<int> &v, int k = 2) { int n = h.size(); for (int curr = 0; curr < n; curr++) { for (int j = curr + 1; j < n and j <= curr + k; j++) { int d1 = abs(h[curr] - h[j]); v[j] = min(v[j], v[curr] + d1); } } } int main() { int n, k; cin >> n >> k; vector<int> h(n), v(n); for (int i = 0; i < n; i++) cin >> h[i]; v[0] = 0; for (int i = 1; i < n; i++) v[i] = INT_MAX; solve(h, v, k); cout << v[n - 1] << endl; }
replace
7
8
7
8
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); ll n, k; ll func(ll cost[], ll h[]) { cost[1] = 0; cost[2] = abs(h[1] - h[2]); for (ll i = 3; i <= k + 1; i++) { ll temp = cost[i - 1] + abs(h[i] - h[i - 1]); for (ll j = 2; j < i; j++) { temp = min(temp, cost[i - j] + abs(h[i] - h[i - j])); } cost[i] = temp; } for (ll i = k + 2; i <= n; i++) { ll temp = cost[i - 1] + abs(h[i] - h[i - 1]); for (ll j = 2; j <= k; j++) { temp = min(temp, cost[i - j] + abs(h[i] - h[i - j])); } cost[i] = temp; } return cost[n]; } int32_t main() { IOS; cin >> n >> k; ll h[n + 1]; for (int i = 1; i <= n; i++) { cin >> h[i]; } ll cost[n + 1]; ll costn = func(cost, h); cout << costn; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); ll n, k; ll func(ll cost[], ll h[]) { cost[1] = 0; cost[2] = abs(h[1] - h[2]); for (ll i = 3; i <= k + 1; i++) { ll temp = cost[i - 1] + abs(h[i] - h[i - 1]); for (ll j = 2; j < i; j++) { temp = min(temp, cost[i - j] + abs(h[i] - h[i - j])); } cost[i] = temp; } for (ll i = k + 2; i <= n; i++) { ll temp = cost[i - 1] + abs(h[i] - h[i - 1]); for (ll j = 2; j <= k; j++) { temp = min(temp, cost[i - j] + abs(h[i] - h[i - j])); } cost[i] = temp; } return cost[n]; } int32_t main() { IOS; cin >> n >> k; ll h[n + 1]; if (k > n) { k = n; } for (int i = 1; i <= n; i++) { cin >> h[i]; } ll cost[n + 1]; ll costn = func(cost, h); cout << costn; return 0; }
insert
32
32
32
35
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define rep0(i, n) for (int i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i < (n); ++i) #define REP(i, a, b) for (int i = a; i < (b); ++i) #define mREP(i, a, b) for (int i = a; i > (b); --i) #define all(x) (x).begin(), (x).end() const int INF = 1000000007; typedef long long ll; 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 gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } int main() { vector<int> dp(1000000, INF); int n, k; cin >> n >> k; vector<int> a(n); rep0(i, n) cin >> a[i]; dp[0] = 0; dp[1] = abs(a[0] - a[1]); REP(i, 2, n) { rep1(j, k + 1) { if (k > j) continue; dp[i] = min(dp[i], (dp[i - j] + abs(a[i - j] - a[i]))); } } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> #define rep0(i, n) for (int i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i < (n); ++i) #define REP(i, a, b) for (int i = a; i < (b); ++i) #define mREP(i, a, b) for (int i = a; i > (b); --i) #define all(x) (x).begin(), (x).end() const int INF = 1000000007; typedef long long ll; 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 gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } int main() { vector<int> dp(1000000, INF); int n, k; cin >> n >> k; vector<int> a(n); rep0(i, n) cin >> a[i]; dp[0] = 0; dp[1] = abs(a[0] - a[1]); REP(i, 2, n) { rep1(j, k + 1) { if (j > i) continue; dp[i] = min(dp[i], (dp[i - j] + abs(a[i - j] - a[i]))); } } cout << dp[n - 1] << endl; return 0; }
replace
34
35
34
35
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #include <cmath> using namespace std; int minCost(int a[], int n, int k) { int dp[n]; dp[0] = 0; for (int i = 1; i < n; i++) dp[i] = INT_MAX; for (int i = 0; i < n; i++) { for (int j = i + 1; j <= i + k; j++) { dp[j] = min(dp[j], dp[i] + abs(a[j] - a[i])); } } return dp[n - 1]; } int main() { int n, k; cin >> n; cin >> k; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } cout << minCost(a, n, k); }
#include <bits/stdc++.h> #include <cmath> using namespace std; int minCost(int a[], int n, int k) { int dp[n]; dp[0] = 0; for (int i = 1; i < n; i++) dp[i] = INT_MAX; for (int i = 0; i < n; i++) { for (int j = i + 1; j <= i + k; j++) { if (j < n) { dp[j] = min(dp[j], dp[i] + abs(a[j] - a[i])); } } } return dp[n - 1]; } int main() { int n, k; cin >> n; cin >> k; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } cout << minCost(a, n, k); }
replace
11
12
11
14
0
p03161
C++
Runtime Error
/* ############################## # Author: Pratyush Gaurav # # College: NIT ROURKELA # ############################## */ #include <bits/stdc++.h> using namespace std; typedef long long lli; typedef long double ld; typedef pair<lli, lli> plli; typedef vector<lli> vlli; typedef vector<plli> vplli; #define IOS ios_base::sync_with_stdio(false); #define gcd(a, b) __gcd(a, b) #define lcm(a, b) ((a * b) / gcd(a, b)) #define sqr(x) (x) * (x) #define all(a) a.begin(), a.end() #define UN(v) sort(all(v)), v.resize(unique(all(v)) - v.begin()) #define endl '\n' const long long INF = 100000000000000000; const long long MOD = 1000000007; const long long MAXN = 100005; lli dx[] = {0, 0, -1, 1, -1, -1, 1, 1}; lli dy[] = {1, -1, 0, 0, 1, -1, -1, 1}; lli n, k; lli a[MAXN], dp[MAXN]; lli f(lli x) { if (x > n) return INF; if (x == n) return 0; if (dp[x] != -1) return dp[x]; lli ans = INF; for (int i = 1; i <= k; ++i) { ans = min(ans, abs(a[x] - a[x + i]) + f(x + i)); } return dp[x] = ans; } void solve() { cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; } memset(dp, -1, sizeof(dp)); lli ans = f(1); cout << ans << endl; } int main() { IOS; lli t = 1; // cin >> t; solve(); return 0; }
/* ############################## # Author: Pratyush Gaurav # # College: NIT ROURKELA # ############################## */ #include <bits/stdc++.h> using namespace std; typedef long long lli; typedef long double ld; typedef pair<lli, lli> plli; typedef vector<lli> vlli; typedef vector<plli> vplli; #define IOS ios_base::sync_with_stdio(false); #define gcd(a, b) __gcd(a, b) #define lcm(a, b) ((a * b) / gcd(a, b)) #define sqr(x) (x) * (x) #define all(a) a.begin(), a.end() #define UN(v) sort(all(v)), v.resize(unique(all(v)) - v.begin()) #define endl '\n' const long long INF = 100000000000000000; const long long MOD = 1000000007; const long long MAXN = 100005; lli dx[] = {0, 0, -1, 1, -1, -1, 1, 1}; lli dy[] = {1, -1, 0, 0, 1, -1, -1, 1}; lli n, k; lli a[MAXN], dp[MAXN]; lli f(lli x) { if (x > n) return INF; if (x == n) return 0; if (dp[x] != -1) return dp[x]; lli ans = INF; for (int i = 1; i <= k; ++i) { if (x + i > n) continue; ans = min(ans, abs(a[x] - a[x + i]) + f(x + i)); } return dp[x] = ans; } void solve() { cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; } memset(dp, -1, sizeof(dp)); lli ans = f(1); cout << ans << endl; } int main() { IOS; lli t = 1; // cin >> t; solve(); return 0; }
insert
45
45
45
47
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; int k; cin >> k; int a[n + 1]; for (int i = 0; i < n; i++) { cin >> a[i]; } vector<ll> dp(n + 1, INT_MAX); dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (int i = 2; i < n; i++) { for (int j = i - 1, jump = 0; j >= 0, jump < k; j--, jump++) { dp[i] = min(dp[i], abs(a[i] - a[j]) + dp[j]); } } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; int k; cin >> k; int a[n + 1]; for (int i = 0; i < n; i++) { cin >> a[i]; } vector<ll> dp(n + 1, INT_MAX); dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (int i = 2; i < n; i++) { for (int j = i - 1, jump = 0; j >= 0 && jump < k; j--, jump++) { dp[i] = min(dp[i], abs(a[i] - a[j]) + dp[j]); } } cout << dp[n - 1] << endl; return 0; }
replace
17
18
17
18
0
p03161
C++
Runtime Error
#include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long unsigned int ll; #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define ALL(x) (x).begin(), (x).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; } int main() { int N, K; cin >> N; cin >> K; int h[0] = {}; REP(i, N) cin >> h[i]; ll DP[N]; REP(i, N) DP[i] = INF; DP[0] = 0; REPS(i, N - 1) { REPS(j, K) { if (i - j >= 0) chmin(DP[i], DP[i - j] + abs(h[i] - h[i - j])); } } cout << DP[N - 1]; return 0; }
#include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long unsigned int ll; #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define ALL(x) (x).begin(), (x).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; } int main() { int N, K; cin >> N; cin >> K; int h[N] = {}; REP(i, N) cin >> h[i]; ll DP[N]; REP(i, N) DP[i] = INF; DP[0] = 0; REPS(i, N - 1) { REPS(j, K) { if (i - j >= 0) chmin(DP[i], DP[i - j] + abs(h[i] - h[i - j])); } } cout << DP[N - 1]; return 0; }
replace
45
46
45
46
-6
*** stack smashing detected ***: terminated
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int sa(int a, int b) { return max(a - b, b - a); } int main() { int N, K; cin >> N >> K; vector<int> he(N); for (int i = 0; i < N; i++) cin >> he[i]; vector<int> dp(N); for (int i = 0; i <= K; i++) { dp[i] = sa(he[0], he[i]); } for (int i = K + 1; i < N; i++) { dp[i] = sa(he[i], he[i - 1]) + dp[i - 1]; for (int j = 2; j <= K; j++) { int kuma = sa(he[i], he[i - j]) + dp[i - j]; if (dp[i] > kuma) dp[i] = kuma; } } cout << dp[N - 1] << endl; }
#include <bits/stdc++.h> using namespace std; int sa(int a, int b) { return max(a - b, b - a); } int main() { int N, K; cin >> N >> K; vector<int> he(N); for (int i = 0; i < N; i++) cin >> he[i]; if (N - 1 <= K) { cout << sa(he[0], he[N - 1]) << endl; return 0; } vector<int> dp(N); for (int i = 0; i <= K; i++) { dp[i] = sa(he[0], he[i]); } for (int i = K + 1; i < N; i++) { dp[i] = sa(he[i], he[i - 1]) + dp[i - 1]; for (int j = 2; j <= K; j++) { int kuma = sa(he[i], he[i - j]) + dp[i - j]; if (dp[i] > kuma) dp[i] = kuma; } } cout << dp[N - 1] << endl; }
insert
9
9
9
13
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #include <stdlib.h> #include <string> using namespace std; int h[10002] = {0}; int memo[10002] = {-1}; int compute(int startPos, int n, int k) { if (memo[startPos] != -1) { return memo[startPos]; } if (startPos == n - 1) { return 0; } if (startPos == n - 2) { return abs(h[startPos] - h[startPos + 1]); } int value = abs(h[startPos] - h[startPos + 1]) + compute(startPos + 1, n, k); for (int i = 2; i <= k; i++) { if (startPos + i <= n - 1) value = min(value, abs(h[startPos] - h[startPos + i]) + compute(startPos + i, n, k)); } memo[startPos] = value; return value; } int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> h[i]; memo[i] = -1; } int answer = compute(0, n, k); cout << answer << endl; return 0; }
#include <bits/stdc++.h> #include <iostream> #include <stdlib.h> #include <string> using namespace std; int h[100002] = {0}; int memo[100002] = {-1}; int compute(int startPos, int n, int k) { if (memo[startPos] != -1) { return memo[startPos]; } if (startPos == n - 1) { return 0; } if (startPos == n - 2) { return abs(h[startPos] - h[startPos + 1]); } int value = abs(h[startPos] - h[startPos + 1]) + compute(startPos + 1, n, k); for (int i = 2; i <= k; i++) { if (startPos + i <= n - 1) value = min(value, abs(h[startPos] - h[startPos + i]) + compute(startPos + i, n, k)); } memo[startPos] = value; return value; } int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> h[i]; memo[i] = -1; } int answer = compute(0, n, k); cout << answer << endl; return 0; }
replace
7
9
7
9
0
p03161
C++
Runtime Error
//////////////////////////////////// /// Please Give Me AC!!! /// //////////////////////////////////// #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; #define forin(in) \ for (int i = 0; i < (int)in.size(); i++) \ cin >> in[i] #define forout(out) \ for (int i = 0; i < (int)out.size(); i++) \ cout << out[i] << endl #define rep(i, n) for (int i = 0; i < (n); i++) 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; } int main() { int N, K; cin >> N >> K; vector<int> h(N); forin(h); vector<int> dp(N, 1e9); dp[0] = 0; for (int i = 0; i < N; i++) { for (int j = 1; j <= K; j++) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } } cout << dp[N - 1] << endl; }
//////////////////////////////////// /// Please Give Me AC!!! /// //////////////////////////////////// #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; #define forin(in) \ for (int i = 0; i < (int)in.size(); i++) \ cin >> in[i] #define forout(out) \ for (int i = 0; i < (int)out.size(); i++) \ cout << out[i] << endl #define rep(i, n) for (int i = 0; i < (n); i++) 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; } int main() { int N, K; cin >> N >> K; vector<int> h(N); forin(h); vector<int> dp(N + K, 1e9); dp[0] = 0; for (int i = 0; i < N; i++) { for (int j = 1; j <= K; j++) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } } cout << dp[N - 1] << endl; }
replace
37
38
37
38
-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)
p03161
C++
Runtime Error
#include <bits/stdc++.h> #include <stack> using namespace std; typedef long long ll; typedef long double lxd; #define INF 1000000007 #define mem(dp, a) memset(dp, a, sizeof dp) #define flash \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define repb(i, a, b) for (ll i = a; i >= b; i--) #define f(i, n) for (ll i = 0; i < n; i++) #define fo(i, x, n) for (ll i = x; i <= n; i++) #define pb(x) push_back(x) #define tr(c, it) for (((c).begin())it = (c).begin(); it != (c).end(); it++) #define test \ ll t; \ cin >> t; #define fls fflush(stdout); int main() { ll n, k; cin >> n >> k; ll height[n], dp[n]; for (ll i = 0; i < n; i++) cin >> height[i]; dp[0] = 0; dp[1] = abs(height[1] - height[0]); ll mn; for (ll i = 2; i < k + 1; i++) { mn = INT_MAX; for (ll j = i - 1; j >= 0; j--) { mn = min(mn, dp[j] + abs(height[j] - height[i])); dp[i] = mn; } } for (ll i = k + 1; i < n; i++) { mn = INT_MAX; for (ll j = i - 1; j >= i - k; j--) { mn = min(mn, dp[j] + abs(height[j] - height[i])); dp[i] = mn; } } // for(ll i=0;i<n;i++)cout<<dp[i]<<" ";cout<<endl; cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> #include <stack> using namespace std; typedef long long ll; typedef long double lxd; #define INF 1000000007 #define mem(dp, a) memset(dp, a, sizeof dp) #define flash \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define repb(i, a, b) for (ll i = a; i >= b; i--) #define f(i, n) for (ll i = 0; i < n; i++) #define fo(i, x, n) for (ll i = x; i <= n; i++) #define pb(x) push_back(x) #define tr(c, it) for (((c).begin())it = (c).begin(); it != (c).end(); it++) #define test \ ll t; \ cin >> t; #define fls fflush(stdout); int main() { ll n, k; cin >> n >> k; ll height[n], dp[n]; for (ll i = 0; i < n; i++) cin >> height[i]; dp[0] = 0; dp[1] = abs(height[1] - height[0]); ll mn; for (ll i = 2; i < k + 1 && i < n; i++) { mn = INT_MAX; for (ll j = i - 1; j >= 0; j--) { mn = min(mn, dp[j] + abs(height[j] - height[i])); dp[i] = mn; } } for (ll i = k + 1; i < n; i++) { mn = INT_MAX; for (ll j = i - 1; j >= i - k; j--) { mn = min(mn, dp[j] + abs(height[j] - height[i])); dp[i] = mn; } } // for(ll i=0;i<n;i++)cout<<dp[i]<<" ";cout<<endl; cout << dp[n - 1] << endl; return 0; }
replace
29
30
29
30
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const int INF = INT_MAX; // 2147483647 const int MOD = (int)1e9 + 7; const double EPS = 1e-9; #ifdef LOCAL_ENV #define debug(var) std::cout << #var " = " << var << std::endl #else #define debug(var) #endif #define p(var) std::cout << var << std::endl #define PI (acos(-1)) #define rep(i, n) for (int i = 0, i##_length = (n); i < i##_length; ++i) #define repeq(i, n) for (int i = 1, i##_length = (n); i <= i##_length; ++i) #define all(a) (a).begin(), (a).end() #define pb push_back inline double isnatural(double a) { return a >= 0 && ceil(a) == floor(a); } template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) { for (int i = 0, len = v.size(); i < len; ++i) { s << v[i]; if (i < len - 1) s << "\t"; } return s; } template <typename T> ostream &operator<<(ostream &s, const vector<vector<T>> &vv) { for (int i = 0, len = vv.size(); i < len; ++i) { s << vv[i] << endl; } return s; } template <typename T1, typename T2> ostream &operator<<(ostream &s, const map<T1, T2> &m) { s << "{" << endl; for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) { s << "\t" << (*itr).first << " : " << (*itr).second << endl; } s << "}" << endl; return s; } /*-----8<-----8<-----*/ int main() { int n, k; cin >> n >> k; vector<int> h(n + 1, 0); repeq(i, n) { cin >> h[i]; } vector<int> dp(n + 1, 0); dp[2] = abs(h[1] - h[2]); for (int i = 3; i <= n; i++) { int minval = 99999999; for (int j = 1; j <= k; j++) { chmin(minval, dp[i - j] + abs(h[i] - h[i - j])); } dp[i] = minval; } p(dp[n]); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int INF = INT_MAX; // 2147483647 const int MOD = (int)1e9 + 7; const double EPS = 1e-9; #ifdef LOCAL_ENV #define debug(var) std::cout << #var " = " << var << std::endl #else #define debug(var) #endif #define p(var) std::cout << var << std::endl #define PI (acos(-1)) #define rep(i, n) for (int i = 0, i##_length = (n); i < i##_length; ++i) #define repeq(i, n) for (int i = 1, i##_length = (n); i <= i##_length; ++i) #define all(a) (a).begin(), (a).end() #define pb push_back inline double isnatural(double a) { return a >= 0 && ceil(a) == floor(a); } template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) { for (int i = 0, len = v.size(); i < len; ++i) { s << v[i]; if (i < len - 1) s << "\t"; } return s; } template <typename T> ostream &operator<<(ostream &s, const vector<vector<T>> &vv) { for (int i = 0, len = vv.size(); i < len; ++i) { s << vv[i] << endl; } return s; } template <typename T1, typename T2> ostream &operator<<(ostream &s, const map<T1, T2> &m) { s << "{" << endl; for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) { s << "\t" << (*itr).first << " : " << (*itr).second << endl; } s << "}" << endl; return s; } /*-----8<-----8<-----*/ int main() { int n, k; cin >> n >> k; vector<int> h(n + 1, 0); repeq(i, n) { cin >> h[i]; } vector<int> dp(n + 1, 0); dp[2] = abs(h[1] - h[2]); for (int i = 3; i <= n; i++) { int minval = INF; for (int j = 1; j <= k && i - j >= 1; j++) { chmin(minval, dp[i - j] + abs(h[i] - h[i - j])); } dp[i] = minval; } p(dp[n]); return 0; }
replace
68
70
68
70
0
p03161
C++
Runtime Error
// In The Name Of God #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define sqr(A) ((A) * (A)) #define X first #define Y second #define MP make_pair #define bsz __builtin_popcount #define all(A) A.begin(), A.end() using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; const int MOD = 1e9 + 7; const int MOD2 = 1e9 + 9; const int PR = 727; const int INF = INT_MAX; const ll LINF = LLONG_MAX; const int N = 1e5 + 20; ll dp[N], n, h[N], k; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = n - 2; i >= 0; i--) { dp[i] = INF; for (int j = i + 1; j <= i + k; j++) dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j])); } cout << dp[0] << endl; }
// In The Name Of God #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define sqr(A) ((A) * (A)) #define X first #define Y second #define MP make_pair #define bsz __builtin_popcount #define all(A) A.begin(), A.end() using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; const int MOD = 1e9 + 7; const int MOD2 = 1e9 + 9; const int PR = 727; const int INF = INT_MAX; const ll LINF = LLONG_MAX; const int N = 1e5 + 20; ll dp[N], n, h[N], k; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = n - 2; i >= 0; i--) { dp[i] = INF; for (int j = i + 1; j <= min(i + k, n - 1); j++) dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j])); } cout << dp[0] << endl; }
replace
35
36
35
36
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<long long> vll; typedef vector<pair<int, int>> vpi; typedef vector<pair<long long, long long>> vpl; typedef pair<int, int> pii; typedef pair<long long, long long> pll; #define INF (int)(1e9) #define MAXX 1.1529215e+18 #define inf 999999 #define EPS (1e-7) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, n) for (int i = 1; i <= (int)(n); i++) #define FOR(i, k, n) for (int i = (k); i < (int)(n); i++) #define ALL(a) a.begin(), a.end() #define RALL(a) a.begin(), a.end(), greater<int>() #define ROT(a) a.begin(), a.begin() + 1, a.end() #define RROT(a) a.begin(), a.end() - 1, a.end() #define PB push_back #define MP make_pair #define PI acos(-1.0) #define sz(a) a.size() const ll MOD = 1e9 + 7; const int MAX = 10000000; template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } // cout << 'H' << endl; /*--------------------------------------------*/ int main() { // cout << fixed << setprecision(10) cin.tie(0); ios::sync_with_stdio(false); int N, K; cin >> N >> K; vector<int> h(N); rep(i, N) cin >> h[i]; int dp[100010]; memset(dp, 0, sizeof(dp)); dp[1] = abs(h[0] - h[1]); for (int i = 2; i < N; i++) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); for (int j = 2; j <= K; j++) { chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[N - 1] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<long long> vll; typedef vector<pair<int, int>> vpi; typedef vector<pair<long long, long long>> vpl; typedef pair<int, int> pii; typedef pair<long long, long long> pll; #define INF (int)(1e9) #define MAXX 1.1529215e+18 #define inf 999999 #define EPS (1e-7) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, n) for (int i = 1; i <= (int)(n); i++) #define FOR(i, k, n) for (int i = (k); i < (int)(n); i++) #define ALL(a) a.begin(), a.end() #define RALL(a) a.begin(), a.end(), greater<int>() #define ROT(a) a.begin(), a.begin() + 1, a.end() #define RROT(a) a.begin(), a.end() - 1, a.end() #define PB push_back #define MP make_pair #define PI acos(-1.0) #define sz(a) a.size() const ll MOD = 1e9 + 7; const int MAX = 10000000; template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } // cout << 'H' << endl; /*--------------------------------------------*/ int main() { // cout << fixed << setprecision(10) cin.tie(0); ios::sync_with_stdio(false); int N, K; cin >> N >> K; vector<int> h(N); rep(i, N) cin >> h[i]; int dp[100010]; memset(dp, 0, sizeof(dp)); dp[1] = abs(h[0] - h[1]); for (int i = 2; i < N; i++) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); for (int j = 2; j <= K; j++) { if (i - j >= 0) { chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } } cout << dp[N - 1] << endl; }
replace
66
67
66
69
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { ll n, k; cin >> n >> k; ll a[n]; for (int i = 0; i < n; i++) cin >> a[i]; ll dp[n]; dp[0] = 0; for (int i = 1; i < n; i++) dp[i] = 1e9; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j <= i + k; j++) dp[j] = min(dp[j], abs(a[i] - a[j]) + dp[i]); } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { ll n, k; cin >> n >> k; ll a[n]; for (int i = 0; i < n; i++) cin >> a[i]; ll dp[n]; dp[0] = 0; for (int i = 1; i < n; i++) dp[i] = 1e9; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j <= i + k; j++) if (j < n) dp[j] = min(dp[j], abs(a[i] - a[j]) + dp[i]); else break; } cout << dp[n - 1]; return 0; }
replace
16
17
16
20
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int #define mod 1000000007 using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, k; cin >> n >> k; ll arr[n + 1]; for (int a = 1; a <= n; a++) cin >> arr[a]; ll dp[n + 1]; dp[1] = 0; for (int a = 2; a <= k + 1; a++) { dp[a] = abs(arr[a] - arr[1]); } for (int a = k + 2; a <= n; a++) { ll x, y, z; ll mini = 1000000000000000; for (int b = 1; b <= k; b++) { x = dp[a - b] + abs(arr[a] - arr[a - b]); mini = min(mini, x); } dp[a] = mini; } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> #define ll long long int #define mod 1000000007 using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, k; cin >> n >> k; ll arr[n + 1]; for (int a = 1; a <= n; a++) cin >> arr[a]; ll dp[n + 1]; dp[1] = 0; for (int a = 2; a <= min(n, k + 1); a++) { dp[a] = abs(arr[a] - arr[1]); } for (int a = k + 2; a <= n; a++) { ll x, y, z; ll mini = 1000000000000000; for (int b = 1; b <= k; b++) { x = dp[a - b] + abs(arr[a] - arr[a - b]); mini = min(mini, x); } dp[a] = mini; } cout << dp[n] << endl; return 0; }
replace
15
16
15
16
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; inline int ctoi(char c) { if (c < '0' || '9' < c) throw invalid_argument("ctoi error"); return c - '0'; } #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++) #define all(x) (x).begin(), (x).end() template <typename T> inline T gcd(T x, T y) { if (x <= 0 || y <= 0) throw invalid_argument("gcd error: x <= 0 or y <= 0"); if (x < y) swap(x, y); T r = x % y; while (r != 0) { x = y; y = r; r = x % y; } return y; } template <typename T> inline T lcm(T x, T y) { if (x <= 0 || y <= 0) throw invalid_argument("lcm error: x <= 0 or y <= 0"); return x * y / gcd(x, y); } int main() { int n, k; cin >> n >> k; vector<int> vec; rep(i, n) { int x; cin >> x; vec.push_back(x); } vector<int> dp(n); dp[0] = 0; dp[1] = abs(vec[1] - vec[0]); for (int i = 2; i < n; i++) { int m = INT_MAX; rep(j, k) { m = min(m, dp[i - j - 1] + abs(vec[i] - vec[i - j - 1])); } dp[i] = m; } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; inline int ctoi(char c) { if (c < '0' || '9' < c) throw invalid_argument("ctoi error"); return c - '0'; } #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++) #define all(x) (x).begin(), (x).end() template <typename T> inline T gcd(T x, T y) { if (x <= 0 || y <= 0) throw invalid_argument("gcd error: x <= 0 or y <= 0"); if (x < y) swap(x, y); T r = x % y; while (r != 0) { x = y; y = r; r = x % y; } return y; } template <typename T> inline T lcm(T x, T y) { if (x <= 0 || y <= 0) throw invalid_argument("lcm error: x <= 0 or y <= 0"); return x * y / gcd(x, y); } int main() { int n, k; cin >> n >> k; vector<int> vec; rep(i, n) { int x; cin >> x; vec.push_back(x); } vector<int> dp(n); dp[0] = 0; dp[1] = abs(vec[1] - vec[0]); for (int i = 2; i < n; i++) { int m = INT_MAX; rep(j, k) { if (0 <= i - j - 1) m = min(m, dp[i - j - 1] + abs(vec[i] - vec[i - j - 1])); } dp[i] = m; } cout << dp[n - 1]; return 0; }
replace
53
54
53
57
0
p03161
C++
Runtime Error
// [email protected] // Aarsh Sharma #include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(char ch) { string s(1, ch); return '\'' + s + '\''; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef ONLINE_JUDGE #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define f(i, x, n) for (int i = x; i < n; i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define F first #define S second #define pb push_back #define endl "\n" #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); #define fast_io() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) typedef long long int ll; #define int ll typedef pair<ll, ll> pll; typedef vector<vector<ll>> matrix; typedef vector<ll> vll; const ll mod = 1e9 + 7; const ll inf = LLONG_MAX; const ll N = 1e5 + 10; int32_t main() { fast_io(); int n, k; cin >> n >> k; vll a(n); f(i, 0, n) cin >> a[i]; if (n == 2) { cout << abs(a[n - 1] - a[0]); return 0; } vll dp(n); dp[0] = 0; // dp[1] = abs(a[1] - a[0]); // f (i, 2, k) { // int mini = inf; // f (j, 1, i+1) { // if (abs(a[i]-a[i-j]) < mini) { // mini = abs(a[i]-a[i-j]); // dp[i] = dp[i-j] + abs(a[i]-a[i-j]); // } // } // dp[i] = mini; // } f(i, 1, n) { int mini = inf; for (int j = 1; j <= k; j++) { if (abs(a[i] - a[i - j]) + dp[i - j] < mini) { mini = abs(a[i] - a[i - j]) + dp[i - j]; } } dp[i] = mini; } debug(dp); cout << dp[n - 1]; return 0; }
// [email protected] // Aarsh Sharma #include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(char ch) { string s(1, ch); return '\'' + s + '\''; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef ONLINE_JUDGE #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define f(i, x, n) for (int i = x; i < n; i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define F first #define S second #define pb push_back #define endl "\n" #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); #define fast_io() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) typedef long long int ll; #define int ll typedef pair<ll, ll> pll; typedef vector<vector<ll>> matrix; typedef vector<ll> vll; const ll mod = 1e9 + 7; const ll inf = LLONG_MAX; const ll N = 1e5 + 10; int32_t main() { fast_io(); int n, k; cin >> n >> k; vll a(n); f(i, 0, n) cin >> a[i]; if (n == 2) { cout << abs(a[n - 1] - a[0]); return 0; } vll dp(n); dp[0] = 0; // dp[1] = abs(a[1] - a[0]); // f (i, 2, k) { // int mini = inf; // f (j, 1, i+1) { // if (abs(a[i]-a[i-j]) < mini) { // mini = abs(a[i]-a[i-j]); // dp[i] = dp[i-j] + abs(a[i]-a[i-j]); // } // } // dp[i] = mini; // } f(i, 1, n) { int mini = inf; for (int j = 1; j <= min(i, k); j++) { if (abs(a[i] - a[i - j]) + dp[i - j] < mini) { mini = abs(a[i] - a[i - j]) + dp[i - j]; } } dp[i] = mini; } debug(dp); cout << dp[n - 1]; return 0; }
replace
89
90
89
90
0
[dp]: {0, 20, 30, 40, 30}
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAX 100007 #define MOD 1000000007 long long frog(long long arr[], int n, int k) { long long output[n + 1]; for (int i = 0; i < n; i++) output[i] = INT_MAX; output[0] = 0; for (int i = 1; i < k; i++) { for (int j = i - 1; j >= 0; j--) { if (output[j] + abs(arr[i] - arr[j]) < output[i]) output[i] = output[j] + abs(arr[i] - arr[j]); } } for (int i = k; i < n; i++) { for (int j = i - 1; j >= i - k; j--) { if (output[j] + abs(arr[i] - arr[j]) < output[i]) output[i] = output[j] + abs(arr[i] - arr[j]); } } return output[n - 1]; } int main() { int n; cin >> n; int k; cin >> k; long long arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; cout << frog(arr, n, k); return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX 100007 #define MOD 1000000007 long long frog(long long arr[], int n, int k) { long long output[n + 1]; for (int i = 0; i < n; i++) output[i] = INT_MAX; output[0] = 0; for (int i = 1; i < min(k, n); i++) { for (int j = i - 1; j >= 0; j--) { if (output[j] + abs(arr[i] - arr[j]) < output[i]) output[i] = output[j] + abs(arr[i] - arr[j]); } } for (int i = k; i < n; i++) { for (int j = i - 1; j >= i - k; j--) { if (output[j] + abs(arr[i] - arr[j]) < output[i]) output[i] = output[j] + abs(arr[i] - arr[j]); } } return output[n - 1]; } int main() { int n; cin >> n; int k; cin >> k; long long arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; cout << frog(arr, n, k); return 0; }
replace
10
11
10
11
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 100001; int v[N]; int dp[N]; int n, k; int calc(int idx) { if (idx >= n - 1) return 0; int &ret = dp[idx]; if (ret != -1) return ret; ret = 1e9; for (int i = 1; i <= k; ++i) if (idx + i < n) ret = min(ret, calc(idx + i) + abs(v[idx] - v[idx + i])); return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("test.txt", "r", stdin); #endif cin >> n >> k; for (int i = 0; i < n; ++i) cin >> v[i]; memset(dp, -1, sizeof(dp)); cout << calc(0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100001; int v[N]; int dp[N]; int n, k; int calc(int idx) { if (idx >= n - 1) return 0; int &ret = dp[idx]; if (ret != -1) return ret; ret = 1e9; for (int i = 1; i <= k; ++i) if (idx + i < n) ret = min(ret, calc(idx + i) + abs(v[idx] - v[idx + i])); return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 0; i < n; ++i) cin >> v[i]; memset(dp, -1, sizeof(dp)); cout << calc(0) << endl; return 0; }
delete
22
25
22
22
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #include <string> #include <time.h> using namespace std; #define ll long long int #define pb push_back #define mp make_pair #define fi first #define se second void solve() { ll n, i, j, k; cin >> n >> k; vector<ll> a(n); for (i = 0; i < n; i++) cin >> a[i]; ll dp[n]; dp[0] = 0; dp[1] = abs(a[0] - a[1]); ll m = INT_MAX; for (i = 2; i < n; i++) { for (j = i - 1; j >= (i - k > 0 ? i - k : 0); j--) { m = min(m, abs(a[i] - a[j]) + dp[j]); // if(i==n-1) // cout<<abs(a[i]-a[j])<<" "<<dp[i-j]<<" "<<i<<" "<<j<<"\n"; } dp[i] = m; m = INT_MAX; } cout << dp[n - 1]; // for(i=0;i<n;i++) // cout<<dp[i]<<" "; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif clock_t tStart = clock(); ll t, k; // cin>>t; // while(t--) solve(); // printf("\nTime taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC); return 0; }
#include <bits/stdc++.h> #include <iostream> #include <string> #include <time.h> using namespace std; #define ll long long int #define pb push_back #define mp make_pair #define fi first #define se second void solve() { ll n, i, j, k; cin >> n >> k; vector<ll> a(n); for (i = 0; i < n; i++) cin >> a[i]; ll dp[n]; dp[0] = 0; dp[1] = abs(a[0] - a[1]); ll m = INT_MAX; for (i = 2; i < n; i++) { for (j = i - 1; j >= (i - k > 0 ? i - k : 0); j--) { m = min(m, abs(a[i] - a[j]) + dp[j]); // if(i==n-1) // cout<<abs(a[i]-a[j])<<" "<<dp[i-j]<<" "<<i<<" "<<j<<"\n"; } dp[i] = m; m = INT_MAX; } cout << dp[n - 1]; // for(i=0;i<n;i++) // cout<<dp[i]<<" "; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif clock_t tStart = clock(); ll t, k; // cin>>t; // while(t--) solve(); // printf("\nTime taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC); return 0; }
replace
36
40
36
40
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define PI 3.14159265358979323846 #define EPS 1e-6 #define INF 1000000000 // MAXIMIZAR f(x) == MINIMIZAR -f(x) #define _ \ ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0), \ cout.precision(15); #define FOR(i, a, b) for (int i = int(a); i < int(b); i++) #define FORC(i, a, b) for (int i = int(a); i >= int(b); i--) #define pb push_back #define fi first #define se second #define debug(x) cout << (#x) << " = " << x << "\n"; #define all(a) (a).begin(), (a).end() #define sz(a) (int)(a).size() clock_t t_; using namespace std; template <class T> void print(T s) { cout << s << "\n"; } template <class T> void emax(T &a, T b) { a = (a < b ? b : a); }; template <class T> void emin(T &a, T b) { a = (a > b ? b : a); } template <class T> void printC(T s) { for (auto &x : s) { cout << x << " "; } cout << "\n"; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ii> vii; typedef vector<vi> vvi; #define maxn 100005 #define mod 1000000007 const int inf = 1e9 + 5; int main(int argc, const char *argv[]) { #ifdef input freopen("input.txt", "r", stdin); #endif #ifdef output freopen("output.txt", "w", stdout); #endif t_ = clock(); int n, k; cin >> n >> k; vi info(n); for (auto &i : info) { cin >> i; } vi dp(n + 1, inf); dp[1] = 0; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= i + k; j++) { dp[j] = min(dp[j], dp[i] + abs(info[i - 1] - info[j - 1])); } } print(dp[n]); t_ = clock() - t_; cerr << setprecision(9) << fixed << ((float)t_ / CLOCKS_PER_SEC) << endl; return 0; }
#include <bits/stdc++.h> #define PI 3.14159265358979323846 #define EPS 1e-6 #define INF 1000000000 // MAXIMIZAR f(x) == MINIMIZAR -f(x) #define _ \ ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0), \ cout.precision(15); #define FOR(i, a, b) for (int i = int(a); i < int(b); i++) #define FORC(i, a, b) for (int i = int(a); i >= int(b); i--) #define pb push_back #define fi first #define se second #define debug(x) cout << (#x) << " = " << x << "\n"; #define all(a) (a).begin(), (a).end() #define sz(a) (int)(a).size() clock_t t_; using namespace std; template <class T> void print(T s) { cout << s << "\n"; } template <class T> void emax(T &a, T b) { a = (a < b ? b : a); }; template <class T> void emin(T &a, T b) { a = (a > b ? b : a); } template <class T> void printC(T s) { for (auto &x : s) { cout << x << " "; } cout << "\n"; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ii> vii; typedef vector<vi> vvi; #define maxn 100005 #define mod 1000000007 const int inf = 1e9 + 5; int main(int argc, const char *argv[]) { #ifdef input freopen("input.txt", "r", stdin); #endif #ifdef output freopen("output.txt", "w", stdout); #endif t_ = clock(); int n, k; cin >> n >> k; vi info(n); for (auto &i : info) { cin >> i; } vi dp(n + 1, inf); dp[1] = 0; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n && j <= i + k; j++) { dp[j] = min(dp[j], dp[i] + abs(info[i - 1] - info[j - 1])); } } print(dp[n]); t_ = clock() - t_; cerr << setprecision(9) << fixed << ((float)t_ / CLOCKS_PER_SEC) << endl; return 0; }
replace
65
66
65
66
-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)
p03161
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using vi = vector<int>; // intの1次元の型に vi という別名をつける using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける int MOD = 1000000007; // 10^9 + 7 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; } int main(int argc, char *argv[]) { int n, k; cin >> n >> k; vi h(n); rep(i, n) cin >> h[i]; // 無限大の値 const long long INF = 1LL << 60; // DP テーブル long long dp[100010]; // DP テーブル全体を初期化 (最小化問題:INF else:0) for (int i = 0; i < 100010; ++i) dp[i] = INF; // 初期条件 dp[0] = 0; // ループ for (int i = 0; i < n; ++i) { for (int j = 1; j < k + 1; ++j) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } } // 解を得て出力 cout << dp[n - 1] << endl; return 0; }
#include <cstdlib> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using vi = vector<int>; // intの1次元の型に vi という別名をつける using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける int MOD = 1000000007; // 10^9 + 7 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; } int main(int argc, char *argv[]) { int n, k; cin >> n >> k; vi h(n); rep(i, n) cin >> h[i]; // 無限大の値 const long long INF = 1LL << 60; // DP テーブル long long dp[100010]; // DP テーブル全体を初期化 (最小化問題:INF else:0) for (int i = 0; i < 100010; ++i) dp[i] = INF; // 初期条件 dp[0] = 0; // ループ for (int i = 0; i < n; ++i) { for (int j = 1; j < k + 1; ++j) { if (i + j < n) chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } } // 解を得て出力 cout << dp[n - 1] << endl; return 0; }
replace
47
48
47
49
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> #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 FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define llong long long #define pb(a) push_back(a) #define INF 999999999 using namespace std; typedef pair<int, int> P; typedef pair<llong, llong> LP; typedef pair<int, P> PP; typedef pair<llong, LP> LPP; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int main() { int N; cin >> N; int K; cin >> K; vector<int> h(N); REP(i, N) cin >> h[i]; vector<int> dp(N, INF); dp[0] = 0; FOR(i, 1, N) { FOR(j, max(i - K, 0), N) { dp[i] = min(dp[i], dp[j] + abs(h[j] - h[i])); } } cout << dp[N - 1] << endl; }
#include <bits/stdc++.h> #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 FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define llong long long #define pb(a) push_back(a) #define INF 999999999 using namespace std; typedef pair<int, int> P; typedef pair<llong, llong> LP; typedef pair<int, P> PP; typedef pair<llong, LP> LPP; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int main() { int N; cin >> N; int K; cin >> K; vector<int> h(N); REP(i, N) cin >> h[i]; vector<int> dp(N, INF); dp[0] = 0; FOR(i, 1, N) { FOR(j, max(i - K, 0), i) { dp[i] = min(dp[i], dp[j] + abs(h[j] - h[i])); } } cout << dp[N - 1] << endl; }
replace
28
29
28
29
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define lsb(x) (x & -x) using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("a.in", "r", stdin); int n, k; cin >> n >> k; vector<int> v(n + 1, 0); for (int i = 1; i <= n; i++) cin >> v[i]; vector<int> dp(n + 1, 1000000000); dp[1] = 0; for (int i = 2; i <= n; i++) for (int j = i - 1; j >= i - k; j--) dp[i] = min(dp[i], dp[j] + abs(v[i] - v[j])); cout << dp[n]; return 0; }
#include <bits/stdc++.h> #define ll long long #define lsb(x) (x & -x) using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("a.in", "r", stdin); int n, k; cin >> n >> k; vector<int> v(n + 1, 0); for (int i = 1; i <= n; i++) cin >> v[i]; vector<int> dp(n + 1, 1000000000); dp[1] = 0; for (int i = 2; i <= n; i++) for (int j = i - 1; j >= max(i - k, 1); j--) dp[i] = min(dp[i], dp[j] + abs(v[i] - v[j])); cout << dp[n]; return 0; }
replace
22
23
22
23
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; 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; } const ll INF = 10000000000; int main() { int n, k; cin >> n >> k; vector<int> h(n); vector<ll> dp(n, INF); for (int i = 0; i < n; ++i) { cin >> h[i]; } dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) { chmin(dp[i + j], dp[i] + abs(h[i + j] - h[i])); } } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; 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; } const ll INF = 10000000000; int main() { int n, k; cin >> n >> k; vector<int> h(n); vector<ll> dp(n, INF); for (int i = 0; i < n; ++i) { cin >> h[i]; } dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) { if (i + j > n) { continue; } chmin(dp[i + j], dp[i] + abs(h[i + j] - h[i])); } } cout << dp[n - 1] << endl; return 0; }
insert
32
32
32
35
-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)
p03161
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stdlib.h> #include <string> #include <utility> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define loop(i, x, n) for (int i = (x); i < (n); i++) #define all(v) (v).begin(), (v).end() #define int long long using namespace std; const int MOD = 1e9 + 7; const int INF = 1e9; template <typename T> void cmax(T &a, T b) { a = max(a, b); } template <typename T> void cmin(T &a, T b) { a = min(a, b); } signed main() { int n, k; cin >> n >> k; vector<int> h(n); rep(i, n) cin >> h[i]; vector<int> dp(n + 10, INF); dp[0] = 0; rep(i, n) loop(j, 1, k + 1) cmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); cout << dp[n - 1] << endl; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stdlib.h> #include <string> #include <utility> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define loop(i, x, n) for (int i = (x); i < (n); i++) #define all(v) (v).begin(), (v).end() #define int long long using namespace std; const int MOD = 1e9 + 7; const int INF = 1e9; template <typename T> void cmax(T &a, T b) { a = max(a, b); } template <typename T> void cmin(T &a, T b) { a = min(a, b); } signed main() { int n, k; cin >> n >> k; vector<int> h(n); rep(i, n) cin >> h[i]; vector<int> dp(n + 200, INF); dp[0] = 0; rep(i, n) loop(j, 1, k + 1) cmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); cout << dp[n - 1] << endl; return 0; }
replace
30
31
30
31
0
p03161
C++
Runtime Error
/** * purpose : * author : kyomukyomupurin * created : **/ #include <bits/stdc++.h> using namespace std; using int64 = int64_t; using pii = pair<int, int>; using pll = pair<int64_t, int64_t>; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(v) (v).begin(), (v).end() #define print(x) cout << (x) << '\n' #define PB push_back #define EB emplace_back #define MP make_pair #define fi first #define se second const int64 INF = 1e15; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; int64 h[n]; rep(i, n) cin >> h[i]; int64 dp[n]; // dp[i] : minimum const to reach 'i' fill(dp, dp + n, INF); dp[0] = 0; dp[1] = abs(h[0] - h[1]); for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } print(dp[n - 1]); return 0; }
/** * purpose : * author : kyomukyomupurin * created : **/ #include <bits/stdc++.h> using namespace std; using int64 = int64_t; using pii = pair<int, int>; using pll = pair<int64_t, int64_t>; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(v) (v).begin(), (v).end() #define print(x) cout << (x) << '\n' #define PB push_back #define EB emplace_back #define MP make_pair #define fi first #define se second const int64 INF = 1e15; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; int64 h[n]; rep(i, n) cin >> h[i]; int64 dp[100000 + 100]; // dp[i] : minimum const to reach 'i' rep(i, 100000 + 100) dp[i] = INF; dp[0] = 0; dp[1] = abs(h[0] - h[1]); for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } print(dp[n - 1]); return 0; }
replace
31
33
31
33
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define fo(i, n) for (i = 0; i < n; i++) #define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1) #define int long long int #define si(x) scanf("%d", &x) #define sl(x) scanf("%lld", &x) #define ss(s) scanf("%s", s) #define pi(x) printf("%d\n", x) #define pl(x) printf("%lld\n", x) #define ps(s) printf("%s\n", s) #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) #define PI 3.1415926535897932384626 #define INF 100000000000 typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vpii; typedef vector<vi> vvi; int mpow(int base, int exp); void ipgraph(int m); const int mod = 1000000007; const int N = 1e5 + 5, M = N; //========================================= int h[N], dp[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("inputf.in", "r", stdin); freopen("outputf.in", "w", stdout); #endif int n, k; cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> h[i]; dp[1] = 0; for (int i = 2; i <= n; ++i) { // dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); dp[i] = 2 * 1e9; for (int j = i - 1; j >= max(i - k, 1LL); --j) { dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j])); } } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define fo(i, n) for (i = 0; i < n; i++) #define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1) #define int long long int #define si(x) scanf("%d", &x) #define sl(x) scanf("%lld", &x) #define ss(s) scanf("%s", s) #define pi(x) printf("%d\n", x) #define pl(x) printf("%lld\n", x) #define ps(s) printf("%s\n", s) #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) #define PI 3.1415926535897932384626 #define INF 100000000000 typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pii> vpii; typedef vector<vi> vvi; int mpow(int base, int exp); void ipgraph(int m); const int mod = 1000000007; const int N = 1e5 + 5, M = N; //========================================= int h[N], dp[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> h[i]; dp[1] = 0; for (int i = 2; i <= n; ++i) { // dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); dp[i] = 2 * 1e9; for (int j = i - 1; j >= max(i - k, 1LL); --j) { dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j])); } } cout << dp[n] << endl; return 0; }
delete
39
44
39
39
TLE
p03161
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> int main() { int n, k; std::cin >> n >> k; std::vector<int> in(n); for (int i = 0; i < n; ++i) { std::cin >> in[i]; } std::vector<int> dp(n); for (int i = 0; i < k; i++) { dp[i] = std::abs(in[i] - in[0]); } // O(n*k) for (int i = 1; i < n; i++) { std::vector<int> currCosts; for (int j = 1; j <= k; j++) { if (i - j < 0) { break; } currCosts.push_back(dp[i - j] + std::abs(in[i] - in[i - j])); } dp[i] = *std::min_element(currCosts.begin(), currCosts.end()); } std::cout << dp[n - 1] << std::endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> int main() { int n, k; std::cin >> n >> k; std::vector<int> in(n); for (int i = 0; i < n; ++i) { std::cin >> in[i]; } std::vector<int> dp(n); // O(n*k) for (int i = 1; i < n; i++) { std::vector<int> currCosts; for (int j = 1; j <= k; j++) { if (i - j < 0) { break; } currCosts.push_back(dp[i - j] + std::abs(in[i] - in[i - j])); } dp[i] = *std::min_element(currCosts.begin(), currCosts.end()); } std::cout << dp[n - 1] << std::endl; return 0; }
delete
14
18
14
14
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define END '\n' #define int long long #define pb push_back #define pii pair<int, int> #define loop(i, a, b) for (int i = (a); i < (b); i++) #define loopb(i, b, a) for (int i = (b); i > (a); --i) void fastscan(int &x) { bool neg = false; register int c; x = 0; c = getchar(); if (c == '-') { neg = true; c = getchar(); } for (; (c > 47 && c < 58); c = getchar()) x = (x << 1) + (x << 3) + c - 48; if (neg) x *= -1; } string alpha = "abcdefghijklmnopqrstuvwxyz"; const int mod = 1e9 + 7; const int inf = 2e18 + 5; const int nax = 100010; const int mod1 = 998244353; signed main() { int n, k; cin >> n >> k; vector<int> v; loop(i, 0, n) { int x; cin >> x; v.pb(x); } int dp[n]; loop(i, 0, n) dp[i] = inf; dp[0] = 0; loop(i, 0, n) { loop(j, i + 1, i + k + 1) { dp[j] = min(dp[i] + abs(v[j] - v[i]), dp[j]); } } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; #define END '\n' #define int long long #define pb push_back #define pii pair<int, int> #define loop(i, a, b) for (int i = (a); i < (b); i++) #define loopb(i, b, a) for (int i = (b); i > (a); --i) void fastscan(int &x) { bool neg = false; register int c; x = 0; c = getchar(); if (c == '-') { neg = true; c = getchar(); } for (; (c > 47 && c < 58); c = getchar()) x = (x << 1) + (x << 3) + c - 48; if (neg) x *= -1; } string alpha = "abcdefghijklmnopqrstuvwxyz"; const int mod = 1e9 + 7; const int inf = 2e18 + 5; const int nax = 100010; const int mod1 = 998244353; signed main() { int n, k; cin >> n >> k; vector<int> v; loop(i, 0, n) { int x; cin >> x; v.pb(x); } int dp[n]; loop(i, 0, n) dp[i] = inf; dp[0] = 0; loop(i, 0, n) { loop(j, i + 1, min(i + k + 1, n)) { dp[j] = min(dp[i] + abs(v[j] - v[i]), dp[j]); } } cout << dp[n - 1]; return 0; }
replace
42
43
42
45
0
p03161
C++
Runtime Error
/* Author: Rennan Rocha * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$ **/ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define F first #define S second #define PB push_back #define MP make_pair #define ll long long #define vi vector<int> #define vll vector<ll> #define pi pair<int, int> #define pll pair<ll, ll> #define vpi vector<pi> #define vpll vector<pll> #define INF 1000000000000000000 using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //*find_by_order(); order_of_key(); int n; ll h[100010]; ll dp[100010]; int k; ll solve(int i) { if (i == n - 1) return 0; if (i >= n) return INF; if (dp[i] != -1) return dp[i]; for (int j = 1; j <= k; j++) { dp[i] = min(dp[i] == -1 ? INF : dp[i], min(abs(h[i] - h[i + j]) + solve(i + j), abs(h[i] - h[i + j]) + solve(i + j))); } return dp[i]; } int main() { ios_base::sync_with_stdio(false); memset(dp, -1, sizeof dp); cin >> n >> k; for (int i = 0; i < n; i++) cin >> h[i]; cout << solve(0) << "\n"; }
/* Author: Rennan Rocha * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$ **/ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define F first #define S second #define PB push_back #define MP make_pair #define ll long long #define vi vector<int> #define vll vector<ll> #define pi pair<int, int> #define pll pair<ll, ll> #define vpi vector<pi> #define vpll vector<pll> #define INF 1000000000000000000 using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //*find_by_order(); order_of_key(); int n; ll h[100110]; ll dp[100110]; int k; ll solve(int i) { if (i == n - 1) return 0; if (i >= n) return INF; if (dp[i] != -1) return dp[i]; for (int j = 1; j <= k; j++) { dp[i] = min(dp[i] == -1 ? INF : dp[i], min(abs(h[i] - h[i + j]) + solve(i + j), abs(h[i] - h[i + j]) + solve(i + j))); } return dp[i]; } int main() { ios_base::sync_with_stdio(false); memset(dp, -1, sizeof dp); cin >> n >> k; for (int i = 0; i < n; i++) cin >> h[i]; cout << solve(0) << "\n"; }
replace
31
33
31
33
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; typedef double ld; #define rep(i, a, n) for (ll i = (a); i < (n); i++) #define per(i, a, n) for (ll i = (n - 1); i >= (a); i--) #define F first #define S second #define maxx(a, b) a = max(a, b) #define minn(a, b) a = min(a, b) #define db(x) cerr << #x << " = " << x << endl #define N 1111111 #define mod 1000000007 #define rt return #define inf 12345678908761 using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("Ulug'bek","r",stdin); // freopen("Abdimanabov","w",stdout); int n, k; cin >> n >> k; vector<int> v(n), ans(n, INT_MAX); for (auto &x : v) cin >> x; ans[0] = 0; rep(i, 2, n) rep(j, 1, k + 1) if (i - j >= 0) minn(ans[i], abs(v[i] - v[i - k]) + ans[i - k]); cout << ans[n - 1]; rt 0; }
#include <bits/stdc++.h> typedef long long ll; typedef double ld; #define rep(i, a, n) for (ll i = (a); i < (n); i++) #define per(i, a, n) for (ll i = (n - 1); i >= (a); i--) #define F first #define S second #define maxx(a, b) a = max(a, b) #define minn(a, b) a = min(a, b) #define db(x) cerr << #x << " = " << x << endl #define N 1111111 #define mod 1000000007 #define rt return #define inf 12345678908761 using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("Ulug'bek","r",stdin); // freopen("Abdimanabov","w",stdout); int n, k; cin >> n >> k; vector<int> v(n), ans(n, INT_MAX); for (auto &x : v) cin >> x; ans[0] = 0; rep(i, 1, n) rep(j, 1, k + 1) if (i - j >= 0) minn(ans[i], abs(v[i] - v[i - j]) + ans[i - j]); cout << ans[n - 1]; rt 0; }
replace
33
35
33
35
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const long long INF = 1LL << 60; template <class T> inline void chmin(T &a, T b) { if (a > b) { a = b; return; } return; } int main() { int n, k; cin >> n >> k; vector<int> h(n); for (int i = 0; i < n; i++) cin >> h[i]; long long dp[1000000]; for (int i = 0; i < n; i++) { dp[i] = INF; } dp[0] = 0; for (int i = 1; i < n; i++) { for (int j = 1; j <= i; j++) { if (k - j >= 0) chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1LL << 60; template <class T> inline void chmin(T &a, T b) { if (a > b) { a = b; return; } return; } int main() { int n, k; cin >> n >> k; vector<int> h(n); for (int i = 0; i < n; i++) cin >> h[i]; long long dp[1000000]; for (int i = 0; i < n; i++) { dp[i] = INF; } dp[0] = 0; for (int i = 1; i < n; i++) { for (int j = 1; j <= i; j++) { if (k - j >= 0) chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j])); else break; } } cout << dp[n - 1] << endl; return 0; }
insert
30
30
30
32
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define ll long long int #define ull unsigned long long int #define ld long double #define mod 1000000007 #define inf 1000000000000000007 #define eps 0.000001 #define pi 3.141592653589793 #define pii pair<int, int> #define pdd pair<ld, ld> #define pll pair<ll, ll> #define ff first #define ss second #define vii vector<int> #define vpl vector<pll> #define vll vector<ll> #define sti stack<int> #define stll stack<ll> #define mseti set<ll> #define msetd multiset<ll, greater<ll>> #define mp make_pair #define pb push_back #define lb lower_bound #define ub upper_bound #define stp setprecision(20) // use fixed before stp #define endl '\n' int main() { FAST #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif ll n, k; cin >> n >> k; ll a[n], dp[n]; for (ll i = 0; i < n; i++) { cin >> a[i]; dp[i] = inf; } dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (ll i = 2; i < n; i++) { for (ll j = (i - 1); j >= (i - k); j--) { if (j < 0) break; dp[i] = min(dp[i], (dp[j] + abs(a[j] - a[i]))); } } cout << dp[n - 1]; }
#include <bits/stdc++.h> using namespace std; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define ll long long int #define ull unsigned long long int #define ld long double #define mod 1000000007 #define inf 1000000000000000007 #define eps 0.000001 #define pi 3.141592653589793 #define pii pair<int, int> #define pdd pair<ld, ld> #define pll pair<ll, ll> #define ff first #define ss second #define vii vector<int> #define vpl vector<pll> #define vll vector<ll> #define sti stack<int> #define stll stack<ll> #define mseti set<ll> #define msetd multiset<ll, greater<ll>> #define mp make_pair #define pb push_back #define lb lower_bound #define ub upper_bound #define stp setprecision(20) // use fixed before stp #define endl '\n' int main() { FAST ll n, k; cin >> n >> k; ll a[n], dp[n]; for (ll i = 0; i < n; i++) { cin >> a[i]; dp[i] = inf; } dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (ll i = 2; i < n; i++) { for (ll j = (i - 1); j >= (i - k); j--) { if (j < 0) break; dp[i] = min(dp[i], (dp[j] + abs(a[j] - a[i]))); } } cout << dp[n - 1]; }
replace
31
37
31
32
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mod 1000000000 #define ll long long #define ull unsigned long long #define vi vector<ll> #define vs vector<string> #define psi pair<string, ll> #define pii pair<ll, ll> #define vsi vector<psi> #define vii vector<pii> #define mp make_pair #define pb push_back #define rep(n) for (long i = 0; i < n; i++) #define repr(n) for (long i = (n - 1); i >= 0; i--) #define fori(i, a, b) for (auto i = a; i <= b; i++) #define ford(i, b, a) for (auto i = b; i >= a; i--) #define mset(a, i) memset(a, i, sizeof(a)) #define minv(a) min_element(a.begin(), a.end()) #define maxv(a) max_element(a.begin(), a.end()) #define min3(a, b, c) min(a, min(b, c)) #define max3(a, b, c) max(a, max(b, c)) #define sotit(a) sort(a.begin(), a.end()); #define sot(a) sort(a, a + n); #define ff first #define maxheap(v) priority_queue<v> #define minheap(v) priority_queue<v, vector<v>, greater<v>> #define ss second #define endl "\n" #define NL cout << endl; #define printmat(a, m, n) \ for (int i = 0; i < m; i++) { \ for (int j = 0; j < n; j++) { \ cout << a[i][j] << " "; \ } \ cout << endl; \ } #define dbgn(x) cout << " $ " << x << " $ " << endl; #define dbg(x) cout << " $ " << x << " $ "; #define all(v) v.begin(), v.end() #define len(s) (ll)(s.size()) #define show(a, n) \ for (int i = 0; i < n; i++) { \ cout << a[i] << " "; \ } \ NL #define fast_io() \ cin.sync_with_stdio(false); \ cout.sync_with_stdio(false); \ cin.tie(NULL); // lower_bound - which does not compare less than val // upper_bound - which compares greater than val int main() { fast_io() #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll t; // cin>>t; t = 1; while (t--) { ll n, k; cin >> n >> k; ll h[n], dp[n]; rep(n) { cin >> h[i]; } mset(dp, 0); dp[1] = abs(h[1] - h[0]); ll ans; fori(i, 2, n - 1) { ans = LLONG_MAX; fori(j, 1, min((ll)i, k)) { if (i - j >= 0) ans = min(ans, dp[i - j] + abs(h[i] - h[i - j])); } dp[i] = ans; } cout << dp[n - 1]; } return 0; }
#include <bits/stdc++.h> using namespace std; #define mod 1000000000 #define ll long long #define ull unsigned long long #define vi vector<ll> #define vs vector<string> #define psi pair<string, ll> #define pii pair<ll, ll> #define vsi vector<psi> #define vii vector<pii> #define mp make_pair #define pb push_back #define rep(n) for (long i = 0; i < n; i++) #define repr(n) for (long i = (n - 1); i >= 0; i--) #define fori(i, a, b) for (auto i = a; i <= b; i++) #define ford(i, b, a) for (auto i = b; i >= a; i--) #define mset(a, i) memset(a, i, sizeof(a)) #define minv(a) min_element(a.begin(), a.end()) #define maxv(a) max_element(a.begin(), a.end()) #define min3(a, b, c) min(a, min(b, c)) #define max3(a, b, c) max(a, max(b, c)) #define sotit(a) sort(a.begin(), a.end()); #define sot(a) sort(a, a + n); #define ff first #define maxheap(v) priority_queue<v> #define minheap(v) priority_queue<v, vector<v>, greater<v>> #define ss second #define endl "\n" #define NL cout << endl; #define printmat(a, m, n) \ for (int i = 0; i < m; i++) { \ for (int j = 0; j < n; j++) { \ cout << a[i][j] << " "; \ } \ cout << endl; \ } #define dbgn(x) cout << " $ " << x << " $ " << endl; #define dbg(x) cout << " $ " << x << " $ "; #define all(v) v.begin(), v.end() #define len(s) (ll)(s.size()) #define show(a, n) \ for (int i = 0; i < n; i++) { \ cout << a[i] << " "; \ } \ NL #define fast_io() \ cin.sync_with_stdio(false); \ cout.sync_with_stdio(false); \ cin.tie(NULL); // lower_bound - which does not compare less than val // upper_bound - which compares greater than val int main() { fast_io() ll t; // cin>>t; t = 1; while (t--) { ll n, k; cin >> n >> k; ll h[n], dp[n]; rep(n) { cin >> h[i]; } mset(dp, 0); dp[1] = abs(h[1] - h[0]); ll ans; fori(i, 2, n - 1) { ans = LLONG_MAX; fori(j, 1, min((ll)i, k)) { if (i - j >= 0) ans = min(ans, dp[i - j] + abs(h[i] - h[i - j])); } dp[i] = ans; } cout << dp[n - 1]; } return 0; }
replace
58
64
58
59
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int frog(std::vector<int> v, int n, int k) { int dp[100001] = {0}; dp[0] = 0; dp[1] = abs(v[0] - v[1]); for (int i = 2; i <= n; i++) { int ans = INT_MAX; for (int j = 1; j <= k; j++) { int a1 = dp[i - j] + abs(v[i] - v[i - j]); ans = min(ans, a1); } dp[i] = ans; } return dp[n - 1]; } int main() { int n; cin >> n; int k; cin >> k; std::vector<int> v; for (int i = 0; i < n; i++) { int a; cin >> a; v.push_back(a); } int ans = frog(v, n, k); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int frog(std::vector<int> v, int n, int k) { int dp[100001] = {0}; dp[0] = 0; dp[1] = abs(v[0] - v[1]); for (int i = 2; i <= n; i++) { int ans = INT_MAX; for (int j = 1; j <= k; j++) { int a1 = 0; if ((i - j) >= 0) { a1 = dp[i - j] + abs(v[i] - v[i - j]); } else { break; } ans = min(ans, a1); } dp[i] = ans; } return dp[n - 1]; } int main() { int n; cin >> n; int k; cin >> k; std::vector<int> v; for (int i = 0; i < n; i++) { int a; cin >> a; v.push_back(a); } int ans = frog(v, n, k); cout << ans << endl; return 0; }
replace
9
10
9
15
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int min_jumps[n]; min_jumps[0] = 0; for (int i = 1; i < k; i++) min_jumps[i] = abs(arr[0] - arr[i]); for (int i = k; i < n; i++) { min_jumps[i] = INT_MAX; // cout << min_jumps[i] << " " << i << " " << // abs(arr[i]-arr[i-1])+min_jumps[i-1] << endl; for (int j = 1; j <= k; j++) { if (min_jumps[i] > abs(arr[i] - arr[i - j]) + min_jumps[i - j]) min_jumps[i] = abs(arr[i] - arr[i - j]) + min_jumps[i - j]; } } // for(int i =0; i<n;i++) cout << min_jumps[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int min_jumps[max(n, k)]; min_jumps[0] = 0; for (int i = 1; i < k; i++) min_jumps[i] = abs(arr[0] - arr[i]); for (int i = k; i < n; i++) { min_jumps[i] = INT_MAX; // cout << min_jumps[i] << " " << i << " " << // abs(arr[i]-arr[i-1])+min_jumps[i-1] << endl; for (int j = 1; j <= k; j++) { if (min_jumps[i] > abs(arr[i] - arr[i - j]) + min_jumps[i - j]) min_jumps[i] = abs(arr[i] - arr[i - j]) + min_jumps[i - j]; } } // for(int i =0; i<n;i++) cout << min_jumps[n - 1] << endl; return 0; }
replace
11
12
11
12
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll i, a[10000] = {0}, dp[10000] = {0}, n, cost, k; cin >> n >> k; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 2; i <= n; i++) { if (i == 2) dp[i] = abs(a[i - 1] - a[i - 2]); else { ll mini = INT_MAX, z; for (z = 1; z <= k; z++) { cost = dp[i - z] + abs(a[i - 1] - a[i - 1 - z]); mini = min(cost, mini); if (i - 1 - z == 0) break; } dp[i] = mini; } } cout << dp[n]; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll i, a[100001] = {0}, dp[100001] = {0}, n, cost, k; cin >> n >> k; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 2; i <= n; i++) { if (i == 2) dp[i] = abs(a[i - 1] - a[i - 2]); else { ll mini = INT_MAX, z; for (z = 1; z <= k; z++) { cost = dp[i - z] + abs(a[i - 1] - a[i - 1 - z]); mini = min(cost, mini); if (i - 1 - z == 0) break; } dp[i] = mini; } } cout << dp[n]; }
replace
4
5
4
5
0
p03161
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 vi = vector<int>; using vvi = vector<vi>; int main() { int n, k; cin >> n >> k; vi h(n + 1); rep(i, n) cin >> h[i + 1]; vi dp(n + 1); dp[0] = 0; dp[1] = 0; for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); for (int j = 1; j <= k; j++) { dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[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 vi = vector<int>; using vvi = vector<vi>; int main() { int n, k; cin >> n >> k; vi h(n + 1); rep(i, n) cin >> h[i + 1]; vi dp(n + 1); dp[0] = 0; dp[1] = 0; for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); for (int j = 1; j <= k; j++) { if (i - j <= 0) continue; dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[n] << endl; }
insert
18
18
18
20
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int *arr = new int[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int dp[n]; for (int i = 0; i < n; i++) { dp[i] = INT_MAX; } dp[0] = 0; dp[1] = abs(arr[1] - arr[0]); for (int i = 2; i < n; i++) { int res = INT_MAX; for (int j = 1; j <= k; j++) { res = min(res, dp[i - j] + abs(arr[i - j] - arr[i])); } dp[i] = res; } cout << dp[n - 1] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int *arr = new int[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int dp[n]; for (int i = 0; i < n; i++) { dp[i] = INT_MAX; } dp[0] = 0; dp[1] = abs(arr[1] - arr[0]); for (int i = 2; i < n; i++) { int res = INT_MAX; for (int j = 1; j <= k; j++) { if (i - j >= 0) res = min(res, dp[i - j] + abs(arr[i - j] - arr[i])); } dp[i] = res; } cout << dp[n - 1] << "\n"; return 0; }
replace
23
24
23
25
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; vector<int> arr; int min(int a, int b) { return a < b ? a : b; } int main() { int n, k; cin >> n >> k; arr.resize(n); for (int i = 0; i < n; i++) cin >> arr[i]; vector<int> ans(n, INT_MAX); ans[0] = 0; ans[1] = abs(arr[0] - arr[1]); for (int i = 2; i < n; i++) { // ans[i]=min(abs(arr[i]-arr[i-1])+ans[i-1],abs(arr[i]-arr[i-2])+ans[i-2]); for (int j = i - 1; j >= i - k; j--) { ans[i] = min(ans[i], abs(arr[i] - arr[j]) + ans[j]); } } cout << ans[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> arr; int min(int a, int b) { return a < b ? a : b; } int main() { int n, k; cin >> n >> k; arr.resize(n); for (int i = 0; i < n; i++) cin >> arr[i]; vector<int> ans(n, INT_MAX); ans[0] = 0; ans[1] = abs(arr[0] - arr[1]); for (int i = 2; i < n; i++) { // ans[i]=min(abs(arr[i]-arr[i-1])+ans[i-1],abs(arr[i]-arr[i-2])+ans[i-2]); for (int j = i - 1; j >= i - k; j--) { if (j < 0) break; ans[i] = min(ans[i], abs(arr[i] - arr[j]) + ans[j]); } } cout << ans[n - 1]; return 0; }
insert
16
16
16
18
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const long long INF = 1LL << 60; int N; long long h[1000010]; long long dp[1000010]; int main() { int N, K; cin >> N >> K; for (int i = 0; i < N; i++) cin >> h[i]; for (int i = 0; i < N; i++) dp[i] = INF; dp[0] = 0; for (int i = 0; i < N; i++) { for (int j = 1; j <= K; K++) { chmin(dp[i + j], dp[i] + abs(h[i + j] - h[i])); } } cout << dp[N - 1] << endl; }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const long long INF = 1LL << 60; int N; long long h[1000010]; long long dp[1000010]; int main() { int N, K; cin >> N >> K; for (int i = 0; i < N; i++) cin >> h[i]; for (int i = 0; i < N; i++) dp[i] = INF; dp[0] = 0; for (int i = 0; i < N; i++) { for (int j = 1; j <= K; j++) { chmin(dp[i + j], dp[i] + abs(h[i + j] - h[i])); } } cout << dp[N - 1] << endl; }
replace
30
31
30
31
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #define all(a) (a).begin(), (a).end() #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define sz size #define fr first #define sc second #define pb push_back #define er erase #define in insert #define mp make_pair #define pi pair #define rc(s) return cout << s, 0 #define int long long using namespace std; int n, h[100005], dp[100005], k; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cerr.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> h[i]; dp[1] = 0; for (int i = 2; i <= n; i++) dp[i] = 1e18; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { dp[i + j] = min(dp[i] + abs(h[i] - h[i + j]), dp[i + j]); } } cout << dp[n]; }
#include <bits/stdc++.h> #define ll long long #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #define all(a) (a).begin(), (a).end() #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define sz size #define fr first #define sc second #define pb push_back #define er erase #define in insert #define mp make_pair #define pi pair #define rc(s) return cout << s, 0 #define int long long using namespace std; int n, h[150005], dp[150005], k; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cerr.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> h[i]; dp[1] = 0; for (int i = 2; i <= n; i++) dp[i] = 1e18; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { dp[i + j] = min(dp[i] + abs(h[i] - h[i + j]), dp[i + j]); } } cout << dp[n]; }
replace
18
19
18
19
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> h(n + k); for (int i = 0; i < n; ++i) { cin >> h[i]; } vector<int> dp(n + 2, INT_MAX); dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) dp[i + j] = min(dp[i] + abs(h[i] - h[i + j]), dp[i + j]); } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> h(n + k); for (int i = 0; i < n; ++i) { cin >> h[i]; } vector<int> dp(n + k, INT_MAX); dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) dp[i + j] = min(dp[i] + abs(h[i] - h[i + j]), dp[i + j]); } cout << dp[n - 1] << endl; return 0; }
replace
10
11
10
11
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; int minima(int i, int a[], int h[], int k) { int arr[k]; int min = abs(h[i - 1] - h[i]) + a[i - 1]; for (int j = 0; j < k; ++j) { arr[j] = abs(h[i - j - 1] - h[i]) + a[i - j - 1]; if (arr[j] < min) min = arr[j]; } return min; } int main() { int n, k; cin >> n >> k; int h[n]; for (int i = 0; i < n; ++i) cin >> h[i]; int a[n]; for (int i = 0; i <= k; ++i) a[i] = abs(h[i] - h[0]); for (int i = k + 1; i < n; ++i) a[i] = minima(i, a, h, k); cout << a[n - 1]; }
#include <bits/stdc++.h> #include <iostream> using namespace std; int minima(int i, int a[], int h[], int k) { int arr[k]; int min = abs(h[i - 1] - h[i]) + a[i - 1]; for (int j = 0; j < k; ++j) { arr[j] = abs(h[i - j - 1] - h[i]) + a[i - j - 1]; if (arr[j] < min) min = arr[j]; } return min; } int main() { int n, k; cin >> n >> k; int h[n]; for (int i = 0; i < n; ++i) cin >> h[i]; int a[n]; for (int i = 0; i <= k && i < n; ++i) a[i] = abs(h[i] - h[0]); for (int i = k + 1; i < n; ++i) a[i] = minima(i, a, h, k); cout << a[n - 1]; }
replace
20
21
20
21
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define INF 1000000000 using namespace std; int minCost(vector<int> &v, int index, int k, vector<int> &cost) { if (cost[index] != -1) { return cost[index]; } if (index <= 0) { return 0; } else if (index == 1) { return cost[index] = abs(v[1] - v[0]); } else { int c = 0; int minimum = INF; for (int i = 1; i <= k; i++) { c = abs(v[index] - v[index - i]) + minCost(v, index - i, k, cost); minimum = min(minimum, c); } return cost[index] = minimum; } } int32_t main() { int n, k; cin >> n >> k; vector<int> v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } vector<int> cost; for (int i = 0; i < n; i++) { cost.push_back(-1); } cost[0] = 0; cout << minCost(v, v.size() - 1, k, cost) << endl; return 0; }
#include <bits/stdc++.h> #define int long long #define INF 1000000000 using namespace std; int minCost(vector<int> &v, int index, int k, vector<int> &cost) { if (cost[index] != -1) { return cost[index]; } if (index <= 0) { return 0; } else if (index == 1) { return cost[index] = abs(v[1] - v[0]); } else { int c = 0; int minimum = INF; for (int i = 1; i <= k; i++) { if (index - i >= 0) { c = abs(v[index] - v[index - i]) + minCost(v, index - i, k, cost); minimum = min(minimum, c); } } return cost[index] = minimum; } } int32_t main() { int n, k; cin >> n >> k; vector<int> v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } vector<int> cost; for (int i = 0; i < n; i++) { cost.push_back(-1); } cost[0] = 0; cout << minCost(v, v.size() - 1, k, cost) << endl; return 0; }
replace
17
19
17
21
0
p03161
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int N, K; cin >> N >> K; vector<int> heights(N); for (int i = 0; i < N; i++) cin >> heights[i]; vector<long> dp(N, 0); for (int i = 1; i < N; i++) { dp[i] = dp[i - 1] + abs(heights[i] - heights[i - 1]); for (int k = 2; k <= K; k++) { dp[i] = min(dp[i], abs(heights[i] - heights[i - k]) + dp[i - k]); } } cout << dp[N - 1] << endl; return 0; // EDABC }
#include <iostream> #include <vector> using namespace std; int main() { int N, K; cin >> N >> K; vector<int> heights(N); for (int i = 0; i < N; i++) cin >> heights[i]; vector<long> dp(N, 0); for (int i = 1; i < N; i++) { dp[i] = dp[i - 1] + abs(heights[i] - heights[i - 1]); // i - k >= 0 i >= k for (int k = 2; k <= min(i, K); k++) { dp[i] = min(dp[i], abs(heights[i] - heights[i - k]) + dp[i - k]); } } cout << dp[N - 1] << endl; return 0; // EDABC }
replace
17
18
17
19
0
p03161
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> #define INF 1000000000 using namespace std; int main(void) { int n, k; cin >> n >> k; vector<int> h(n); for (int i = 0; i < n; i++) cin >> h[i]; vector<int> cost(n, INF); cost[0] = 0; for (int i = 0; i < n; i++) { int m = min(k, n - i); for (int j = 1; j <= m; j++) { cost[i + j] = min(cost[i + j], cost[i] + abs(h[i + j] - h[i])); } } cout << cost[n - 1] << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> #define INF 1000000000 using namespace std; int main(void) { int n, k; cin >> n >> k; vector<int> h(n); for (int i = 0; i < n; i++) cin >> h[i]; vector<int> cost(n, INF); cost[0] = 0; for (int i = 0; i < n; i++) { int m = min(k, n - 1 - i); for (int j = 1; j <= m; j++) { cost[i + j] = min(cost[i + j], cost[i] + abs(h[i + j] - h[i])); } } cout << cost[n - 1] << endl; return 0; }
replace
14
15
14
15
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long int const int N = 1e5 + 5; ll dp[N] = {-1}, v[N], k, n; const int inf = 0x3f3f3f3f; ll check(int index); int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll i, j, m; memset(dp, -1, sizeof(dp)); cin >> n >> k; for (i = 0; i < n; i++) cin >> v[i]; cout << check(0) << "\n"; return 0; } ll check(int index) { if (index >= n) return inf; else if (dp[index] != -1) return dp[index]; else if (index == n - 1) return dp[index] = 0; ll var = inf; for (int i = index + 1; i <= min(n - 1, k + index); i++) var = min(var, abs(v[index] - v[i]) + check(i)); return var; }
#include <bits/stdc++.h> using namespace std; #define ll long long int const int N = 1e5 + 5; ll dp[N] = {-1}, v[N], k, n; const int inf = 0x3f3f3f3f; ll check(int index); int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll i, j, m; memset(dp, -1, sizeof(dp)); cin >> n >> k; for (i = 0; i < n; i++) cin >> v[i]; cout << check(0) << "\n"; return 0; } ll check(int index) { if (index >= n) return inf; else if (dp[index] != -1) return dp[index]; else if (index == n - 1) return dp[index] = 0; ll var = inf; for (int i = index + 1; i <= min(n - 1, k + index); i++) var = min(var, abs(v[index] - v[i]) + check(i)); return dp[index] = var; }
replace
33
34
33
34
TLE
p03161
C++
Time Limit Exceeded
#define watch5(a, b, c, d, e) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define watch4(a, b, c, d) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << endl; #define watch3(a, b, c) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << endl; #define printclock \ cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n"; #define sharingan cerr << "\n-------------------------------------------\n\n"; #define watch2(a, b) \ cerr << #a << ": " << a << " | " << #b << ": " << b << endl; #define Yup \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define watch(a) cerr << #a << ": " << a << endl; #define __gcd(a, b) __algo_gcd(a, b) #define PI 3.14159265358979323846 #define mod 1000000007 #include <algorithm> #include <array> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define endl "\n" #define int long long int k; int dp[100001]; int go(int i, vector<int> &v) { int n = v.size(); if (i == n - 1) return 0; int ans = LLONG_MAX; for (int j = i + 1; j <= n - 1 && j <= i + k; j++) { ans = min(ans, go(j, v) + abs(v[i] - v[j])); } return dp[i] = ans; } int32_t main() { Yup; int n; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } memset(dp, -1, sizeof(dp)); cout << go(0, v); }
#define watch5(a, b, c, d, e) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define watch4(a, b, c, d) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << endl; #define watch3(a, b, c) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << endl; #define printclock \ cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n"; #define sharingan cerr << "\n-------------------------------------------\n\n"; #define watch2(a, b) \ cerr << #a << ": " << a << " | " << #b << ": " << b << endl; #define Yup \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define watch(a) cerr << #a << ": " << a << endl; #define __gcd(a, b) __algo_gcd(a, b) #define PI 3.14159265358979323846 #define mod 1000000007 #include <algorithm> #include <array> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define endl "\n" #define int long long int k; int dp[100001]; int go(int i, vector<int> &v) { int n = v.size(); if (i == n - 1) return 0; if (dp[i] != -1) return dp[i]; int ans = LLONG_MAX; for (int j = i + 1; j <= n - 1 && j <= i + k; j++) { ans = min(ans, go(j, v) + abs(v[i] - v[j])); } return dp[i] = ans; } int32_t main() { Yup; int n; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } memset(dp, -1, sizeof(dp)); cout << go(0, v); }
insert
52
52
52
54
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } int main() { int n = ri(), k = ri(); int a[n]; for (int i = 0; i < n; i++) a[i] = ri(); std::set<std::pair<int, int>> to_list; for (int i = 0; i < k; i++) to_list.insert({a[i], i}); int64_t dp[n]; for (int i = 0; i < n; i++) dp[i] = 1000000000000000000; dp[0] = 0; auto dp_go = [&](int dest, int origin) { dp[dest] = std::min(dp[dest], dp[origin] + std::abs(a[dest] - a[origin])); }; for (int i = 0; i + 1 < n; i++) { to_list.erase({a[i], i}); if (i + k < n) to_list.insert({a[i + k], i + k}); auto itr = to_list.lower_bound({a[i], 0}); if (itr != to_list.end()) dp_go(itr->second, i); if (itr != to_list.begin()) dp_go(to_list.lower_bound({std::prev(itr)->first, 0})->second, i); } std::cout << dp[n - 1] << std::endl; return 0; }
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } int main() { int n = ri(), k = ri(); k = std::min(k, n - 1); int a[n]; for (int i = 0; i < n; i++) a[i] = ri(); std::set<std::pair<int, int>> to_list; for (int i = 0; i < k; i++) to_list.insert({a[i], i}); int64_t dp[n]; for (int i = 0; i < n; i++) dp[i] = 1000000000000000000; dp[0] = 0; auto dp_go = [&](int dest, int origin) { dp[dest] = std::min(dp[dest], dp[origin] + std::abs(a[dest] - a[origin])); }; for (int i = 0; i + 1 < n; i++) { to_list.erase({a[i], i}); if (i + k < n) to_list.insert({a[i + k], i + k}); auto itr = to_list.lower_bound({a[i], 0}); if (itr != to_list.end()) dp_go(itr->second, i); if (itr != to_list.begin()) dp_go(to_list.lower_bound({std::prev(itr)->first, 0})->second, i); } std::cout << dp[n - 1] << std::endl; return 0; }
insert
10
10
10
11
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define lli long long int #define REP(i, s, n) for (int i = s; i < n; i++) #define MOD 1000000007 #define NUM 2520 #define INF (1LL << 50) #define DEBUG 0 #define mp(a, b) make_pair(a, b) #define SORT(V) sort(V.begin(), V.end()) #define PI (3.141592653589794) lli dp[101000]; int main() { lli n, k; cin >> n >> k; vector<lli> v; v.resize(n + k); REP(i, 0, 101000) dp[i] = INF; dp[0] = 0; REP(i, 0, n) cin >> v[i]; REP(i, n, n + k + 10) v[i] = INF; REP(i, 0, n) { REP(j, 1, k + 1) dp[i + j] = min(dp[i + j], dp[i] + abs(v[i] - v[i + j])); } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define lli long long int #define REP(i, s, n) for (int i = s; i < n; i++) #define MOD 1000000007 #define NUM 2520 #define INF (1LL << 50) #define DEBUG 0 #define mp(a, b) make_pair(a, b) #define SORT(V) sort(V.begin(), V.end()) #define PI (3.141592653589794) lli dp[101000]; int main() { lli n, k; cin >> n >> k; vector<lli> v; v.resize(n + k + 10); REP(i, 0, 101000) dp[i] = INF; dp[0] = 0; REP(i, 0, n) cin >> v[i]; REP(i, n, n + k + 10) v[i] = INF; REP(i, 0, n) { REP(j, 1, k + 1) dp[i + j] = min(dp[i + j], dp[i] + abs(v[i] - v[i + j])); } cout << dp[n - 1] << endl; return 0; }
replace
20
21
20
21
-6
malloc(): corrupted top size
p03161
C++
Runtime Error
#include <bits/stdc++.h> #include <utility> using namespace std; const long long N = 200001; const long long K = 201; long long n, k; long long arr[N]; long long matrix[N][K]; long long checkmin(long long i) { long long res = matrix[i][0]; for (long long j = 0; j < k; j++) { if (matrix[i][j] >= 0) { res = min(res, matrix[i][j]); } } return res; } int main() { cin >> n >> k; for (long long i = 0; i < n; i++) { cin >> arr[i]; } for (long long r = 0; r < k; r++) { for (long long f = 0; f < n; f++) { if (r > n - f - 2) matrix[f][r] = -1; } matrix[n - 1][r] = 0; matrix[n - r - 2][r] = abs(arr[n - r - 2] - arr[n - 1]); } for (long long j = n - 2; j >= 0; j--) { for (long long h = 0; h < k; h++) { if (h < n - j - 1) { matrix[j][h] = abs(arr[j] - arr[j + h + 1]) + checkmin(j + h + 1); } } } long long res = matrix[0][0]; long long maximo = -1; for (long long a = 0; a < k; a++) { res = min(res, matrix[0][a]); maximo = max(maximo, matrix[0][a]); } if (maximo == -1) res = 0; else if (res == -1) { res = maximo; for (long long a = 0; a < k; a++) { if (matrix[0][a] != -1) { res = min(res, matrix[0][a]); } } } cout << res; }
#include <bits/stdc++.h> #include <utility> using namespace std; const long long N = 200001; const long long K = 201; long long n, k; long long arr[N]; long long matrix[N][K]; long long checkmin(long long i) { long long res = matrix[i][0]; for (long long j = 0; j < k; j++) { if (matrix[i][j] >= 0) { res = min(res, matrix[i][j]); } } return res; } int main() { cin >> n >> k; for (long long i = 0; i < n; i++) { cin >> arr[i]; } for (long long r = 0; r < k; r++) { for (long long f = 0; f < n; f++) { if (r > n - f - 2) matrix[f][r] = -1; } matrix[n - 1][r] = 0; if (n - r - 2 > 0) { matrix[n - r - 2][r] = abs(arr[n - r - 2] - arr[n - 1]); } } for (long long j = n - 2; j >= 0; j--) { for (long long h = 0; h < k; h++) { if (h < n - j - 1) { matrix[j][h] = abs(arr[j] - arr[j + h + 1]) + checkmin(j + h + 1); } } } long long res = matrix[0][0]; long long maximo = -1; for (long long a = 0; a < k; a++) { res = min(res, matrix[0][a]); maximo = max(maximo, matrix[0][a]); } if (maximo == -1) res = 0; else if (res == -1) { res = maximo; for (long long a = 0; a < k; a++) { if (matrix[0][a] != -1) { res = min(res, matrix[0][a]); } } } cout << res; }
replace
33
34
33
36
-11
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; void frog2(vector<int> &cost, int m) { int n = cost.size(); if (n == 0) { cout << 0 << endl; } int dp[n]; dp[0] = 0; dp[1] = abs(cost[1] - cost[0]) + dp[0]; int j; for (int i = 2; i < n; i++) { int mn = 1e9 + 5; for (int k = 0; k < m; k++) { mn = min(mn, abs(cost[i] - cost[i - k - 1]) + dp[i - k - 1]); } dp[i] = mn; } cout << dp[n - 1] << endl; } int main() { int n; int k; cin >> n; cin >> k; vector<int> cost; int val; for (int i = 0; i < n; i++) { cin >> val; cost.push_back(val); } frog2(cost, k); return 0; }
#include <bits/stdc++.h> using namespace std; void frog2(vector<int> &cost, int m) { int n = cost.size(); if (n == 0) { cout << 0 << endl; } int dp[n]; dp[0] = 0; dp[1] = abs(cost[1] - cost[0]) + dp[0]; int j; for (int i = 2; i < n; i++) { int mn = 1e9 + 5; for (int k = 0; k < m; k++) { if (i - k - 1 >= 0) mn = min(mn, abs(cost[i] - cost[i - k - 1]) + dp[i - k - 1]); } dp[i] = mn; } cout << dp[n - 1] << endl; } int main() { int n; int k; cin >> n; cin >> k; vector<int> cost; int val; for (int i = 0; i < n; i++) { cin >> val; cost.push_back(val); } frog2(cost, k); return 0; }
replace
15
16
15
17
0
p03161
C++
Runtime Error
#include <algorithm> #include <cmath> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, a, b) for (long long i = a; i < b; i++) using namespace std; long long n; vector<long long> dp; long long bf(long long p, long long pp, long long sum, vector<long long> &fec) { if (dp[p] != -1) { return dp[p]; } if (pp == n - 1) { // cout << sum << endl; return sum; } else if (pp >= n) { return 1000000000; } // cout << abs(fec[pp]-fec[p]) << " " << p << " " << pp << endl; return dp[p] = min(bf(p + 1, p, sum + abs(fec[pp] - fec[p]), fec), bf(p + 2, p, sum + abs(fec[pp] - fec[p]), fec)); } int main() { int k; cin >> n >> k; dp.assign(n, 10000000000000); vector<long long> fector(n + k, 0); rep(i, 0, n) { cin >> fector[i]; } dp[0] = 0; rep(i, 0, n - 1) { rep(j, 1, k + 1) { dp[i + j] = min(dp[i + j], dp[i] + abs(fector[i] - fector[i + j])); } } cout << dp[n - 1] << "\n"; // cout << bf(0, 0, 0, fector) << endl; }
#include <algorithm> #include <cmath> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, a, b) for (long long i = a; i < b; i++) using namespace std; long long n; vector<long long> dp; long long bf(long long p, long long pp, long long sum, vector<long long> &fec) { if (dp[p] != -1) { return dp[p]; } if (pp == n - 1) { // cout << sum << endl; return sum; } else if (pp >= n) { return 1000000000; } // cout << abs(fec[pp]-fec[p]) << " " << p << " " << pp << endl; return dp[p] = min(bf(p + 1, p, sum + abs(fec[pp] - fec[p]), fec), bf(p + 2, p, sum + abs(fec[pp] - fec[p]), fec)); } int main() { int k; cin >> n >> k; dp.assign(n + k + k, 10000000000000); vector<long long> fector(n + k, 0); rep(i, 0, n) { cin >> fector[i]; } dp[0] = 0; rep(i, 0, n - 1) { rep(j, 1, k + 1) { dp[i + j] = min(dp[i + j], dp[i] + abs(fector[i] - fector[i + j])); } } cout << dp[n - 1] << "\n"; // cout << bf(0, 0, 0, fector) << endl; }
replace
33
34
33
34
-6
munmap_chunk(): invalid pointer
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define oo 0x3f3f3f3f using namespace std; const int N = 100005; const int K = 105; int dp[K]; int vet[N]; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &vet[i]); } dp[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { dp[i % (k + 1)] = oo; for (int j = i + 1; j < min(n, i + k + 1); j++) { dp[i % (k + 1)] = min(dp[i % (k + 1)], dp[j % (k + 1)] + abs(vet[i] - vet[j])); } } printf("%d\n", dp[0]); }
#include <bits/stdc++.h> #define oo 0x3f3f3f3f using namespace std; const int N = 100005; const int K = 105; int dp[K]; int vet[N]; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &vet[i]); } dp[(n - 1) % (k + 1)] = 0; for (int i = n - 2; i >= 0; i--) { dp[i % (k + 1)] = oo; for (int j = i + 1; j < min(n, i + k + 1); j++) { dp[i % (k + 1)] = min(dp[i % (k + 1)], dp[j % (k + 1)] + abs(vet[i] - vet[j])); } } printf("%d\n", dp[0]); }
replace
16
17
16
17
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define F(i, a, n) for (int i = a; i < n; i++) #define pb push_back typedef vector<int> vi; typedef pair<int, int> pi; typedef long long ll; #define mod 1000000007 int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int w = 1; // cin>>w; while (w--) { int n, k; cin >> n >> k; vi v(n); F(i, 0, n) { cin >> v[i]; } vi dp(n); dp[0] = 0; dp[1] = abs(v[0] - v[1]); F(i, 2, n) { dp[i] = INT_MAX; F(j, 1, k + 1) dp[i] = min(dp[i - j] + abs(v[i] - v[i - j]), dp[i]); } cout << dp[n - 1]; } }
#include <bits/stdc++.h> using namespace std; #define F(i, a, n) for (int i = a; i < n; i++) #define pb push_back typedef vector<int> vi; typedef pair<int, int> pi; typedef long long ll; #define mod 1000000007 int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int w = 1; // cin>>w; while (w--) { int n, k; cin >> n >> k; vi v(n); F(i, 0, n) { cin >> v[i]; } vi dp(n); dp[0] = 0; dp[1] = abs(v[0] - v[1]); F(i, 2, n) { dp[i] = INT_MAX; F(j, 1, k + 1) if (i - j >= 0) dp[i] = min(dp[i - j] + abs(v[i] - v[i - j]), dp[i]); } cout << dp[n - 1]; } }
replace
26
27
26
28
0
p03161
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> #define pii pair<int, int> #define makep make_pair #define pb push_back #define fi first #define se second using namespace std; typedef long long ll; typedef unsigned long long ull; int main0(); int main() { #ifndef ONLINE_JUDGE freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin); #endif ios::sync_with_stdio(false); main0(); #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } const int INF = 0x3f3f3f3f; const int N = 1e6; int a[N]; int dp[N]; int main0() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } memset(dp, INF, sizeof dp); dp[1] = 0; for (int i = 2; i <= n; i++) for (int j = 1; j <= k; j++) if (i > j) dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j])); cout << dp[n] << endl; }
#define ONLINE_JUDGE #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> #define pii pair<int, int> #define makep make_pair #define pb push_back #define fi first #define se second using namespace std; typedef long long ll; typedef unsigned long long ull; int main0(); int main() { #ifndef ONLINE_JUDGE freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin); #endif ios::sync_with_stdio(false); main0(); #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } const int INF = 0x3f3f3f3f; const int N = 1e6; int a[N]; int dp[N]; int main0() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } memset(dp, INF, sizeof dp); dp[1] = 0; for (int i = 2; i <= n; i++) for (int j = 1; j <= k; j++) if (i > j) dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j])); cout << dp[n] << endl; }
insert
0
0
0
1
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double dbl; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef vector<long long> vl; typedef vector<pair<int, int>> vii; #define mp make_pair #define pb push_back #define invec(a, n) \ for (ll i = 0; i < n; i++) { \ cin >> a[i]; \ } #define rep(n) for (ll i = 0; i < n; i++) #define fr(i, j, k) for (ll i = j; i < k; i++) #define FR(i, j, k) for (ll i = j; i >= 0; i--) #define MOD 1000000007 #define all(v) v.begin(), v.end() #define INF 1000000 #define xx first #define yy second #define pl cout << endl; template <typename F, typename S> ostream &operator<<(ostream &os, const pair<F, S> &p) { return os << "(" << p.first << "," << p.second << ")"; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; typename vector<T>::const_iterator it; for (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) { os << "["; typename set<T>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename F, typename S> ostream &operator<<(ostream &os, const map<F, S> &v) { os << "["; typename map<F, S>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << it->first << " = " << it->second; } return os << "]"; } #define deb(x) cout << #x << " = " << x << endl; #define er(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } 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...); } void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); } int main() { ll n, k; cin >> n >> k; vl v(n); invec(v, n); vl dp(n); fill(all(dp), LLONG_MAX); dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (ll i = 2; i < n; i++) { for (ll j = 1; j <= k; j++) { if (i - j < 0) { break; } dp[i] = min(dp[i], abs(v[i] - v[i - k]) + dp[i - k]); } // dp[i]=min(abs(v[i]-v[i-2])+dp[i-2],abs(v[i]-v[i-1])+dp[i-1]); } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double dbl; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef vector<long long> vl; typedef vector<pair<int, int>> vii; #define mp make_pair #define pb push_back #define invec(a, n) \ for (ll i = 0; i < n; i++) { \ cin >> a[i]; \ } #define rep(n) for (ll i = 0; i < n; i++) #define fr(i, j, k) for (ll i = j; i < k; i++) #define FR(i, j, k) for (ll i = j; i >= 0; i--) #define MOD 1000000007 #define all(v) v.begin(), v.end() #define INF 1000000 #define xx first #define yy second #define pl cout << endl; template <typename F, typename S> ostream &operator<<(ostream &os, const pair<F, S> &p) { return os << "(" << p.first << "," << p.second << ")"; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; typename vector<T>::const_iterator it; for (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) { os << "["; typename set<T>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename F, typename S> ostream &operator<<(ostream &os, const map<F, S> &v) { os << "["; typename map<F, S>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << it->first << " = " << it->second; } return os << "]"; } #define deb(x) cout << #x << " = " << x << endl; #define er(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } 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...); } void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); } int main() { ll n, k; cin >> n >> k; vl v(n); invec(v, n); vl dp(n); fill(all(dp), LLONG_MAX); dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (ll i = 2; i < n; i++) { for (ll j = 1; j <= k; j++) { if (i - j < 0) { break; } dp[i] = min(dp[i], abs(v[i] - v[i - j]) + dp[i - j]); } // dp[i]=min(abs(v[i]-v[i-2])+dp[i-2],abs(v[i]-v[i-1])+dp[i-1]); } cout << dp[n - 1] << endl; }
replace
97
98
97
98
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef string str; using P = pair<int, int>; const int inf = 1e9; const int minus_inf = -1e9; struct edge { int to; int cost; }; using graph = vector<vector<edge>>; int main() { int n, k; cin >> n >> k; vector<int> dp(n, inf); vector<int> h(n); for (int i = 0; i < n; i++) { cin >> h[i]; } // initialization dp[0] = 0; for (int i = 1; i < k; i++) { dp[i] = abs(h[i] - h[0]); } if (k < n) { for (int i = k; i < n; i++) { for (int j = 1; j <= k; j++) { dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef string str; using P = pair<int, int>; const int inf = 1e9; const int minus_inf = -1e9; struct edge { int to; int cost; }; using graph = vector<vector<edge>>; int main() { int n, k; cin >> n >> k; vector<int> dp(n, inf); vector<int> h(n); for (int i = 0; i < n; i++) { cin >> h[i]; } // initialization dp[0] = 0; if (k >= n) { for (int i = 1; i <= n; i++) { dp[i] = abs(h[i] - h[0]); } } else { for (int i = 1; i <= k; i++) { dp[i] = abs(h[i] - h[0]); } for (int i = k; i < n; i++) { for (int j = 1; j <= k; j++) { dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } } cout << dp[n - 1] << endl; return 0; }
replace
24
28
24
32
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main(void) { int N, K; cin >> N >> K; vector<long long> h(N); for (int i = 0; i < N; i++) { cin >> h[i]; } vector<long long> dp(N, 100000007); dp[0] = 0; for (int i = 1; i < N; i++) { for (int j = 1; j <= i; j++) { if (j <= K) { dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } } cout << dp[N - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int N, K; cin >> N >> K; vector<long long> h(N); for (int i = 0; i < N; i++) { cin >> h[i]; } vector<long long> dp(N, 100000007); dp[0] = 0; for (int i = 1; i < N; i++) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); for (int j = 2; j <= K; j++) { if (i - j >= 0) { dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } } cout << dp[N - 1] << endl; return 0; }
replace
13
15
13
16
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, k; ios::sync_with_stdio(0); cin.tie(NULL); cin >> N >> k; vector<int> H(N); vector<int> DP(N); for (int &x : H) cin >> x; DP[0] = 0; for (int i = 1; i < N; ++i) { DP[i] = 0x3f3f3f3f; for (int prev = i - 1; i - prev <= k; --prev) { DP[i] = min(DP[i], DP[prev] + abs(H[prev] - H[i])); } } cout << DP[N - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N, k; ios::sync_with_stdio(0); cin.tie(NULL); cin >> N >> k; vector<int> H(N); vector<int> DP(N); for (int &x : H) cin >> x; DP[0] = 0; for (int i = 1; i < N; ++i) { DP[i] = 0x3f3f3f3f; for (int prev = i - 1; i - prev <= k; --prev) { if (prev < 0) break; DP[i] = min(DP[i], DP[prev] + abs(H[prev] - H[i])); } } cout << DP[N - 1] << endl; return 0; }
insert
19
19
19
21
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> A(n); for (int i = 0; i < n; i++) cin >> A[i]; vector<int> dp(n, INT_MAX); dp[n - 1] = 0; for (int i = n - 2; i >= n - k; i--) { for (int j = i + 1; j < n; j++) { dp[i] = min(dp[i], abs(A[i] - A[j]) + dp[j]); } } for (int i = n - k - 1; i >= 0; i--) { for (int j = i + 1; j - i <= k; j++) { dp[i] = min(dp[i], abs(A[i] - A[j]) + dp[j]); } } cout << dp[0] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; k = min(k, n); vector<int> A(n); for (int i = 0; i < n; i++) cin >> A[i]; vector<int> dp(n, INT_MAX); dp[n - 1] = 0; for (int i = n - 2; i >= n - k; i--) { for (int j = i + 1; j < n; j++) { dp[i] = min(dp[i], abs(A[i] - A[j]) + dp[j]); } } for (int i = n - k - 1; i >= 0; i--) { for (int j = i + 1; j - i <= k; j++) { dp[i] = min(dp[i], abs(A[i] - A[j]) + dp[j]); } } cout << dp[0] << endl; }
insert
6
6
6
7
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k, i, j; cin >> n >> k; int h[n]; for (i = 0; i < n; i++) { cin >> h[i]; } int dp[n]; dp[0] = 0; for (i = 1; i < n; i++) { dp[i] = INT_MAX; } for (i = 0; i < n; i++) { for (j = i + 1; j <= i + k; j++) { dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j])); } } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k, i, j; cin >> n >> k; int h[n]; for (i = 0; i < n; i++) { cin >> h[i]; } int dp[n]; dp[0] = 0; for (i = 1; i < n; i++) { dp[i] = INT_MAX; } for (i = 0; i < n; i++) { for (j = i + 1; j <= i + k; j++) { if (j < n) dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j])); } } cout << dp[n - 1] << endl; }
replace
16
17
16
18
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define MAX_N 100000 #define MAX_M 100000 const int MAX = 10000000; const int MOD = 1000000007; #define vec vector<int> #define vecll vector<ll> #define vecllvec vector<vector<ll>> #define vecb vector<bool> #define vecbvecb vector<vector<bool>> #define vecst vector<string> #define vecch vector<char> #define vecf vector<float> #define vecvec vector<vector<int>> #define vecHvec vector<vector<char>> #define all(x) (x).begin(), (x).end() const int INF = 1e9; #define PI 3.141592653589793 // cout << setprecision(15) << std::fixed; int main() { ll n, k; cin >> n >> k; vecll h(n + 1, 0); for (ll i = 0; i < n; i++) { cin >> h[i]; } vecll dp(n + 1); dp[0] = 0; for (ll i = 1; i < n; i++) { dp[i] = INF; for (ll j = 1; j <= k; j++) { dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define MAX_N 100000 #define MAX_M 100000 const int MAX = 10000000; const int MOD = 1000000007; #define vec vector<int> #define vecll vector<ll> #define vecllvec vector<vector<ll>> #define vecb vector<bool> #define vecbvecb vector<vector<bool>> #define vecst vector<string> #define vecch vector<char> #define vecf vector<float> #define vecvec vector<vector<int>> #define vecHvec vector<vector<char>> #define all(x) (x).begin(), (x).end() const int INF = 1e9; #define PI 3.141592653589793 // cout << setprecision(15) << std::fixed; int main() { ll n, k; cin >> n >> k; vecll h(n + 1, 0); for (ll i = 0; i < n; i++) { cin >> h[i]; } vecll dp(n + 1); dp[0] = 0; for (ll i = 1; i < n; i++) { dp[i] = INF; for (ll j = 1; j <= k; j++) { if (!(i - j < 0)) dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[n - 1] << endl; }
replace
34
35
34
36
0
p03161
C++
Runtime Error
//_Hala Madrid_// #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; typedef long double ldb; typedef vector<int> vi; typedef pair<int, int> pii; #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define FOR(i, start, end) for (ll i = start; i <= end; i++) #define FORD(i, start, end) for (ll i = start; i >= end; i--) #define br cout << "\n" #define test(t) \ int t; \ cin >> t; \ while (t--) #define rz return 0 #define all(s) s.begin(), s.end() #define sz size() #define pb push_back #define F first #define S second const int mod = 1e9 + 7; void solve() { bool flag = 0, flag1 = 0; int m, n, k, l, r, sum = 0, ans = 0, pos, pos1, c1 = 0, c2 = 0, c3 = 0, c4 = 0, x, y, z, cnt = 0, mx = 0, mn = INT_MAX; string s, t, s1, s2; string str = ""; vi v; cin >> n >> m; int h[n]; vector<ll> dp(n + 5, INT_MAX); FOR(i, 0, n - 1) cin >> h[i]; dp[0] = 0; FOR(i, 0, n - 1) { FOR(k, 1, m) { dp[i + k] = min(dp[i + k], dp[i] + abs(h[i + k] - h[i])); } } cout << dp[n - 1]; } int main() { fio bool notc = 0; int t = 1; if (notc) { cin >> t; } while (t--) { #ifdef _DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif solve(); } rz; }
//_Hala Madrid_// #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; typedef long double ldb; typedef vector<int> vi; typedef pair<int, int> pii; #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define FOR(i, start, end) for (ll i = start; i <= end; i++) #define FORD(i, start, end) for (ll i = start; i >= end; i--) #define br cout << "\n" #define test(t) \ int t; \ cin >> t; \ while (t--) #define rz return 0 #define all(s) s.begin(), s.end() #define sz size() #define pb push_back #define F first #define S second const int mod = 1e9 + 7; void solve() { bool flag = 0, flag1 = 0; int m, n, k, l, r, sum = 0, ans = 0, pos, pos1, c1 = 0, c2 = 0, c3 = 0, c4 = 0, x, y, z, cnt = 0, mx = 0, mn = INT_MAX; string s, t, s1, s2; string str = ""; vi v; cin >> n >> m; int h[n]; vector<ll> dp(n + m + 5, INT_MAX); FOR(i, 0, n - 1) cin >> h[i]; dp[0] = 0; FOR(i, 0, n - 1) { FOR(k, 1, m) { dp[i + k] = min(dp[i + k], dp[i] + abs(h[i + k] - h[i])); } } cout << dp[n - 1]; } int main() { fio bool notc = 0; int t = 1; if (notc) { cin >> t; } while (t--) { #ifdef _DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif solve(); } rz; }
replace
40
41
40
41
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; const ll INF = 10e9; int solve(int n, int k, vector<int> h) { vector<int> dp(n, INF); dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= i + k; j++) { if (j < n) { dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j])); } } } return dp[n - 1]; } int main() { int n, k; cin >> n >> k; vector<int> h(n); rep(i, n) cin >> h[i]; cout << solve(n, k, h); }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; const ll INF = 10e9; int solve(int n, int k, vector<int> h) { vector<int> dp(n, INF); dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j <= i + k; j++) { if (j < n) { dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j])); } } } return dp[n - 1]; } int main() { int n, k; cin >> n >> k; vector<int> h(n); rep(i, n) cin >> h[i]; cout << solve(n, k, h); }
replace
10
11
10
11
TLE
p03161
Python
Runtime Error
N, K = map(int, input().split()) (*h,) = map(int, input().split()) a = [abs(h[0] - h[k]) for k in range(K)] for n in range(N - K): a = a[1:] + [min([a[k] + abs(h[n + k] - h[n + K]) for k in range(K)])] print(a[K - 1])
N, K = map(int, input().split()) K = min(N, K) (*h,) = map(int, input().split()) a = [abs(h[0] - h[k]) for k in range(K)] for n in range(N - K): a = a[1:] + [min([a[k] + abs(h[n + k] - h[n + K]) for k in range(K)])] print(a[K - 1])
insert
1
1
1
2
0
p03161
Python
Time Limit Exceeded
# 配るDP # Hは1-index import sys input = sys.stdin.readline n, k = map(int, input().split()) H = [0] + list(map(int, input().split())) dp = [10**10] * (n + 1) dp[1] = 0 for i in range(1, n): for j in range(1, k + 1): if i + j <= n: dp[i + j] = min(dp[i + j], dp[i] + abs(H[i + j] - H[i])) print(dp[n])
# 配るDP # Hは1-index # PyPyじゃないと厳しい。 # 二重for loop import sys input = sys.stdin.readline n, k = map(int, input().split()) H = [0] + list(map(int, input().split())) dp = [10**10] * (n + 1) dp[1] = 0 for i in range(1, n): for j in range(1, k + 1): if i + j <= n: dp[i + j] = min(dp[i + j], dp[i] + abs(H[i + j] - H[i])) print(dp[n])
insert
2
2
2
4
TLE
p03161
Python
Time Limit Exceeded
import math n, k = map(int, input().split()) h = list(map(int, input().split())) dp = [math.inf] * n dp[0] = 0 for i in range(1, n): for j in range(max(0, i - k), i): dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j])) print(dp[n - 1])
import math n, k = map(int, input().split()) h = list(map(int, input().split())) dp = [math.inf] * n dp[0] = 0 for i in range(1, n): dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)) print(dp[n - 1])
replace
8
10
8
9
TLE
p03161
Python
Time Limit Exceeded
def compute(n, k, h): inf = 1e9 if n == 1: return 0 dp = [inf] * n dp[0] = 0 for i in range(1, n): cnt = 1 while cnt <= k and i - cnt > -1: dp[i] = min(dp[i], dp[i - cnt] + abs(h[i] - h[i - cnt])) return dp[-1] if __name__ == "__main__": N, K = map(int, input().split()) h = list(map(int, input().split())) print(compute(N, K, h))
n, k = [int(_) for _ in input().split()] h = [int(_) for _ in input().split()] inf = 1e9 dp = [inf] * n dp[0] = 0 for i in range(1, n): cnt = 1 while cnt <= k and i - cnt > -1: dp[i] = min(dp[i], dp[i - cnt] + abs(h[i] - h[i - cnt])) cnt += 1 print(dp[-1])
replace
0
18
0
11
TLE
p03161
Python
Time Limit Exceeded
N, K = map(int, input().split()) H = list(map(int, input().split())) H = [H[0]] * K + H # dp[i] := 足場iに来るのにかかる最小のコスト INF = 10**18 dp = [INF] * (K + N) dp[0] = 0 for i in range(1, N + K): for j in range(1, K + 1): v = dp[i - j] + abs(H[i] - H[i - j]) dp[i] = min(dp[i], v) # print('dp', dp) ans = dp[-1] print(ans)
N, K = map(int, input().split()) H = list(map(int, input().split())) H = [H[0]] * K + H # dp[i] := 足場iに来るのにかかる最小のコスト INF = 10**18 dp = [INF] * (K + N) dp[0] = 0 for i in range(K, N + K): h = H[i] dp[i] = min(x + abs(y - h) for x, y in zip(dp[i - K : i], H[i - K : i])) # for j in range(1, K + 1): # v = dp[i - j] + abs(H[i] - H[i - j]) # dp[i] = min(dp[i], v) # print('dp', dp) ans = dp[-1] print(ans)
replace
8
12
8
14
TLE
p03161
Python
Time Limit Exceeded
#!/usr/bin/env python3 N, K = list(map(int, input().split())) h_list = list(map(int, input().split())) dp_list = [float("inf")] * N dp_list[0] = 0 for i in range(1, N): # j番目の足場からi番目の足場に飛ぶ for j in range(max(i - K, 0), i + 1): # j番目の足場までの最適コスト cum_cost = dp_list[j] # j番目の足場から飛んだ場合のコスト cost = dp_list[j] + abs(h_list[j] - h_list[i]) # 最適コストで更新 dp_list[i] = min(dp_list[i], cost) ans = dp_list[-1] print(ans)
N, K = list(map(int, input().split())) h_list = list(map(int, input().split())) dp_list = [float("inf")] * N dp_list[0] = 0 for i in range(1, N): # j番目の足場からi番目の足場に飛ぶ for j in range(max(i - K, 0), i + 1): # j番目の足場までの最適コスト cum_cost = dp_list[j] # j番目の足場から飛んだ場合のコスト cost = dp_list[j] + abs(h_list[j] - h_list[i]) # 最適コストで更新 dp_list[i] = min(dp_list[i], cost) ans = dp_list[-1] print(ans)
delete
0
2
0
0
TLE