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
Python
Time Limit Exceeded
n, k, *L = map(int, open(0).read().split()) # dp = [float("inf")] * n dp = [0] * n for i in range(1, n): j = max(0, i - k) dp[i] = min([DP + abs(L[i] - COST) for DP, COST in zip(dp[j:i], L[j:i])]) print(dp[-1])
n, k, *L = map(int, open(0).read().split()) # dp = [float("inf")] * n dp = [0] * n for i in range(1, n): j = max(0, i - k) e = L[i] dp[i] = min([DP + abs(e - COST) for DP, COST in zip(dp[j:i], L[j:i])]) print(dp[-1])
replace
7
8
7
9
TLE
p03161
Python
Runtime Error
N, K = map(int, input().split()) h = list(map(int, input().split())) inf = 10**9 dp = [inf] * N dp[0] = 0 def chmin(num: int, sbn: int): dp[num + sbn] = min((dp[num + sbn], dp[num] + abs(h[i + sbn] - h[num]))) for i in range(N): for sbn in range(K): try: chmin(dp, i, sbn + 1) except IndexError: break print(dp[-1])
N, K = map(int, input().split()) h = list(map(int, input().split())) inf = 10**9 dp = [inf] * N dp[0] = 0 def chmin(num: int, sbn: int): dp[num + sbn] = min((dp[num + sbn], dp[num] + abs(h[i + sbn] - h[num]))) for i in range(N): for sbn in range(K): try: chmin(i, sbn + 1) except IndexError: break print(dp[-1])
replace
14
15
14
15
TypeError: chmin() takes 2 positional arguments but 3 were given
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03161/Python/s825976309.py", line 15, in <module> chmin(dp, i, sbn + 1) TypeError: chmin() takes 2 positional arguments but 3 were given
p03161
Python
Time Limit Exceeded
n, k = map(int, input().split()) h = tuple(map(int, input().split())) INF = 10**10 dp = [INF] * n dp[0] = 0 # dp[i] = (足場iにたどり着いた時点でのコストの最小値) for i in range(n): for j in range(i + 1, min(i + k + 1, n)): if dp[j] > dp[i] + abs(h[j] - h[i]): dp[j] = dp[i] + abs(h[j] - h[i]) print(dp[n - 1])
n, k = map(int, input().split()) h = tuple(map(int, input().split())) INF = 10**10 dp = [INF] * n dp[0] = 0 # dp[i] = (足場iにたどり着いた時点でのコストの最小値) for i in range(1, n): dp[i] = min(dp[j] + abs(h[j] - h[i]) for j in range(max(0, i - k), i)) print(dp[n - 1])
replace
8
12
8
10
TLE
p03161
Python
Runtime Error
N, K = map(int, input().split()) H = list(map(int, input().split())) dp = [1 << 62] * N dp[0] = 0 for i in range(1, N): for j in range(i, min(i + 1 + K, N)): cost = abs(H[i - 1] - H[i + j]) dp[i + j] = min(dp[i + j], dp[i - 1] + cost) print(dp[-1])
N, K = map(int, input().split()) H = list(map(int, input().split())) dp = [1 << 62] * N dp[0] = 0 for i in range(1, N): for j in range(i, min(i + K, N)): cost = abs(H[i - 1] - H[j]) dp[j] = min(dp[j], dp[i - 1] + cost) print(dp[-1])
replace
5
8
5
8
IndexError: list index out of range
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03161/Python/s787574815.py", line 7, in <module> cost = abs(H[i - 1] - H[i + j]) IndexError: list index out of range
p03161
Python
Time Limit Exceeded
n, k = map(int, input().split()) h = list(map(int, input().split())) a = [0] * n a[0] = 0 for i in range(1, n): a[i] = min([a[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)]) print(a[-1])
def main(): n, k = map(int, input().split()) h = list(map(int, input().split())) a = [0 for i in range(n)] for i in range(1, n): a[i] = min([a[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)]) print(a[-1]) if __name__ == "__main__": main()
replace
0
7
0
11
TLE
p03161
Python
Time Limit Exceeded
from math import inf n, k = map(int, input().split()) a = [int(i) for i in input().split()] dp = [inf for _ in range(n)] dp[0] = 0 for i in range(1, n): for j in range(1, k + 1): if i - j >= 0: dp[i] = min(dp[i], abs(a[i] - a[i - j]) + dp[i - j]) print(dp[-1])
from math import inf n, k = map(int, input().split()) a = [int(i) for i in input().split()] dp = [inf for _ in range(n)] dp[0] = 0 for i in range(1, n): for j in range(1, k + 1): if i - j >= 0: dp[i] = min(dp[i], abs(a[i] - a[i - j]) + dp[i - j]) else: break print(dp[-1])
insert
10
10
10
12
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define MOD ((int)(1e9) + 7) #define fast \ cin.tie(0); \ cout.tie(0); \ ios_base::sync_with_stdio(false) #define filename "lynext" #define freop \ freopen(filename ".cik", "w", stdout); \ freopen(filename ".gir", "r", stdin) const long long int N = ((long long int)1e5) + 5; const long long int M = ((long long int)1e3) + 5; const long long int llinf = (long long int)1e18; const int inf = INT_MAX; typedef long long int lli; lli dp[N]; lli n; lli k; lli h[N]; lli f(lli i) { if (dp[i] != -1) return dp[i]; if (i == n) return 0; if (i == n - 1) return abs(h[n] - h[i]); lli t = llinf; for (int j = i + 1; j <= i + k; j++) t = min(t, f(j) + abs(h[i] - h[j])); return dp[i] = t; } int main() { fast; // freop; cin >> n >> k; memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) { cin >> h[i]; } cout << f(1) << "\n"; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define MOD ((int)(1e9) + 7) #define fast \ cin.tie(0); \ cout.tie(0); \ ios_base::sync_with_stdio(false) #define filename "lynext" #define freop \ freopen(filename ".cik", "w", stdout); \ freopen(filename ".gir", "r", stdin) const long long int N = ((long long int)1e5) + 5; const long long int M = ((long long int)1e3) + 5; const long long int llinf = (long long int)1e18; const int inf = INT_MAX; typedef long long int lli; lli dp[N]; lli n; lli k; lli h[N]; lli f(lli i) { if (dp[i] != -1) return dp[i]; if (i == n) return 0; if (i == n - 1) return abs(h[n] - h[i]); lli t = llinf; for (int j = i + 1; j <= min(i + k, n); j++) t = min(t, f(j) + abs(h[i] - h[j])); return dp[i] = t; } int main() { fast; // freop; cin >> n >> k; memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) { cin >> h[i]; } cout << f(1) << "\n"; }
replace
34
35
34
35
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define ff first #define ss second #define ll long long #define ll_MAX LONG_LONG_MAX #define ll_MIN LONG_LONG_MIN #define pi pair<int, int> #define endl "\n" #define MAXN 100005 #define mod 1000000007 using namespace std; void solve() {} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); ll n, k; cin >> n >> k; ll h[n]; for (ll i = 0; i < n; i++) { /* code */ cin >> h[i]; } vector<ll> dp(n, 1e10); dp[0] = 0; // dp[1] = abs(h[1] - h[0]); for (ll i = 1; i < n; i++) { for (ll j = 1; j <= k; j++) { dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i]); } } cout << dp[n - 1] << '\n'; }
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define ff first #define ss second #define ll long long #define ll_MAX LONG_LONG_MAX #define ll_MIN LONG_LONG_MIN #define pi pair<int, int> #define endl "\n" #define MAXN 100005 #define mod 1000000007 using namespace std; void solve() {} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); ll n, k; cin >> n >> k; ll h[n]; for (ll i = 0; i < n; i++) { /* code */ cin >> h[i]; } vector<ll> dp(n, 1e10); dp[0] = 0; // dp[1] = abs(h[1] - h[0]); for (ll i = 1; i < n; i++) { for (ll j = 1; j <= k; j++) { if (i >= j) dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i]); } } cout << dp[n - 1] << '\n'; }
replace
33
34
33
35
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long add(long long a, long long b) { long long res = a + b; if (res >= mod) res -= mod; return res; } long long sub(long long a, long long b) { long long res = a - b + mod; if (res >= mod) res -= mod; return res; } long long mul(long long a, long long b) { return (((a % mod) * (b % mod)) % mod); } long long gcd(long long x, long long y) { if (y == 0) { return x; } if (x > y) { return gcd(y, x % y); } else { return gcd(x, y % x); } } void dfs(vector<vector<int>> &v, vector<int> &visited, int node, int &ans) { if (visited[node]) { return; } visited[node] = 1; ans++; for (auto i : v[node]) { dfs(v, visited, i, ans); } } /*bool good(vector<int> &v, int d, int mid){ vector<int> p(v.size()); p[0] = v[0] - mid; for(int i = 1;i < v.size();i++){ p[i] = p[i - 1] + (v[i] - mid); } vector<int> m(v.size()); m[0] = p[0]; int mi = m[0]; for(int i = 1;i < v.size();i++){ mi = min(mi, p[i]); m[i] = mi; } for(int r = 0;r < v.size();r++){ if(r - d > 0){ if(m[r - d - 1] <= p[r]){ L = r - d; R = r; return true; } } } return false; }*/ int main() { 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 + 1); dp[0] = 0; dp[1] = 0; dp[2] = abs(h[0] - h[1]); for (int i = 3; i <= n; i++) { dp[i] = 1e18; for (int j = 1; j <= k; j++) { dp[i] = min(dp[i], dp[i - j] + abs(h[i - 1] - h[i - j - 1])); } } cout << dp[n] << endl; } /* 5 2 -1 -2 -3 4 5 2 3 4 -3 -2 -1 4 5 2 3 5 + (-3) = 2 4 + -2 = 2 ans = 4 -3 -2 -1 4 5 3 2 5 + -3 = 2 4 + -1 = 3 ans = 5 */
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long add(long long a, long long b) { long long res = a + b; if (res >= mod) res -= mod; return res; } long long sub(long long a, long long b) { long long res = a - b + mod; if (res >= mod) res -= mod; return res; } long long mul(long long a, long long b) { return (((a % mod) * (b % mod)) % mod); } long long gcd(long long x, long long y) { if (y == 0) { return x; } if (x > y) { return gcd(y, x % y); } else { return gcd(x, y % x); } } void dfs(vector<vector<int>> &v, vector<int> &visited, int node, int &ans) { if (visited[node]) { return; } visited[node] = 1; ans++; for (auto i : v[node]) { dfs(v, visited, i, ans); } } /*bool good(vector<int> &v, int d, int mid){ vector<int> p(v.size()); p[0] = v[0] - mid; for(int i = 1;i < v.size();i++){ p[i] = p[i - 1] + (v[i] - mid); } vector<int> m(v.size()); m[0] = p[0]; int mi = m[0]; for(int i = 1;i < v.size();i++){ mi = min(mi, p[i]); m[i] = mi; } for(int r = 0;r < v.size();r++){ if(r - d > 0){ if(m[r - d - 1] <= p[r]){ L = r - d; R = r; return true; } } } return false; }*/ int main() { 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 + 1); dp[0] = 0; dp[1] = 0; dp[2] = abs(h[0] - h[1]); for (int i = 3; i <= n; i++) { dp[i] = 1e18; for (int j = 1; j <= k; j++) { if (i - j - 1 < 0) break; dp[i] = min(dp[i], dp[i - j] + abs(h[i - 1] - h[i - j - 1])); } } cout << dp[n] << endl; } /* 5 2 -1 -2 -3 4 5 2 3 4 -3 -2 -1 4 5 2 3 5 + (-3) = 2 4 + -2 = 2 ans = 4 -3 -2 -1 4 5 3 2 5 + -3 = 2 4 + -1 = 3 ans = 5 */
insert
85
85
85
87
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define repr(i, a, n) for (int i = n - 1; i >= a; i--) using namespace std; using ll = long long; using P = pair<int, int>; template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chmax(T &a, T b) { a = max(a, b); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> h(n); rep(i, 0, n) cin >> h[i]; vector<int> dp(n, 1e9); dp[0] = 0; rep(i, 1, n) { if (i == 1) dp[i] = abs(h[i] - h[i - 1]); else { int min_val = 1e9; rep(j, 1, k + 1) { chmin(min_val, dp[i - j] + abs(h[i] - h[i - j])); } chmin(dp[i], min_val); } } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define repr(i, a, n) for (int i = n - 1; i >= a; i--) using namespace std; using ll = long long; using P = pair<int, int>; template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chmax(T &a, T b) { a = max(a, b); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> h(n); rep(i, 0, n) cin >> h[i]; vector<int> dp(n, 1e9); dp[0] = 0; rep(i, 1, n) { if (i == 1) dp[i] = abs(h[i] - h[i - 1]); else { int min_val = 1e9; rep(j, 1, min(i + 1, k + 1)) { chmin(min_val, dp[i - j] + abs(h[i] - h[i - j])); } chmin(dp[i], min_val); } } cout << dp[n - 1] << endl; }
replace
26
27
26
29
0
p03161
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <functional> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <unordered_map> #include <utility> #include <vector> #define ll long long #define MOD 1000000007 using namespace std; void solve() { int n, k; cin >> n >> k; int arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; } int dp[n]; fill(dp, dp + n, INT_MAX); dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= i + k; ++j) { dp[j] = min(dp[j], dp[i] + abs(arr[j] - arr[i])); } } cout << dp[n - 1] << '\n'; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <functional> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <unordered_map> #include <utility> #include <vector> #define ll long long #define MOD 1000000007 using namespace std; void solve() { int n, k; cin >> n >> k; int arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; } int dp[n]; fill(dp, dp + n, INT_MAX); 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(arr[j] - arr[i])); } } } cout << dp[n - 1] << '\n'; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
replace
33
34
33
36
0
p03161
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> P; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define repone(i, n) for (ll i = 1; i <= (ll)(n); i++) #define each(i, mp) for (auto i : mp) #define FOR(i, m, n) for (ll i = m; i < n; i++) const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ll mod = 1000000007; const ll inf = ll(1e9); const ll half_inf = ll(1e5); const ll ll_inf = ll(1e9) * ll(1e9); 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; } int n, k; vector<ll> h; void solve() { ll dp[half_inf]; rep(i, half_inf) 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] - h[i + j])); } } cout << dp[n - 1] << endl; } int main() { cin >> n >> k; h.resize(n); rep(i, n) cin >> h[i]; solve(); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> P; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define repone(i, n) for (ll i = 1; i <= (ll)(n); i++) #define each(i, mp) for (auto i : mp) #define FOR(i, m, n) for (ll i = m; i < n; i++) const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ll mod = 1000000007; const ll inf = ll(1e9); const ll half_inf = ll(1e5); const ll ll_inf = ll(1e9) * ll(1e9); 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; } int n, k; vector<ll> h; void solve() { ll dp[half_inf + k]; rep(i, half_inf + k) 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] - h[i + j])); } } cout << dp[n - 1] << endl; } int main() { cin >> n >> k; h.resize(n); rep(i, n) cin >> h[i]; solve(); return 0; }
replace
46
48
46
48
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; #define int long long /* short */ #define ALL(v) begin(v), end(v) /* REPmacro */ #define FOR(i, s, n) for (int i = (s); i < (n); i++) #define RFOR(i, s, n) for (int i = (s); i >= (n); i--) #define rep(i, n) FOR(i, 0, n) #define repi(i, n) FOR(i, 1, n + 1) #define rrep(i, n) RFOR(i, n - 1, 0) #define rrepi(i, n) RFOR(i, n, 1) /* alias */ using ll = long long; using ull = unsigned long long; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; using pii = pair<int, int>; using vpii = vector<pii>; using vvpii = vector<pii>; using mii = map<int, int>; using vs = vector<string>; using vb = vector<bool>; template <typename T> using PQ = priority_queue<T>; template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>; /* iostream */ template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> istream &operator>>(istream &is, pair<T, T> &p) { int a, b; is >> a >> b; p = make_pair(a, b); return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> istream &operator>>(istream &is, complex<T> &c) { T x, y; is >> x >> y; c = complex<T>(x, y); return is; } /* input */ #define _overload(_1, _2, _3, _4, _5, _6, name, ...) name #define _g1(a) \ int a; \ cin >> a; #define _g2(a, b) \ int a, b; \ cin >> a >> b; #define _g3(a, b, c) \ int a, b, c; \ cin >> a >> b >> c; #define _g4(a, b, c, d) \ int a, b, c, d; \ cin >> a >> b >> c >> d; #define _g5(a, b, c, d, e) \ int a, b, c, d, e; \ cin >> a >> b >> c >> d >> e; #define _g6(a, b, c, d, e, f) \ int a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f; #define in(...) \ _overload(__VA_ARGS__, _g6, _g5, _g4, _g3, _g2, _g1)(__VA_ARGS__) /* debug */ #define _pp_overload(_1, _2, _3, _4, _5, _6, name, ...) name #define _p1(a) cerr << #a "=" << (a) << endl; #define _p2(a, b) cerr << #a "=" << (a) << "," #b "=" << (b) << endl; #define _p3(a, b, c) \ cerr << #a "=" << (a) << "," #b "=" << (b) << "," #c "=" << (c) << endl; #define _p4(a, b, c, d) \ cerr << #a "=" << (a) << "," #b "=" << (b) << "," #c "=" << (c) \ << "," #d "=" << (d) << endl; #define _p5(a, b, c, d, e) \ cerr << #a "=" << (a) << "," #b "=" << (b) << "," #c "=" << (c) \ << "," #d "=" << (d) << "," #e "=" << (e) << endl; #define _p6(a, b, c, d, e, f) \ cerr << #a "=" << (a) << "," #b "=" << (b) << "," #c "=" << (c) \ << "," #d "=" << (d) << "," #e "=" << (e) << "," #f "=" << (f) << endl; #define pp(...) \ _pp_overload(__VA_ARGS__, _p6, _p5, _p4, _p3, _p2, _p1)(__VA_ARGS__) /* const */ // const int INF = 1001001001; const ll INF = 1001001001001001001ll; const int MOD = 1e9 + 7; /* func */ #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()); #define TIME system("date +%M:%S.%N") inline bool inside(int y, int x, int H, int W) { return y >= 0 && x >= 0 && y < H && x < W; } inline bool odd(int x) { return x % 2 == 1; } inline bool even(int x) { return x % 2 == 0; } inline int sum(vi a) { return accumulate(ALL(a), 0); } inline void yn(bool ans) { cout << (ans ? "Yes" : "No") << endl; } inline void YN(bool ans) { cout << (ans ? "YES" : "NO") << endl; } template <typename T> inline bool chmin(T &a, const T &b) { if (a > b) a = b; return a > b; } template <typename T> inline bool chmax(T &a, const T &b) { if (a < b) a = b; return a < b; } template <class T> T gcd(const T &a, const T &b) { return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a; } template <class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; } template <class T> T div_ceil(const T &a, const T &b) { return (a + b - 1) / b; } template <class T> bool by_snd(const T &a, const T &b) { return a.snd < b.snd; } inline void print_and_exit(int x) { cout << x << endl; exit(0); } const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; signed main() { in(n, k); vi h(n); cin >> h; vi dp(n, INF); dp[0] = 0; rep(i, n) repi(j, k) { if (i + j >= n) continue; chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); pp(i, j, dp); } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; #define int long long /* short */ #define ALL(v) begin(v), end(v) /* REPmacro */ #define FOR(i, s, n) for (int i = (s); i < (n); i++) #define RFOR(i, s, n) for (int i = (s); i >= (n); i--) #define rep(i, n) FOR(i, 0, n) #define repi(i, n) FOR(i, 1, n + 1) #define rrep(i, n) RFOR(i, n - 1, 0) #define rrepi(i, n) RFOR(i, n, 1) /* alias */ using ll = long long; using ull = unsigned long long; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; using pii = pair<int, int>; using vpii = vector<pii>; using vvpii = vector<pii>; using mii = map<int, int>; using vs = vector<string>; using vb = vector<bool>; template <typename T> using PQ = priority_queue<T>; template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>; /* iostream */ template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> istream &operator>>(istream &is, pair<T, T> &p) { int a, b; is >> a >> b; p = make_pair(a, b); return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> istream &operator>>(istream &is, complex<T> &c) { T x, y; is >> x >> y; c = complex<T>(x, y); return is; } /* input */ #define _overload(_1, _2, _3, _4, _5, _6, name, ...) name #define _g1(a) \ int a; \ cin >> a; #define _g2(a, b) \ int a, b; \ cin >> a >> b; #define _g3(a, b, c) \ int a, b, c; \ cin >> a >> b >> c; #define _g4(a, b, c, d) \ int a, b, c, d; \ cin >> a >> b >> c >> d; #define _g5(a, b, c, d, e) \ int a, b, c, d, e; \ cin >> a >> b >> c >> d >> e; #define _g6(a, b, c, d, e, f) \ int a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f; #define in(...) \ _overload(__VA_ARGS__, _g6, _g5, _g4, _g3, _g2, _g1)(__VA_ARGS__) /* debug */ #define _pp_overload(_1, _2, _3, _4, _5, _6, name, ...) name #define _p1(a) cerr << #a "=" << (a) << endl; #define _p2(a, b) cerr << #a "=" << (a) << "," #b "=" << (b) << endl; #define _p3(a, b, c) \ cerr << #a "=" << (a) << "," #b "=" << (b) << "," #c "=" << (c) << endl; #define _p4(a, b, c, d) \ cerr << #a "=" << (a) << "," #b "=" << (b) << "," #c "=" << (c) \ << "," #d "=" << (d) << endl; #define _p5(a, b, c, d, e) \ cerr << #a "=" << (a) << "," #b "=" << (b) << "," #c "=" << (c) \ << "," #d "=" << (d) << "," #e "=" << (e) << endl; #define _p6(a, b, c, d, e, f) \ cerr << #a "=" << (a) << "," #b "=" << (b) << "," #c "=" << (c) \ << "," #d "=" << (d) << "," #e "=" << (e) << "," #f "=" << (f) << endl; #define pp(...) \ _pp_overload(__VA_ARGS__, _p6, _p5, _p4, _p3, _p2, _p1)(__VA_ARGS__) /* const */ // const int INF = 1001001001; const ll INF = 1001001001001001001ll; const int MOD = 1e9 + 7; /* func */ #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()); #define TIME system("date +%M:%S.%N") inline bool inside(int y, int x, int H, int W) { return y >= 0 && x >= 0 && y < H && x < W; } inline bool odd(int x) { return x % 2 == 1; } inline bool even(int x) { return x % 2 == 0; } inline int sum(vi a) { return accumulate(ALL(a), 0); } inline void yn(bool ans) { cout << (ans ? "Yes" : "No") << endl; } inline void YN(bool ans) { cout << (ans ? "YES" : "NO") << endl; } template <typename T> inline bool chmin(T &a, const T &b) { if (a > b) a = b; return a > b; } template <typename T> inline bool chmax(T &a, const T &b) { if (a < b) a = b; return a < b; } template <class T> T gcd(const T &a, const T &b) { return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a; } template <class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; } template <class T> T div_ceil(const T &a, const T &b) { return (a + b - 1) / b; } template <class T> bool by_snd(const T &a, const T &b) { return a.snd < b.snd; } inline void print_and_exit(int x) { cout << x << endl; exit(0); } const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; signed main() { in(n, k); vi h(n); cin >> h; vi dp(n, INF); dp[0] = 0; rep(i, n) repi(j, k) { if (i + j >= n) continue; chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } cout << dp[n - 1] << endl; }
delete
193
194
193
193
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, K; int h[10102]; int64_t dp[10102]; cin >> N >> K; for (int i = 0; i < N; ++i) { cin >> h[i]; dp[i] = 100000 * 10001; } dp[0] = 0; for (int i = 0; i < N - 1; ++i) { for (int j = 1; j < K + 1; ++j) { dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i])); } } cout << dp[N - 1] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, K; int h[100102]; int64_t dp[100102]; cin >> N >> K; for (int i = 0; i < N; ++i) { cin >> h[i]; dp[i] = 100000 * 10001; } dp[0] = 0; for (int i = 0; i < N - 1; ++i) { for (int j = 1; j < K + 1; ++j) { dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i])); } } cout << dp[N - 1] << endl; }
replace
5
7
5
7
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(n, i) for (ll i = 0; i < n; ++i) const ll INF = 1e18; void solve(void) { ll n, k; cin >> n >> k; vector<ll> h(n, 0); rep(n, i) cin >> h[i]; vector<ll> dp(n + 1, INF); dp[0] = 0; for (ll i = 1; i < n; ++i) { 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; } int main(void) { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(n, i) for (ll i = 0; i < n; ++i) const ll INF = 1e18; void solve(void) { ll n, k; cin >> n >> k; vector<ll> h(n, 0); rep(n, i) cin >> h[i]; vector<ll> dp(n + 1, INF); dp[0] = 0; for (ll i = 1; i < n; ++i) { for (ll j = 1; j <= k; ++j) { if (i >= j) dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[n - 1] << endl; } int main(void) { solve(); return 0; }
replace
16
17
16
18
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const ll IMF = 1LL << 60; int main() { int N, K; cin >> N >> K; vector<int> h(N); for (int i = 0; i < N; i++) cin >> h[i]; ll dp[N]; for (int i = 0; i < N; i++) { dp[i] = IMF; } dp[0] = 0; for (int i = 0; i < N; i++) { for (int k = 1; k <= K; k++) { if (i + k >= N) chmin(dp[i + k], dp[i] + abs(h[i + k] - h[i])); } } cout << dp[N - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const ll IMF = 1LL << 60; int main() { int N, K; cin >> N >> K; vector<int> h(N); for (int i = 0; i < N; i++) cin >> h[i]; ll dp[N]; for (int i = 0; i < N; i++) { dp[i] = IMF; } dp[0] = 0; for (int i = 0; i < N; i++) { for (int k = 1; k <= K; k++) { if (i + k < N) chmin(dp[i + k], dp[i] + abs(h[i + k] - h[i])); } } cout << dp[N - 1]; return 0; }
replace
34
35
34
35
0
p03161
C++
Runtime Error
#include <cmath> #include <iostream> using namespace std; int main() { int n, k; cin >> n >> k; int *h = new int[n]; for (int i = 0; i < n; i++) cin >> h[i]; int *dp = new int[n]; // dp[i]はi番目の足場までの最小コスト dp[0] = 0; for (int i = 0; i < n - 1; i++) { dp[i + 1] = dp[i] + abs(h[i] - h[i + 1]); for (int j = 2; j <= min(k, i + i); j++) { dp[i + 1] = min(dp[i + 1], dp[i + 1 - j] + abs(h[i + 1] - h[i + 1 - j])); } } cout << dp[n - 1] << endl; return 0; }
#include <cmath> #include <iostream> using namespace std; int main() { int n, k; cin >> n >> k; int *h = new int[n]; for (int i = 0; i < n; i++) cin >> h[i]; int *dp = new int[n]; // dp[i]はi番目の足場までの最小コスト dp[0] = 0; for (int i = 0; i < n - 1; i++) { dp[i + 1] = dp[i] + abs(h[i] - h[i + 1]); for (int j = 2; j <= min(k, i + 1); j++) { dp[i + 1] = min(dp[i + 1], dp[i + 1 - j] + abs(h[i + 1] - h[i + 1 - j])); } } cout << dp[n - 1] << endl; return 0; }
replace
17
18
17
18
0
p03161
C++
Runtime Error
/*input 10 4 40 10 20 70 80 10 20 70 80 60 */ // author - Madhav Thakker #include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") #pragma GCC optimize("O2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; #define ll long long #define ld long double #define int ll #define endl '\n' typedef pair<int, int> pii; typedef pair<ld, ld> pdd; #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define FileIn(file) freopen(file, "r", stdin) #define FileOut(file) freopen(file, "w", stdout) #define all(c) c.begin(), c.end() #define tr(container, it) \ for (__typeof__(container.begin()) it = container.begin(); \ it != container.end(); it++) #define present(container, element) \ (container.find(element) != container.end()) // map, set #define cpresent(container, element) \ (find(aint(container), element) != container.end()) // vector #define what_is(x) cout << #x << " is " << x << endl; #define all(c) c.begin(), c.end() // #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; const int MOD = 1e9 + 7; int fast_pow(int a, int b) { int res = a, ret = 1; while (b > 0) { if (b % 2) ret = (ret * res) % MOD; res = (res * res) % MOD; b /= 2; } return ret; } // -- // -- // -- // std::cout << std::fixed; // std::cout << std::setprecision(26) << f << '\n'; // str = to_string(n) // stoi , stoll, stol // sort(arr, arr+n, greater<int>()); // fill(prefix.begin(), prefix.end(), 0); // // //// const int maxN = 1e5 + 7; int arr[maxN]; int dp[maxN]; //// signed main() { fastio; time_t time_t1, time_t2; time_t1 = clock(); //////////////////////////////// int t = 1; // cin >> t ; while (t--) { int n; cin >> n; int k; cin >> k; for (int i = 0; i < n; ++i) { cin >> arr[i]; } for (int i = 0; i <= n; ++i) { dp[i] = 1e16; } dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) { dp[i + j] = min(dp[i + j], dp[i] + abs(arr[i + j] - arr[i])); } } cout << dp[n - 1] << endl; } /////////////////////////////// // time_t2 = clock(); // cerr << "time taken :" << time_t2 - time_t1 << endl; return 0; }
/*input 10 4 40 10 20 70 80 10 20 70 80 60 */ // author - Madhav Thakker #include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") #pragma GCC optimize("O2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; #define ll long long #define ld long double #define int ll #define endl '\n' typedef pair<int, int> pii; typedef pair<ld, ld> pdd; #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define FileIn(file) freopen(file, "r", stdin) #define FileOut(file) freopen(file, "w", stdout) #define all(c) c.begin(), c.end() #define tr(container, it) \ for (__typeof__(container.begin()) it = container.begin(); \ it != container.end(); it++) #define present(container, element) \ (container.find(element) != container.end()) // map, set #define cpresent(container, element) \ (find(aint(container), element) != container.end()) // vector #define what_is(x) cout << #x << " is " << x << endl; #define all(c) c.begin(), c.end() // #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; const int MOD = 1e9 + 7; int fast_pow(int a, int b) { int res = a, ret = 1; while (b > 0) { if (b % 2) ret = (ret * res) % MOD; res = (res * res) % MOD; b /= 2; } return ret; } // -- // -- // -- // std::cout << std::fixed; // std::cout << std::setprecision(26) << f << '\n'; // str = to_string(n) // stoi , stoll, stol // sort(arr, arr+n, greater<int>()); // fill(prefix.begin(), prefix.end(), 0); // // //// const int maxN = 1e5 + 700; int arr[maxN]; int dp[maxN]; //// signed main() { fastio; time_t time_t1, time_t2; time_t1 = clock(); //////////////////////////////// int t = 1; // cin >> t ; while (t--) { int n; cin >> n; int k; cin >> k; for (int i = 0; i < n; ++i) { cin >> arr[i]; } for (int i = 0; i <= n; ++i) { dp[i] = 1e16; } dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) { dp[i + j] = min(dp[i + j], dp[i] + abs(arr[i + j] - arr[i])); } } cout << dp[n - 1] << endl; } /////////////////////////////// // time_t2 = clock(); // cerr << "time taken :" << time_t2 - time_t1 << endl; return 0; }
replace
86
87
86
87
0
p03161
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; typedef long long ll; const ll INF = 1LL << 60; int main() { int n, k; cin >> n >> k; int A[100000]; for (int i = 0; i < n; i++) { cin >> A[i]; } vector<ll> dp(n, INF); dp[0] = 0; for (int i = 1; i < n; i++) { for (int j = 1; j <= k; j++) { dp[i] = min(dp[i], dp[i - j] + abs(A[i] - A[i - j])); } } cout << dp.back() << endl; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; typedef long long ll; const ll INF = 1LL << 60; int main() { int n, k; cin >> n >> k; int A[100000]; for (int i = 0; i < n; i++) { cin >> A[i]; } vector<ll> dp(n, INF); dp[0] = 0; for (int i = 1; i < n; i++) { for (int j = 1; j <= k; j++) { if (i - j >= 0) { dp[i] = min(dp[i], dp[i - j] + abs(A[i] - A[i - j])); } } } cout << dp.back() << endl; }
replace
24
25
24
27
0
p03161
C++
Runtime Error
/* The Island Was Silent before. ..... And One day again it became Silent. */ #include <bits/stdc++.h> using namespace std; #define endl '\n' #define ll long long #define modd(a, b) ((a + 2 * b) % b) #define debug(a) cout << #a << ": " << (a) << "\n" #define ioso ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define rtt cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC << endl; #define ffe \ freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout); int main() { ioso #ifndef ONLINE_JUDGE ffe #endif ll n, k; cin >> n >> k; int co[100010]; for (int i = 1; i <= n; i++) { cin >> co[i]; } int dp[100010]; memset(dp, -1, sizeof(dp)); dp[1] = 0; dp[0] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { if (dp[i + j] == -1) { dp[i + j] = dp[i] + (abs(co[i + j] - co[i])); } else { dp[i + j] = min(dp[i + j], dp[i] + abs(co[i + j] - co[i])); } } } cout << dp[n] << endl; }
/* The Island Was Silent before. ..... And One day again it became Silent. */ #include <bits/stdc++.h> using namespace std; #define endl '\n' #define ll long long #define modd(a, b) ((a + 2 * b) % b) #define debug(a) cout << #a << ": " << (a) << "\n" #define ioso ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define rtt cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC << endl; #define ffe \ freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout); int main() { ioso #ifndef ONLINE_JUDGE ffe #endif ll n, k; cin >> n >> k; int co[100010]; for (int i = 1; i <= n; i++) { cin >> co[i]; } int dp[100010]; memset(dp, -1, sizeof(dp)); dp[1] = 0; dp[0] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { if (i + j <= n) { if (dp[i + j] == -1) { dp[i + j] = dp[i] + (abs(co[i + j] - co[i])); } else { dp[i + j] = min(dp[i + j], dp[i] + abs(co[i + j] - co[i])); } } } } cout << dp[n] << endl; }
replace
35
39
35
41
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define itn int #define REP(i, n) for (int i = 0; i < n; i++) #define IREP(i, n) for (int i = n - 1; i >= 0; i--) #define REPEACH(i, k) for (auto &i : k) #define all(a) a.begin(), a.end() #define MOD 1000000007 using namespace std; typedef long long ll; const ll INF = 1LL << 60; signed main() { int n, k; cin >> n >> k; vector<int> a(n); vector<ll> dp(n, INF); REP(i, n) { cin >> a[i]; if (i != 0) { if (i <= 1) { for (int j = 0; j != i; j++) { dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j])); } } else { for (int j = i - 1; j >= i - k; j--) { dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j])); } } } else { dp[0] = 0; } } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> #define itn int #define REP(i, n) for (int i = 0; i < n; i++) #define IREP(i, n) for (int i = n - 1; i >= 0; i--) #define REPEACH(i, k) for (auto &i : k) #define all(a) a.begin(), a.end() #define MOD 1000000007 using namespace std; typedef long long ll; const ll INF = 1LL << 60; signed main() { int n, k; cin >> n >> k; vector<int> a(n); vector<ll> dp(n, INF); REP(i, n) { cin >> a[i]; if (i != 0) { if (i <= k) { for (int j = 0; j != i; j++) { dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j])); } } else { for (int j = i - 1; j >= i - k; j--) { dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j])); } } } else { dp[0] = 0; } } cout << dp[n - 1] << endl; }
replace
20
21
20
21
0
p03161
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <vector> #define MOD (1000000007) using namespace std; typedef long long ll; typedef pair<ll, ll> tup; const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; ll modpow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } template <typename T> bool next_combination(const T first, const T last, int k) { const T subset = first + k; // empty container | k = 0 | k == n if (first == last || first == subset || last == subset) { return false; } T src = subset; while (first != src) { src--; if (*src < *(last - 1)) { T dest = subset; while (*src >= *dest) { dest++; } iter_swap(src, dest); rotate(src + 1, dest + 1, last); rotate(subset, subset + (last - dest) - 1, last); return true; } } // restore rotate(first, subset, last); return false; } int main() { ll N, K; cin >> N >> K; vector<ll> h(N); for (ll i = 0; i < N; i++) cin >> h[i]; vector<ll> dp(N); dp[0] = 0; for (ll i = 1; i < N; i++) { ll temp = 999999999; for (ll j = 1; j <= K; j++) temp = min(temp, abs(h[i - j] - h[i]) + dp[i - j]); dp[i] = temp; } // for(ll i = 0; i < N-1; i++) { // cout << dp[i] <<" " ; // } cout << dp[N - 1] << endl; return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <vector> #define MOD (1000000007) using namespace std; typedef long long ll; typedef pair<ll, ll> tup; const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; ll modpow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } template <typename T> bool next_combination(const T first, const T last, int k) { const T subset = first + k; // empty container | k = 0 | k == n if (first == last || first == subset || last == subset) { return false; } T src = subset; while (first != src) { src--; if (*src < *(last - 1)) { T dest = subset; while (*src >= *dest) { dest++; } iter_swap(src, dest); rotate(src + 1, dest + 1, last); rotate(subset, subset + (last - dest) - 1, last); return true; } } // restore rotate(first, subset, last); return false; } int main() { ll N, K; cin >> N >> K; vector<ll> h(N); for (ll i = 0; i < N; i++) cin >> h[i]; vector<ll> dp(N); dp[0] = 0; for (ll i = 1; i < N; i++) { ll temp = 999999999; for (ll j = 1; j <= min(K, i); j++) temp = min(temp, abs(h[i - j] - h[i]) + dp[i - j]); dp[i] = temp; } // for(ll i = 0; i < N-1; i++) { // cout << dp[i] <<" " ; // } cout << dp[N - 1] << endl; return 0; }
replace
91
92
91
92
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, k; cin >> N >> k; int h[N]; // INPUT ARRAY int ans[N]; // OUTPUT ARRAY for (int i = 0; i < N; i++) { cin >> h[i]; ans[i] = INT_MAX; } ans[0] = 0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N, j - i <= k; j++) ans[j] = min(ans[j], ans[i] + abs(h[i] - h[j])); } cout << ans[N - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N, k; cin >> N >> k; int h[N]; // INPUT ARRAY int ans[N]; // OUTPUT ARRAY for (int i = 0; i < N; i++) { cin >> h[i]; ans[i] = INT_MAX; } ans[0] = 0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N && j - i <= k; j++) ans[j] = min(ans[j], ans[i] + abs(h[i] - h[j])); } cout << ans[N - 1]; return 0; }
replace
14
15
14
15
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int nature(ll a) { // 絶対値を返す if (a >= 0) { return (a); } else { return (-1 * a); } } int main() { ll N, K, h[N], dp[N], ans; cin >> N >> K; // 足場の個数N//何個先まで飛べるかK for (ll i = 0; i < N; i++) { // 各足場の高さ cin >> h[i]; } dp[0] = 0; for (ll i = 1; i < N; i++) { ans = dp[i - 1] + nature(h[i] - h[i - 1]); for (ll j = 1; j <= min(K, i); j++) { ans = min(dp[i - j] + nature(h[i] - h[i - j]), ans); // 各足場までの最小コスト } dp[i] = ans; } cout << dp[N - 1] << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long int nature(ll a) { // 絶対値を返す if (a >= 0) { return (a); } else { return (-1 * a); } } int main() { ll N, K, h[100010], dp[100010], ans; cin >> N >> K; // 足場の個数N//何個先まで飛べるかK for (ll i = 0; i < N; i++) { // 各足場の高さ cin >> h[i]; } dp[0] = 0; for (ll i = 1; i < N; i++) { ans = dp[i - 1] + nature(h[i] - h[i - 1]); for (ll j = 1; j <= min(K, i); j++) { ans = min(dp[i - j] + nature(h[i] - h[i - j]), ans); // 各足場までの最小コスト } dp[i] = ans; } cout << dp[N - 1] << endl; }
replace
11
12
11
12
-11
p03161
C++
Runtime Error
// thuanqvbn03 #include <bits/stdc++.h> using namespace std; const int MaxN = 100005; const int oo = 1e9; int n, k; int h[MaxN], dp[MaxN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> h[i]; dp[i] = oo; } dp[1] = 0; for (int i = 1; i < n; i++) { for (int j = min(n, i + k); j > i; i--) { dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j])); } } cout << dp[n]; return 0; }
// thuanqvbn03 #include <bits/stdc++.h> using namespace std; const int MaxN = 100005; const int oo = 1e9; int n, k; int h[MaxN], dp[MaxN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> h[i]; dp[i] = oo; } dp[1] = 0; for (int i = 1; i < n; i++) { for (int j = min(n, i + k); j > i; j--) { dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j])); } } cout << dp[n]; return 0; }
replace
22
23
22
23
-11
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define maxx 100010 #define mp make_pair typedef pair<int, int> ii; int dp[maxx]; vector<int> peso; int x, k; int main() { cin >> x >> k; int n; for (int i = 0; i < x; i++) { cin >> n; peso.push_back(n); dp[i] = 1e9; } dp[0] = 0; dp[1] = dp[0] + abs(peso[0] - peso[1]); for (int j = 1; j <= k; j++) { for (int i = 2; i < x; i++) dp[i] = min(dp[i - k] + abs(peso[i - k] - peso[i]), dp[i]); } cout << dp[x - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define maxx 100010 #define mp make_pair typedef pair<int, int> ii; int dp[maxx]; vector<int> peso; int x, k; int main() { cin >> x >> k; int n; for (int i = 0; i < x; i++) { cin >> n; peso.push_back(n); dp[i] = 1e9; } dp[0] = 0; dp[1] = dp[0] + abs(peso[0] - peso[1]); for (int i = 2; i < x; i++) { for (int j = 1; j <= min(i, k); j++) dp[i] = min(dp[i - j] + abs(peso[i - j] - peso[i]), dp[i]); } cout << dp[x - 1] << endl; return 0; }
replace
20
23
20
23
0
p03161
C++
Runtime Error
#include <climits> #include <iostream> #include <vector> using namespace std; int main() { int n, k; cin >> n; cin >> k; vector<int> v(n); vector<int> dp(n, INT_MAX); for (int i = 0; i < n; i++) { cin >> v[i]; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j <= k + i && j <= n; j++) { dp[j] = min(dp[i] + abs(v[i] - v[j]), dp[j]); } } cout << dp[n - 1] << endl; }
#include <climits> #include <iostream> #include <vector> using namespace std; int main() { int n, k; cin >> n; cin >> k; vector<int> v(n); vector<int> dp(n, INT_MAX); for (int i = 0; i < n; i++) { cin >> v[i]; } dp[0] = 0; for (int i = 1; i < n; i++) { for (int j = 1; j <= k && i - j >= 0; j++) { dp[i] = min(dp[i - j] + abs(v[i] - v[i - j]), dp[i]); } } cout << dp[n - 1] << endl; }
replace
20
23
20
23
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define ll long long #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl "\n" ll int gcd(ll int a, ll int b) { return b ? gcd(b, a % b) : a; } int main() { int n, k; cin >> n >> k; std::vector<int> v(n); int dp[n]; for (int i = 0; i < n; ++i) { cin >> v[i]; } dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (int i = 2; i < n; i++) { int val = INT_MAX; int j = 1; while (j <= k && i - j < n) { val = min(dp[i - j] + abs(v[i] - v[i - j]), val); // cout<<"hi "<<val<<"\n"; j++; } dp[i] = val; // cout<<i<<" "<<dp[i]<<"--"<<dp[i-1]<<"--"<<dp[i-2]<<"\n"; } cout << dp[n - 1] << "\n"; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define ll long long #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl "\n" ll int gcd(ll int a, ll int b) { return b ? gcd(b, a % b) : a; } int main() { int n, k; cin >> n >> k; std::vector<int> v(n); int dp[n]; for (int i = 0; i < n; ++i) { cin >> v[i]; } dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (int i = 2; i < n; i++) { int val = INT_MAX; int j = 1; while (j <= k && i - j >= 0) { val = min(dp[i - j] + abs(v[i] - v[i - j]), val); // cout<<"hi "<<val<<"\n"; j++; } dp[i] = val; // cout<<i<<" "<<dp[i]<<"--"<<dp[i-1]<<"--"<<dp[i-2]<<"\n"; } cout << dp[n - 1] << "\n"; }
replace
25
26
25
26
0
p03161
C++
Runtime Error
// A-Frog2.cpp #include <bits/stdc++.h> #define ll long long int using namespace std; int main() { // freopen("input.txt","r",stdin); ll n, k; cin >> n >> k; ll *arr = new ll[n]; for (ll i = 0; i < n; i++) cin >> arr[i]; ll *dp = new ll[n + 1]{}; dp[0] = 0; dp[1] = abs(arr[1] - arr[0]); for (ll i = 2; i < n; i++) { ll best = INT_MAX; for (ll j = 1; j <= k; j++) { best = min(best, dp[i - j] + abs(arr[i - j] - arr[i])); } dp[i] = best; } // for(ll i=0;i<n;i++)cout<<dp[i]<<" "; cout << dp[n - 1] << endl; return 0; }
// A-Frog2.cpp #include <bits/stdc++.h> #define ll long long int using namespace std; int main() { // freopen("input.txt","r",stdin); ll n, k; cin >> n >> k; ll *arr = new ll[n]; for (ll i = 0; i < n; i++) cin >> arr[i]; ll *dp = new ll[n + 1]{}; dp[0] = 0; dp[1] = abs(arr[1] - arr[0]); for (ll i = 2; i < n; i++) { ll best = INT_MAX; for (ll j = 1; j <= k; j++) { if (j <= i) best = min(best, dp[i - j] + abs(arr[i - j] - arr[i])); } dp[i] = best; } // for(ll i=0;i<n;i++)cout<<dp[i]<<" "; cout << dp[n - 1] << endl; return 0; }
replace
18
19
18
20
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define be begin #define en end #define pb push_back #define eb emplace_back #define mp make_pair #define ALL(a) (a).be(), (a).en() using LL = long long; template <typename T> using V = vector<T>; using Vi = V<int>; using Vll = V<LL>; using Vs = V<string>; const LL INFLL = 999999999999999LL; template <typename A, size_t N, typename T> void FILL(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } 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; } LL dp[100005]; int main() { int n, k; cin >> n >> k; Vi h(n); for (int i = 0; i < n; i++) { cin >> h[i]; } FILL(dp, INFLL); dp[0] = 0; for (int i = 1; i < n; i++) { int w = ((i - k >= 0) ? k : k - 1); for (int j = 1; j <= w; j++) chmin(dp[i], dp[i - j] + abs(h[i - j] - h[i])); } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define be begin #define en end #define pb push_back #define eb emplace_back #define mp make_pair #define ALL(a) (a).be(), (a).en() using LL = long long; template <typename T> using V = vector<T>; using Vi = V<int>; using Vll = V<LL>; using Vs = V<string>; const LL INFLL = 999999999999999LL; template <typename A, size_t N, typename T> void FILL(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } 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; } LL dp[100005]; int main() { int n, k; cin >> n >> k; Vi h(n); for (int i = 0; i < n; i++) { cin >> h[i]; } FILL(dp, INFLL); dp[0] = 0; for (int i = 1; i < n; i++) { int w = ((i - k >= 0) ? k : i); for (int j = 1; j <= w; j++) chmin(dp[i], dp[i - j] + abs(h[i - j] - h[i])); } cout << dp[n - 1] << endl; return 0; }
replace
56
57
56
57
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define test() \ ull t; \ cin >> t; \ while (t--) #define pb push_back #define mkp make_pair #define nl cout << endl #define MOD 1000000007 #define loop(i, start, end) for (ull i = start; i < end; i++) #define N 100001 #define all(v) v.begin(), v.end() #define oa(a, n) \ for (int i = 0; i < n; i++) \ cout << a[i] << " "; \ nl #define ov(a) \ for (int i = 0; i < a.size(); i++) \ cout << a[i] << " "; \ nl int main() { fastio(); ll n, k; cin >> n >> k; vector<ll> v(n), dp(n + 1, 0); loop(i, 0, n) cin >> v[i]; for (int i = 1; i < n; i++) { dp[i] = dp[i - 1] + abs(v[i] - v[i - 1]); for (int j = 2; i >= j && j <= k; j++) dp[i] = min(dp[i], dp[i - k] + abs(v[i] - v[i - k])); } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define test() \ ull t; \ cin >> t; \ while (t--) #define pb push_back #define mkp make_pair #define nl cout << endl #define MOD 1000000007 #define loop(i, start, end) for (ull i = start; i < end; i++) #define N 100001 #define all(v) v.begin(), v.end() #define oa(a, n) \ for (int i = 0; i < n; i++) \ cout << a[i] << " "; \ nl #define ov(a) \ for (int i = 0; i < a.size(); i++) \ cout << a[i] << " "; \ nl int main() { fastio(); ll n, k; cin >> n >> k; vector<ll> v(n), dp(n + 1, 0); loop(i, 0, n) cin >> v[i]; for (int i = 1; i < n; i++) { dp[i] = dp[i - 1] + abs(v[i] - v[i - 1]); for (int j = 2; i >= j && j <= k; j++) dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j])); } cout << dp[n - 1]; return 0; }
replace
38
39
38
39
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int mod(int a) { if (a > 0) { return (a); } return (-1 * a); } int min(int a, int b) { if (a > b) { // cout<<b<<"\t" ; return (b); } // cout<<a<<"\t" ; return (a); } int main() { int a, b, c, i, j, n, k; cin >> n >> k; vector<int> v, cost; for (i = 0; i < n; i = i + 1) { cin >> a; v.push_back(a); } int arr[n]; arr[1] = mod(v[1] - v[0]); arr[0] = 0; for (i = 2; i < n; i = i + 1) { b = arr[i - 1] + mod(v[i - 1] - v[i]); for (j = 2; j <= k; j = j + 1) { c = arr[i - j] + mod(v[i - j] - v[i]); b = min(b, c); } arr[i] = b; } cout << arr[n - 1]; }
#include <bits/stdc++.h> using namespace std; int mod(int a) { if (a > 0) { return (a); } return (-1 * a); } int min(int a, int b) { if (a > b) { // cout<<b<<"\t" ; return (b); } // cout<<a<<"\t" ; return (a); } int main() { int a, b, c, i, j, n, k; cin >> n >> k; vector<int> v, cost; for (i = 0; i < n; i = i + 1) { cin >> a; v.push_back(a); } int arr[n]; arr[1] = mod(v[1] - v[0]); arr[0] = 0; for (i = 2; i < n; i = i + 1) { b = arr[i - 1] + mod(v[i - 1] - v[i]); for (j = 2; j <= k; j = j + 1) { if (i - j < 0) { break; } c = arr[i - j] + mod(v[i - j] - v[i]); b = min(b, c); } arr[i] = b; } cout << arr[n - 1]; }
insert
31
31
31
34
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define fu(a, b) for (int i = a; i < b; i++) #define fv(a, b) for (int j = a; j < b; j++) using namespace std; int mini(int x, int y) { return x > y ? y : x; } int main() { int dp[100001]; fu(0, 100001) dp[i] = INT_MAX; dp[0] = dp[1] = 0; int n, k; cin >> n >> k; int a[n + 1]; fu(1, n + 1) { cin >> a[i]; } dp[2] = abs(a[1] - a[2]); fu(1, n + 1) { fv(1, k + 1) { dp[i + j] = mini(dp[i + j], dp[i] + abs(a[i] - a[i + j])); if (i + j == n) break; } } cout << dp[n]; }
#include <bits/stdc++.h> #define fu(a, b) for (int i = a; i < b; i++) #define fv(a, b) for (int j = a; j < b; j++) using namespace std; int mini(int x, int y) { return x > y ? y : x; } int main() { int dp[100001]; fu(0, 100001) dp[i] = INT_MAX; dp[0] = dp[1] = 0; int n, k; cin >> n >> k; int a[n + 1]; fu(1, n + 1) { cin >> a[i]; } dp[2] = abs(a[1] - a[2]); fu(1, n + 1) { fv(i + 1, i + k + 1) { if (j < n + 1) dp[j] = mini(dp[j], dp[i] + abs(a[i] - a[j])); } } cout << dp[n]; }
replace
17
21
17
20
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() typedef long long ll; #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i) #define REP(i, num, n) for (ll i = num, i##_len = (n); i < i##_len; ++i) #define repprev(i, a, b) for (ll i = b - 1; i >= a; i--) #define reprev(i, n) repprev(i, 0, n) using namespace std; #define sz(x) ((int)(x).size()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MEMSET(v, h) memset((v), h, sizeof(v)) 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; } template <class T> int former(const vector<T> &v, T x) { return upper_bound(v.begin(), v.end(), x) - v.begin() - 1; } template <class T> int latter(const vector<T> &v, T x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } #define pb push_back #define mp make_pair #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define BIT_FLAG_0 (1 << 0) // 0000 0000 0000 0001 #define BIT_FLAG_1 (1 << 1) // 0000 0000 0000 0010 #define BIT_FLAG_2 (1 << 2) // 0000 0000 0000 0100 #define BIT_FLAG_3 (1 << 3) // 0000 0000 0000 1000 #define BIT_FLAG_4 (1 << 4) // 0000 0000 0001 0000 #define BIT_FLAG_5 (1 << 5) // 0000 0000 0010 0000 #define BIT_FLAG_6 (1 << 6) // 0000 0000 0100 0000 #define BIT_FLAG_7 (1 << 7) // 0000 0000 1000 0000 ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } const ll LLINF = 1LL << 60; const int INTINF = 1 << 30; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } struct UnionFind { vector<ll> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(ll n) : par(n, -1) {} void init(ll n) { par.assign(n, -1); } ll root(ll x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(ll x, ll y) { return root(x) == root(y); } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } ll size(ll x) { return -par[root(x)]; } }; template <typename T> vector<T> dijkstra(int s, vector<vector<pair<int, T>>> &G) { const T INF = numeric_limits<T>::max(); using P = pair<T, int>; int n = G.size(); vector<T> d(n, INF); vector<int> b(n, -1); priority_queue<P, vector<P>, greater<P>> q; d[s] = 0; q.emplace(d[s], s); while (!q.empty()) { P p = q.top(); q.pop(); int v = p.second; if (d[v] < p.first) continue; for (auto &e : G[v]) { int u = e.first; T c = e.second; if (d[u] > d[v] + c) { d[u] = d[v] + c; b[u] = v; q.emplace(d[u], u); } } } return d; } vector<vector<int>> bfs(vector<string> &s, int sy, int sx, char wall, int dir) { int h = s.size(), w = s.front().size(); vector<vector<int>> dp(h, vector<int>(w, -1)); using P = pair<int, int>; queue<P> q; dp[sy][sx] = 0; q.emplace(sy, sx); int dy[] = {1, -1, 0, 0, 1, 1, -1, -1}; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; auto in = [&](int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; }; while (!q.empty()) { int y, x; tie(y, x) = q.front(); q.pop(); for (int k = 0; k < dir; k++) { int ny = y + dy[k], nx = x + dx[k]; if (!in(ny, nx) || s[ny][nx] == wall) continue; if (~dp[ny][nx]) continue; dp[ny][nx] = dp[y][x] + 1; q.emplace(ny, nx); } } return dp; } int64_t power(int64_t x, int64_t n, int64_t mod) { int64_t ret = 1; while (n > 0) { if (n & 1) (ret *= x) %= mod; (x *= x) %= mod; n >>= 1; } return ret; } vector<int> sieve_of_eratosthenes(int n) { vector<int> primes(n); for (int i = 2; i < n; ++i) primes[i] = i; for (int i = 2; i * i < n; ++i) if (primes[i]) for (int j = i * i; j < n; j += i) primes[j] = 0; return primes; } struct Dice { int s[6]; int &top() { return s[0]; } int &south() { return s[1]; } int &east() { return s[2]; } int &west() { return s[3]; } int &north() { return s[4]; } int &bottom() { return s[5]; } void roll(char c) { // the view from above // N // W E // S string b("EWNSRL"); int v[6][4] = {{0, 3, 5, 2}, {0, 2, 5, 3}, {0, 1, 5, 4}, {0, 4, 5, 1}, {1, 2, 4, 3}, {1, 3, 4, 2}}; for (int k = 0; k < 6; k++) { if (b[k] != c) continue; int t = s[v[k][0]]; s[v[k][0]] = s[v[k][1]]; s[v[k][1]] = s[v[k][2]]; s[v[k][2]] = s[v[k][3]]; s[v[k][3]] = t; } } using ll = long long; ll hash() { ll res = 0; for (int i = 0; i < 6; i++) res = res * 256 + s[i]; return res; } bool operator==(const Dice &d) const { for (int i = 0; i < 6; i++) if (s[i] != d.s[i]) return 0; return 1; } }; vector<Dice> makeDices(Dice d) { vector<Dice> res; for (int i = 0; i < 6; i++) { Dice t(d); if (i == 1) t.roll('N'); if (i == 2) t.roll('S'); if (i == 3) t.roll('S'), t.roll('S'); if (i == 4) t.roll('L'); if (i == 5) t.roll('R'); for (int k = 0; k < 4; k++) { res.push_back(t); t.roll('E'); } } return res; } std::vector<ll> divisor(ll n) // nの約数を列挙 { std::vector<ll> ret; for (ll i = 1; i * i <= n; ++i) { if (n % i == 0) { ret.push_back(i); if (i != 1 && i * i != n) { ret.push_back(n / i); } } } return ret; } // 多次元 vector 生成 template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } using Graph = vector<vector<int>>; // 深さ優先探索 vector<bool> seen; void gdfs(const Graph &G, int v) { seen[v] = true; // v を訪問済にする // v から行ける各頂点 next_v について for (auto next_v : G[v]) { if (seen[next_v]) continue; // next_v が探索済だったらスルー gdfs(G, next_v); // 再帰的に探索 } } const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const string MINUSINF = "-"; void Cmax(string &a, string b) { if (a == MINUSINF) a = b; else if (a.size() < b.size()) a = b; else if (a.size() == b.size()) { if (a < b) a = b; } } ll dp[100100]; int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll N, K; cin >> N >> K; vector<ll> h(N); rep(i, N) cin >> h[i]; dp[0] = 0; REP(i, 1, 100100) dp[i] = LLINF; REP(i, 1, N) { if (i < 2) dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); else { REP(j, 1, K + 1) { chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } } cout << dp[N - 1] << endl; }
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() typedef long long ll; #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i) #define REP(i, num, n) for (ll i = num, i##_len = (n); i < i##_len; ++i) #define repprev(i, a, b) for (ll i = b - 1; i >= a; i--) #define reprev(i, n) repprev(i, 0, n) using namespace std; #define sz(x) ((int)(x).size()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MEMSET(v, h) memset((v), h, sizeof(v)) 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; } template <class T> int former(const vector<T> &v, T x) { return upper_bound(v.begin(), v.end(), x) - v.begin() - 1; } template <class T> int latter(const vector<T> &v, T x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } #define pb push_back #define mp make_pair #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define BIT_FLAG_0 (1 << 0) // 0000 0000 0000 0001 #define BIT_FLAG_1 (1 << 1) // 0000 0000 0000 0010 #define BIT_FLAG_2 (1 << 2) // 0000 0000 0000 0100 #define BIT_FLAG_3 (1 << 3) // 0000 0000 0000 1000 #define BIT_FLAG_4 (1 << 4) // 0000 0000 0001 0000 #define BIT_FLAG_5 (1 << 5) // 0000 0000 0010 0000 #define BIT_FLAG_6 (1 << 6) // 0000 0000 0100 0000 #define BIT_FLAG_7 (1 << 7) // 0000 0000 1000 0000 ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } const ll LLINF = 1LL << 60; const int INTINF = 1 << 30; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } struct UnionFind { vector<ll> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(ll n) : par(n, -1) {} void init(ll n) { par.assign(n, -1); } ll root(ll x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(ll x, ll y) { return root(x) == root(y); } bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } ll size(ll x) { return -par[root(x)]; } }; template <typename T> vector<T> dijkstra(int s, vector<vector<pair<int, T>>> &G) { const T INF = numeric_limits<T>::max(); using P = pair<T, int>; int n = G.size(); vector<T> d(n, INF); vector<int> b(n, -1); priority_queue<P, vector<P>, greater<P>> q; d[s] = 0; q.emplace(d[s], s); while (!q.empty()) { P p = q.top(); q.pop(); int v = p.second; if (d[v] < p.first) continue; for (auto &e : G[v]) { int u = e.first; T c = e.second; if (d[u] > d[v] + c) { d[u] = d[v] + c; b[u] = v; q.emplace(d[u], u); } } } return d; } vector<vector<int>> bfs(vector<string> &s, int sy, int sx, char wall, int dir) { int h = s.size(), w = s.front().size(); vector<vector<int>> dp(h, vector<int>(w, -1)); using P = pair<int, int>; queue<P> q; dp[sy][sx] = 0; q.emplace(sy, sx); int dy[] = {1, -1, 0, 0, 1, 1, -1, -1}; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; auto in = [&](int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; }; while (!q.empty()) { int y, x; tie(y, x) = q.front(); q.pop(); for (int k = 0; k < dir; k++) { int ny = y + dy[k], nx = x + dx[k]; if (!in(ny, nx) || s[ny][nx] == wall) continue; if (~dp[ny][nx]) continue; dp[ny][nx] = dp[y][x] + 1; q.emplace(ny, nx); } } return dp; } int64_t power(int64_t x, int64_t n, int64_t mod) { int64_t ret = 1; while (n > 0) { if (n & 1) (ret *= x) %= mod; (x *= x) %= mod; n >>= 1; } return ret; } vector<int> sieve_of_eratosthenes(int n) { vector<int> primes(n); for (int i = 2; i < n; ++i) primes[i] = i; for (int i = 2; i * i < n; ++i) if (primes[i]) for (int j = i * i; j < n; j += i) primes[j] = 0; return primes; } struct Dice { int s[6]; int &top() { return s[0]; } int &south() { return s[1]; } int &east() { return s[2]; } int &west() { return s[3]; } int &north() { return s[4]; } int &bottom() { return s[5]; } void roll(char c) { // the view from above // N // W E // S string b("EWNSRL"); int v[6][4] = {{0, 3, 5, 2}, {0, 2, 5, 3}, {0, 1, 5, 4}, {0, 4, 5, 1}, {1, 2, 4, 3}, {1, 3, 4, 2}}; for (int k = 0; k < 6; k++) { if (b[k] != c) continue; int t = s[v[k][0]]; s[v[k][0]] = s[v[k][1]]; s[v[k][1]] = s[v[k][2]]; s[v[k][2]] = s[v[k][3]]; s[v[k][3]] = t; } } using ll = long long; ll hash() { ll res = 0; for (int i = 0; i < 6; i++) res = res * 256 + s[i]; return res; } bool operator==(const Dice &d) const { for (int i = 0; i < 6; i++) if (s[i] != d.s[i]) return 0; return 1; } }; vector<Dice> makeDices(Dice d) { vector<Dice> res; for (int i = 0; i < 6; i++) { Dice t(d); if (i == 1) t.roll('N'); if (i == 2) t.roll('S'); if (i == 3) t.roll('S'), t.roll('S'); if (i == 4) t.roll('L'); if (i == 5) t.roll('R'); for (int k = 0; k < 4; k++) { res.push_back(t); t.roll('E'); } } return res; } std::vector<ll> divisor(ll n) // nの約数を列挙 { std::vector<ll> ret; for (ll i = 1; i * i <= n; ++i) { if (n % i == 0) { ret.push_back(i); if (i != 1 && i * i != n) { ret.push_back(n / i); } } } return ret; } // 多次元 vector 生成 template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } using Graph = vector<vector<int>>; // 深さ優先探索 vector<bool> seen; void gdfs(const Graph &G, int v) { seen[v] = true; // v を訪問済にする // v から行ける各頂点 next_v について for (auto next_v : G[v]) { if (seen[next_v]) continue; // next_v が探索済だったらスルー gdfs(G, next_v); // 再帰的に探索 } } const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const string MINUSINF = "-"; void Cmax(string &a, string b) { if (a == MINUSINF) a = b; else if (a.size() < b.size()) a = b; else if (a.size() == b.size()) { if (a < b) a = b; } } ll dp[100100]; int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll N, K; cin >> N >> K; vector<ll> h(N); rep(i, N) cin >> h[i]; dp[0] = 0; REP(i, 1, 100100) dp[i] = LLINF; REP(i, 1, N) { if (i < 2) dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); else { REP(j, 1, K + 1) { if (j > i) break; chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } } cout << dp[N - 1] << endl; }
replace
315
316
315
320
0
p03161
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> P; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define repone(i, n) for (ll i = 1; i <= (ll)(n); i++) #define each(i, mp) for (auto i : mp) #define FOR(i, m, n) for (ll i = m; i < n; i++) const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ll mod = 1000000007; const ll inf = ll(1e9); const ll half_inf = ll(1e5); const ll ll_inf = ll(1e9) * ll(1e9); 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; } int n, k; vector<ll> h; void solve() { ll dp[n]; rep(i, n) dp[i] = inf; dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; ++i) { for (int j = 1; j <= k; ++j) { chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[n - 1] << endl; } int main() { cin >> n >> k; h.resize(n); rep(i, n) cin >> h[i]; solve(); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> P; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define repone(i, n) for (ll i = 1; i <= (ll)(n); i++) #define each(i, mp) for (auto i : mp) #define FOR(i, m, n) for (ll i = m; i < n; i++) const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ll mod = 1000000007; const ll inf = ll(1e9); const ll half_inf = ll(1e5); const ll ll_inf = ll(1e9) * ll(1e9); 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; } int n, k; vector<ll> h; void solve() { ll dp[n]; rep(i, n) dp[i] = inf; dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; ++i) { for (int j = 1; j <= k; ++j) { if (i - j >= 0) chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j])); } } cout << dp[n - 1] << endl; } int main() { cin >> n >> k; h.resize(n); rep(i, n) cin >> h[i]; solve(); return 0; }
replace
52
53
52
54
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define lp(i, n) for (int i = 0; i < n; i++) #define all(x) (x).begin(), (x).end() #define pb push_back #define MP make_pair #define SZ(v) ((int)((v).size())) typedef long long ll; const ll MOD = 1e9 + 7; const int OO = (int)1e9; const int N = (int)1e5 + 5; const int K = 105; void init() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int n; int k; int dp[N]; int a[N]; int solve(int idx) { if (idx > n) return OO; if (idx == n - 1) return 0; int &ret = dp[idx]; if (~ret) return ret; ret = OO; for (int i = 1; i <= k; i++) { ret = min(ret, solve(idx + i) + abs(a[idx + i] - a[idx])); } return ret; } int dp2[N][K]; int solve_rec(int idx, int k_so_far) { if (idx == n - 1) return abs(a[idx] - a[idx - k_so_far]); if (k_so_far == k) return solve_rec(idx, 0) + abs(a[idx] - a[idx - k_so_far]); int &ret = dp2[idx][k_so_far]; if (~ret) return ret; ret = solve_rec(idx + 1, k_so_far + 1); if (k_so_far > 0) ret = min(ret, solve_rec(idx, 0) + abs(a[idx] - a[idx - k_so_far])); return ret; } int main() { #ifdef LOCAL_PROJECT freopen("in.txt", "r", stdin); #endif scanf("%d", &n); scanf("%d", &k); memset(dp, -1, sizeof(dp)); memset(dp, -1, sizeof(dp2)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } // printf("%d\n",solve(0)); printf("%d\n", solve_rec(0, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; #define lp(i, n) for (int i = 0; i < n; i++) #define all(x) (x).begin(), (x).end() #define pb push_back #define MP make_pair #define SZ(v) ((int)((v).size())) typedef long long ll; const ll MOD = 1e9 + 7; const int OO = (int)1e9; const int N = (int)1e5 + 5; const int K = 105; void init() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int n; int k; int dp[N]; int a[N]; int solve(int idx) { if (idx > n) return OO; if (idx == n - 1) return 0; int &ret = dp[idx]; if (~ret) return ret; ret = OO; for (int i = 1; i <= k; i++) { ret = min(ret, solve(idx + i) + abs(a[idx + i] - a[idx])); } return ret; } int dp2[N][K]; int solve_rec(int idx, int k_so_far) { if (idx == n - 1) return abs(a[idx] - a[idx - k_so_far]); if (k_so_far == k) return solve_rec(idx, 0) + abs(a[idx] - a[idx - k_so_far]); int &ret = dp2[idx][k_so_far]; if (~ret) return ret; ret = solve_rec(idx + 1, k_so_far + 1); if (k_so_far > 0) ret = min(ret, solve_rec(idx, 0) + abs(a[idx] - a[idx - k_so_far])); return ret; } int main() { #ifdef LOCAL_PROJECT freopen("in.txt", "r", stdin); #endif scanf("%d", &n); scanf("%d", &k); memset(dp, -1, sizeof(dp)); memset(dp2, -1, sizeof(dp2)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } // printf("%d\n",solve(0)); printf("%d\n", solve_rec(0, 0)); return 0; }
replace
59
60
59
60
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fastIO \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define File \ freopen("in.in", "r", stdin); \ freopen("out.out", "w", stdout) #define ll long long ll a[(int)1e5 + 10] = {0}; ll dp[(int)1e5 + 10]; ll n, k; ll solve(int i) { if (i + 1 == n) return 0; if (i > n) return 1e9; ll &re = dp[i]; if (re != -1) return re; for (int j = 1; j <= k; j++) re = min(abs(a[i] - a[i + j]) + solve(i + j), re == -1 ? (int)1e9 : re); return re; } int main() { fastIO; // File; int _(1); // cin >> _; while (_--) { memset(dp, -1, sizeof dp); cin >> n >> k; memset(a, 0, sizeof a); for (int i = 0; i < n; i++) cin >> a[i]; cout << solve(0) << endl; } }
#include <bits/stdc++.h> using namespace std; #define fastIO \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define File \ freopen("in.in", "r", stdin); \ freopen("out.out", "w", stdout) #define ll long long ll a[(int)1e5 + 200] = {0}; ll dp[(int)1e5 + 200]; ll n, k; ll solve(int i) { if (i + 1 == n) return 0; if (i > n) return 1e9; ll &re = dp[i]; if (re != -1) return re; for (int j = 1; j <= k; j++) re = min(abs(a[i] - a[i + j]) + solve(i + j), re == -1 ? (int)1e9 : re); return re; } int main() { fastIO; // File; int _(1); // cin >> _; while (_--) { memset(dp, -1, sizeof dp); cin >> n >> k; memset(a, 0, sizeof a); for (int i = 0; i < n; i++) cin >> a[i]; cout << solve(0) << endl; } }
replace
10
12
10
12
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define int long long int #define frwd(i, a, b, j) for (int i = a; i < b; i += j) #define rev(i, a, b, j) for (int i = a; i >= b; i -= j) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define tr(container, it) \ for (__typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) #define endl '\n' #define fi first #define se second #define mp make_pair #define pb push_back #define vi vector<int> #define vvi vector<vector<int>> #define vpi vector<pair<int, int>> #define pi pair<int, int> #define mxe(v) *max_element(v.begin(), v.end()) #define mne(v) *min_element(v.begin(), v.end()) #define ub upper_bound #define lb lower_bound #define INF LLONG_MAX #define mod 1000000007 using namespace std; void solve() { int n, k; cin >> n >> k; int a[n]; frwd(i, 0, n, 1) cin >> a[i]; vector<int> dp(n + 1, INF); dp[0] = 0; dp[1] = 0; dp[2] = abs(a[1] - a[0]); frwd(i, 3, n + 1, 1) { frwd(j, 1, k + 1, 1) { dp[i] = min(dp[i], dp[i - j] + abs(a[i - 1] - a[i - 1 - j])); } } // frwd(i,0,n+1,1) cout<<dp[i]<<endl; cout << dp[n]; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); /*freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);*/ int t; // cin>>t; t = 1; while (t--) { solve(); cout << endl; } return 0; }
#include <bits/stdc++.h> #define int long long int #define frwd(i, a, b, j) for (int i = a; i < b; i += j) #define rev(i, a, b, j) for (int i = a; i >= b; i -= j) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define tr(container, it) \ for (__typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) #define endl '\n' #define fi first #define se second #define mp make_pair #define pb push_back #define vi vector<int> #define vvi vector<vector<int>> #define vpi vector<pair<int, int>> #define pi pair<int, int> #define mxe(v) *max_element(v.begin(), v.end()) #define mne(v) *min_element(v.begin(), v.end()) #define ub upper_bound #define lb lower_bound #define INF LLONG_MAX #define mod 1000000007 using namespace std; void solve() { int n, k; cin >> n >> k; int a[n]; frwd(i, 0, n, 1) cin >> a[i]; vector<int> dp(n + 1, INF); dp[0] = 0; dp[1] = 0; dp[2] = abs(a[1] - a[0]); frwd(i, 3, n + 1, 1) { frwd(j, 1, k + 1, 1) { if (j < i) dp[i] = min(dp[i], dp[i - j] + abs(a[i - 1] - a[i - 1 - j])); } } // frwd(i,0,n+1,1) cout<<dp[i]<<endl; cout << dp[n]; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); /*freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);*/ int t; // cin>>t; t = 1; while (t--) { solve(); cout << endl; } return 0; }
replace
36
37
36
38
0
p03161
C++
Runtime Error
#pragma GCC Optimize("Ofast") #pragma GCC Optimize "trapv" #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define TC \ int t; \ cin >> t; \ while (t--) #define li long int #define ll long long #define ull unsigned long long #define Vec(x) vector<x> #define Pai(x, y) pair<x, y> #define Map(x, y) map<x, y> #define UMap(x, y) unordered_map<x, y> #define Set(x) set<x> #define Sta(x) stack<x> #define Qu(x) queue<x> #define DQ(x) deque(x) #define PriorQ(x) priority_queue<x> #define bg begin() #define ed end() #define sz size() #define ct(x) count(x) #define len length() #define F first #define S second #define Makep make_pair #define ins(x) insert(x) #define ers(x) erase(x) #define endl "\n"; #define MOD 1000000007 #define PI 2 * acos(0.0) #define clr() clear() #define fori0(n) for (int i = 0; i < n; i++) #define foriab(a, b) for (int i = a; i < b; i++) #define mem0(x) memset(x, 0, sizeof(x)) #define mem1(x) memset(x, 1, sizeof(x)) #define autoit(x) for (auto it = x.beg; it != x.ed; it++) #define count0(x) __builtin_popcount(x) #define ret return mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); const ll Inf = 1e9 + 5; ll a[100005], dp[100005], n, k; ll ans(ll i) { if (i == n - 1) return 0; if (i >= n) return INT_MAX; if (dp[i] != -1) return dp[i]; ll sol = INT_MAX; for (ll j = 1; j <= k; j++) { if (i + j < n) { sol = min(sol, abs(a[i] - a[j]) + ans(j)); } } return dp[i] = sol; } void solve() { memset(dp, -1, sizeof(dp)); cin >> n >> k; for (ll i = 0; i < n; i++) cin >> a[i]; cout << ans(0) << endl; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif FAST srand(chrono::high_resolution_clock::now().time_since_epoch().count()); solve(); ret 0; }
#pragma GCC Optimize("Ofast") #pragma GCC Optimize "trapv" #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define TC \ int t; \ cin >> t; \ while (t--) #define li long int #define ll long long #define ull unsigned long long #define Vec(x) vector<x> #define Pai(x, y) pair<x, y> #define Map(x, y) map<x, y> #define UMap(x, y) unordered_map<x, y> #define Set(x) set<x> #define Sta(x) stack<x> #define Qu(x) queue<x> #define DQ(x) deque(x) #define PriorQ(x) priority_queue<x> #define bg begin() #define ed end() #define sz size() #define ct(x) count(x) #define len length() #define F first #define S second #define Makep make_pair #define ins(x) insert(x) #define ers(x) erase(x) #define endl "\n"; #define MOD 1000000007 #define PI 2 * acos(0.0) #define clr() clear() #define fori0(n) for (int i = 0; i < n; i++) #define foriab(a, b) for (int i = a; i < b; i++) #define mem0(x) memset(x, 0, sizeof(x)) #define mem1(x) memset(x, 1, sizeof(x)) #define autoit(x) for (auto it = x.beg; it != x.ed; it++) #define count0(x) __builtin_popcount(x) #define ret return mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); const ll Inf = 1e9 + 5; ll a[100005], dp[100005], n, k; ll ans(ll i) { if (i == n - 1) return 0; if (i >= n) return INT_MAX; if (dp[i] != -1) return dp[i]; ll sol = INT_MAX; for (ll j = 1; j <= k; j++) { if (i + j < n) { sol = min(sol, abs(a[i] - a[i + j]) + ans(i + j)); } } return dp[i] = sol; } void solve() { memset(dp, -1, sizeof(dp)); cin >> n >> k; for (ll i = 0; i < n; i++) cin >> a[i]; cout << ans(0) << endl; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif FAST srand(chrono::high_resolution_clock::now().time_since_epoch().count()); solve(); ret 0; }
replace
62
63
62
63
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define inf 0x7fffffff #define ll long long #define REP(i, a, b) for (int i = a; i <= b; i++) using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // Code Starts... ll n, k; cin >> n >> k; vector<ll> v(n); for (int i = 0; i < n; i++) cin >> v[i]; vector<ll> dp(n, inf); // it stores the min cost incurred so far... dp[0] = 0; // cost at stone 1 is 0... for (int i = 1; i < n; i++) { for (int j = 1; j <= k; j++) dp[i] = min(dp[j], dp[i - j] + abs(v[i] - v[i - j])); } cout << dp[n - 1] << endl; // Code Ends... return 0; }
#include <bits/stdc++.h> #define inf 0x7fffffff #define ll long long #define REP(i, a, b) for (int i = a; i <= b; i++) using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // Code Starts... ll n, k; cin >> n >> k; vector<ll> v(n); for (int i = 0; i < n; i++) cin >> v[i]; vector<ll> dp(n, inf); // it stores the min cost incurred so far... dp[0] = 0; // cost at stone 1 is 0... for (int i = 1; i < n; i++) { for (int j = 1; j <= k; j++) if (i >= j) dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j])); } cout << dp[n - 1] << endl; // Code Ends... return 0; }
replace
19
20
19
21
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:2000000") #pragma comment(linker, "/HEAP:2000000") #define ll long long #define ld long double #define ull unsigned long long #define pb push_back #define f(n) for (ll i = 0; i < n; i++) #define fn(a, n) for (ll a = 0; a < n; a++) #define flr(a, l, r) for (ll a = l; a <= r; a++) #define mp make_pair #define sorta(a) sort(a.begin(), a.end()); #define sortd(a) sort(a.begin(), a.end(), greater<ll>()); #define sortdp(a) sort(a.begin(), a.end(), greater<pair<ll, ll>>()); #define vec std::vector<ll> #define vp std::vector<pair<ll, ll>> #define all(a) a.begin(), a.end() #define inf (long long)1e18 #define infi (int)1e9 #define endl '\n' #define ff(a, b) \ for (ll i = 0; i < a; i++) \ for (ll j = 0; j < b; j++) #define pp1(a) cout << a << endl; #define pp2(a, b) cout << a << " " << b << endl; #define pp3(a, b, c) cout << a << " " << b << " " << c << endl; #define pp4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl; #define vvl vector<vector<ll>> #define vvc std::vector<std::vector<char>> #define vvi std::vector<std::vector<int>> #define blb(a, b) lower_bound(all(a), b) - a.begin(); #define bub(a, b) upper_bound(all(a), b) - a.begin(); #define graph(n) vvl g(n + 1, vector<ll>(0)); #define in(a, n) \ vec a(n); \ f(n) cin >> a[i]; #define MOD 1000000007 #define MODLL 1000000000000000007 using namespace std; void print_width(ll x) { std::cout << std::fixed; std::cout << std::setprecision(x); } ll power(ll x, ll y, ll p = MOD) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } void printArr(ll a[], ll n) { f(n) cout << a[i] << " "; cout << endl; } void printVector(std::vector<ll> v) { f(v.size()) cout << v[i] << " "; cout << endl; } void printVectorPair(std::vector<pair<ll, ll>> v) { f(v.size()) pp2(v[i].first, v[i].second); cout << endl; } void initialize(ll arr[], ll n) { for (ll i = 0; i <= n; i++) arr[i] = i; } ll root(ll arr[], ll i) { while (arr[i] != i) { arr[i] = arr[arr[i]]; i = arr[i]; } return i; } void Union(ll arr[], ll a, ll b) { ll root_a = root(arr, a); ll root_b = root(arr, b); arr[root_a] = root_b; } ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } ll power_wm(ll x, ll y) { ll res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x); // y must be even now y = y >> 1; // y = y/2 x = (x * x); } return res; } std::vector<ll> vsum(std::vector<ll> a) { std::vector<ll> s(a.size()); s[0] = a[0]; flr(i, 1, a.size() - 1) { s[i] = s[i - 1] + a[i]; } return s; } void time() { #ifndef ONLINE_JUDGE cout << "\nTime: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; #endif } ll sti(string s) { ll ans = 0; ll p = 1; for (ll i = s.size() - 1; i >= 0; i--) { ans = (ans + ((ll)(s[i] - '0') * p) % MOD) % MOD; p = (p * 10) % MOD; } return ans; } int main() { // Using text files for input output #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // start your code here // print_width(15); ll n, k; cin >> n >> k; in(a, n); vec dp(n + 1, inf); dp[0] = 0; dp[1] = abs(a[0] - a[1]); for (ll i = 2; i < n; i++) { ll cost = inf; ll p = 1; while (p <= k) { if (i - p >= 0) { cost = min(cost, dp[i - p] + abs(a[i] - a[i - p])); } else { break; } p++; } dp[i] = cost; } cout << dp[n - 1]; // time(); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:2000000") #pragma comment(linker, "/HEAP:2000000") #define ll long long #define ld long double #define ull unsigned long long #define pb push_back #define f(n) for (ll i = 0; i < n; i++) #define fn(a, n) for (ll a = 0; a < n; a++) #define flr(a, l, r) for (ll a = l; a <= r; a++) #define mp make_pair #define sorta(a) sort(a.begin(), a.end()); #define sortd(a) sort(a.begin(), a.end(), greater<ll>()); #define sortdp(a) sort(a.begin(), a.end(), greater<pair<ll, ll>>()); #define vec std::vector<ll> #define vp std::vector<pair<ll, ll>> #define all(a) a.begin(), a.end() #define inf (long long)1e18 #define infi (int)1e9 #define endl '\n' #define ff(a, b) \ for (ll i = 0; i < a; i++) \ for (ll j = 0; j < b; j++) #define pp1(a) cout << a << endl; #define pp2(a, b) cout << a << " " << b << endl; #define pp3(a, b, c) cout << a << " " << b << " " << c << endl; #define pp4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl; #define vvl vector<vector<ll>> #define vvc std::vector<std::vector<char>> #define vvi std::vector<std::vector<int>> #define blb(a, b) lower_bound(all(a), b) - a.begin(); #define bub(a, b) upper_bound(all(a), b) - a.begin(); #define graph(n) vvl g(n + 1, vector<ll>(0)); #define in(a, n) \ vec a(n); \ f(n) cin >> a[i]; #define MOD 1000000007 #define MODLL 1000000000000000007 using namespace std; void print_width(ll x) { std::cout << std::fixed; std::cout << std::setprecision(x); } ll power(ll x, ll y, ll p = MOD) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } void printArr(ll a[], ll n) { f(n) cout << a[i] << " "; cout << endl; } void printVector(std::vector<ll> v) { f(v.size()) cout << v[i] << " "; cout << endl; } void printVectorPair(std::vector<pair<ll, ll>> v) { f(v.size()) pp2(v[i].first, v[i].second); cout << endl; } void initialize(ll arr[], ll n) { for (ll i = 0; i <= n; i++) arr[i] = i; } ll root(ll arr[], ll i) { while (arr[i] != i) { arr[i] = arr[arr[i]]; i = arr[i]; } return i; } void Union(ll arr[], ll a, ll b) { ll root_a = root(arr, a); ll root_b = root(arr, b); arr[root_a] = root_b; } ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } ll power_wm(ll x, ll y) { ll res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x); // y must be even now y = y >> 1; // y = y/2 x = (x * x); } return res; } std::vector<ll> vsum(std::vector<ll> a) { std::vector<ll> s(a.size()); s[0] = a[0]; flr(i, 1, a.size() - 1) { s[i] = s[i - 1] + a[i]; } return s; } void time() { #ifndef ONLINE_JUDGE cout << "\nTime: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; #endif } ll sti(string s) { ll ans = 0; ll p = 1; for (ll i = s.size() - 1; i >= 0; i--) { ans = (ans + ((ll)(s[i] - '0') * p) % MOD) % MOD; p = (p * 10) % MOD; } return ans; } int main() { // FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // start your code here // print_width(15); ll n, k; cin >> n >> k; in(a, n); vec dp(n + 1, inf); dp[0] = 0; dp[1] = abs(a[0] - a[1]); for (ll i = 2; i < n; i++) { ll cost = inf; ll p = 1; while (p <= k) { if (i - p >= 0) { cost = min(cost, dp[i - p] + abs(a[i] - a[i - p])); } else { break; } p++; } dp[i] = cost; } cout << dp[n - 1]; // time(); return 0; }
delete
154
160
154
154
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (long long i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } const int MOD = 1000000007; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3f; ll gcd(ll x, ll y) { if (y == 0) return x; else return gcd(y, x % y); } ll lcm(ll x, ll y) { ll g = gcd(x, y); return x / g * y; } using Graph = vector<vector<int>>; Graph G; int main() { // input ll n, k; cin >> n >> k; vector<ll> h(n); rep(i, n) cin >> h[i]; // initialization ll dp[100010] = {}; rep(i, 100010) dp[i] = INFL; dp[0] = 0; // compute rep(i, n) { for (int j = 1; j <= k; j++) { dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i])); } } // output cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (long long i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } const int MOD = 1000000007; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3f; ll gcd(ll x, ll y) { if (y == 0) return x; else return gcd(y, x % y); } ll lcm(ll x, ll y) { ll g = gcd(x, y); return x / g * y; } using Graph = vector<vector<int>>; Graph G; int main() { // input ll n, k; cin >> n >> k; vector<ll> h(n); rep(i, n) cin >> h[i]; // initialization ll dp[100010] = {}; rep(i, 100010) dp[i] = INFL; dp[0] = 0; // compute rep(i, n) { for (int j = 1; j <= k; j++) { if (i + j < n) dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i])); } } // output cout << dp[n - 1] << endl; }
replace
53
54
53
55
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long signed main() { cin.tie(0), cout.tie(0), ios::sync_with_stdio(0); int n, k, temp; cin >> n >> k; vector<int> vec(n + 1); for (int i = 1; i <= n; i++) cin >> vec[i]; vector<int> dp(n + 3, 1e12); dp[1] = 0; dp[2] = abs(vec[2] - vec[1]); for (int i = 3; i <= n; i++) { for (int j = 1; j <= k; j++) dp[i] = min(dp[i - j] + abs(vec[i - j] - vec[i]), dp[i]); } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long signed main() { cin.tie(0), cout.tie(0), ios::sync_with_stdio(0); int n, k, temp; cin >> n >> k; vector<int> vec(n + 1); for (int i = 1; i <= n; i++) cin >> vec[i]; vector<int> dp(n + 3, 1e12); dp[1] = 0; dp[2] = abs(vec[2] - vec[1]); for (int i = 3; i <= n; i++) { for (int j = 1; j <= k && i - j > 0; j++) dp[i] = min(dp[i - j] + abs(vec[i - j] - vec[i]), dp[i]); } cout << dp[n] << endl; return 0; }
replace
18
19
18
19
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define db double #define pb push_back #define rep(i, a, b) for (ll i = a; i <= b; i++) #define all(a) a.begin(), a.end() using namespace std; ll mod = 1e9 + 7; const ll M = 500006; ll f[M], inv[M]; ll expo(ll base, ll exponent) { ll ans = 1; while (exponent != 0) { if (exponent & 1) ans = (1LL * ans * base) % mod; base = (1LL * base * base) % mod; exponent >>= 1; } return ans % mod; } void compute() { f[0] = 1; rep(i, 1, M - 1) { f[i] = (1LL * i * f[i - 1]) % mod; } inv[M - 1] = expo(f[M - 1], mod - 2); for (ll i = M - 2; i >= 0; i--) { inv[i] = (1LL * (i + 1) * inv[i + 1]) % mod; } } ll C(ll n, ll r) { return (1LL * ((1LL * f[n] * inv[r]) % mod) * inv[n - r]) % mod; } vector<bool> prime(1000002, true); void Sieve() { for (int p = 2; p * p <= 1000001; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { for (int i = p * p; i <= 1000001; i += p) prime[i] = false; } } } ll n, k; char matrix[2000][2000]; string func(ll i, ll j) { string ans; ll x = i, y = j; while (x < n || y < n) { if (x == n - 1) { ans.pb(matrix[x][y + 1]); y++; continue; } if (y == n - 1) { ans.pb(matrix[x + 1][y]); x++; continue; } if (matrix[x + 1][y] < matrix[x][y + 1]) { ans.pb(matrix[x + 1][y]); x++; } else if (matrix[x + 1][y] > matrix[x][y + 1]) { ans.pb(matrix[x][y + 1]); y++; } else { ans.pb(matrix[x + 1][y]); string t = min(func(x + 1, y), func(x, y + 1)); ans += t; return ans; } } return ans; } int main() { // #ifndef ONLINE_JUDGE // // for getting input from input.txt // freopen("alchemy_input.txt", "r", stdin); // // for writing output to output.txt // freopen("output.txt", "w", stdout); // #endif ios_base::sync_with_stdio(false); cin.tie(NULL); // compute(); ll tests = 1, t = 1; // cin>>tests; while (tests--) { ll i, j, n, k; cin >> n >> k; vector<ll> h(n); rep(i, 0, n - 1) cin >> h[i]; ll dp[n]; dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (i = 2; i < n; i++) { dp[i] = (dp[i - 1] + abs(h[i] - h[i - 1])); for (j = 2; j <= k; j++) { dp[i] = min(dp[i], dp[i - j] + abs(h[i - j] - h[i])); } } cout << dp[n - 1]; } return 0; }
#include <bits/stdc++.h> #define ll long long #define db double #define pb push_back #define rep(i, a, b) for (ll i = a; i <= b; i++) #define all(a) a.begin(), a.end() using namespace std; ll mod = 1e9 + 7; const ll M = 500006; ll f[M], inv[M]; ll expo(ll base, ll exponent) { ll ans = 1; while (exponent != 0) { if (exponent & 1) ans = (1LL * ans * base) % mod; base = (1LL * base * base) % mod; exponent >>= 1; } return ans % mod; } void compute() { f[0] = 1; rep(i, 1, M - 1) { f[i] = (1LL * i * f[i - 1]) % mod; } inv[M - 1] = expo(f[M - 1], mod - 2); for (ll i = M - 2; i >= 0; i--) { inv[i] = (1LL * (i + 1) * inv[i + 1]) % mod; } } ll C(ll n, ll r) { return (1LL * ((1LL * f[n] * inv[r]) % mod) * inv[n - r]) % mod; } vector<bool> prime(1000002, true); void Sieve() { for (int p = 2; p * p <= 1000001; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { for (int i = p * p; i <= 1000001; i += p) prime[i] = false; } } } ll n, k; char matrix[2000][2000]; string func(ll i, ll j) { string ans; ll x = i, y = j; while (x < n || y < n) { if (x == n - 1) { ans.pb(matrix[x][y + 1]); y++; continue; } if (y == n - 1) { ans.pb(matrix[x + 1][y]); x++; continue; } if (matrix[x + 1][y] < matrix[x][y + 1]) { ans.pb(matrix[x + 1][y]); x++; } else if (matrix[x + 1][y] > matrix[x][y + 1]) { ans.pb(matrix[x][y + 1]); y++; } else { ans.pb(matrix[x + 1][y]); string t = min(func(x + 1, y), func(x, y + 1)); ans += t; return ans; } } return ans; } int main() { // #ifndef ONLINE_JUDGE // // for getting input from input.txt // freopen("alchemy_input.txt", "r", stdin); // // for writing output to output.txt // freopen("output.txt", "w", stdout); // #endif ios_base::sync_with_stdio(false); cin.tie(NULL); // compute(); ll tests = 1, t = 1; // cin>>tests; while (tests--) { ll i, j, n, k; cin >> n >> k; vector<ll> h(n); rep(i, 0, n - 1) cin >> h[i]; ll dp[n]; dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (i = 2; i < n; i++) { dp[i] = (dp[i - 1] + abs(h[i] - h[i - 1])); for (j = 2; j <= k; j++) { if (i - j < 0) break; dp[i] = min(dp[i], dp[i - j] + abs(h[i - j] - h[i])); } } cout << dp[n - 1]; } return 0; }
insert
101
101
101
103
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[n]; long long int dp[n]; for (int i = 0; i < n; i++) { cin >> a[i]; dp[i] = 1000000000000000; } dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (int i = 2; i < k; i++) { for (int j = 0; j < i; j++) { dp[i] = min(dp[j] + abs(a[i] - a[j]), dp[i]); } } for (int i = k; i < n; i++) { for (int j = i - k; j < i; j++) { dp[i] = min(dp[j] + abs(a[i] - a[j]), dp[i]); } } cout << dp[n - 1]; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[400000]; long long int dp[n]; for (int i = 0; i < n; i++) { cin >> a[i]; dp[i] = 1000000000000000; } dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (int i = 2; i < k; i++) { for (int j = 0; j < i; j++) { dp[i] = min(dp[j] + abs(a[i] - a[j]), dp[i]); } } for (int i = k; i < n; i++) { for (int j = i - k; j < i; j++) { dp[i] = min(dp[j] + abs(a[i] - a[j]), dp[i]); } } cout << dp[n - 1]; }
replace
5
6
5
6
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; int dp[n]; dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (int i = 2; i < n; i++) { dp[i] = dp[i - 1] + abs(v[i] - v[i - 1]); for (int j = 2; j <= k; j++) { dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j])); } } // dp[i]=min(dp[i-2]+abs(v[i]-v[i-2]),dp[i-1]+abs(v[i]-v[i-1])); cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; int dp[n]; dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (int i = 2; i < n; i++) { dp[i] = dp[i - 1] + abs(v[i] - v[i - 1]); for (int j = 2; j <= k && i - j >= 0; j++) { dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j])); } } // dp[i]=min(dp[i-2]+abs(v[i]-v[i-2]),dp[i-1]+abs(v[i]-v[i-1])); cout << dp[n - 1] << endl; return 0; }
replace
14
15
14
15
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FASTIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define ll long long int #define ld long double #define vll vector<ll> #define pl pair<ll, ll> #define vl(p) vector<p> #define W while #define For(i, s, x) for (i = s; i < x; i++) #define Fore(i, s, x) for (i = s; i <= x; i++) #define FOR(i, x) for (i = 0; i < x; i++) #define Rof(i, s, x) for (i = s; i >= x; i--) #define all(v) v.begin(), v.end() #define it(r, v) for (auto r = v.begin(); r != v.end(); r++) #define pb push_back #define in insert #define sz size() #define F first #define S second #define nl cout << "\n" #define pr(a) cout << a << " " #define pr2(a, b) cout << a << " " << b << "\n" #define pr3(a, b, c) cout << a << " " << b << " " << c << "\n" #define mod 1000000007 int main() { FASTIO; ll t = 1, i, j, k, l; // cin>>t; W(t--) { ll n, k; cin >> n >> k; ll a[n], res[n], temp[k]; FOR(i, n) cin >> a[i]; res[0] = 0; Fore(i, 1, k) res[i] = abs(a[i] - a[0]); For(i, k + 1, n) { FOR(j, k) temp[j] = abs(a[i] - a[i - j - 1]) + res[i - j - 1]; res[i] = *min_element(temp, temp + k); } pr(res[n - 1]); nl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FASTIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define ll long long int #define ld long double #define vll vector<ll> #define pl pair<ll, ll> #define vl(p) vector<p> #define W while #define For(i, s, x) for (i = s; i < x; i++) #define Fore(i, s, x) for (i = s; i <= x; i++) #define FOR(i, x) for (i = 0; i < x; i++) #define Rof(i, s, x) for (i = s; i >= x; i--) #define all(v) v.begin(), v.end() #define it(r, v) for (auto r = v.begin(); r != v.end(); r++) #define pb push_back #define in insert #define sz size() #define F first #define S second #define nl cout << "\n" #define pr(a) cout << a << " " #define pr2(a, b) cout << a << " " << b << "\n" #define pr3(a, b, c) cout << a << " " << b << " " << c << "\n" #define mod 1000000007 int main() { FASTIO; ll t = 1, i, j, k, l; // cin>>t; W(t--) { ll n, k; cin >> n >> k; ll a[n], res[n], temp[k]; FOR(i, n) cin >> a[i]; if (k >= n - 1) { pr(a[n - 1] - a[0]); continue; } Fore(i, 1, k) res[i] = abs(a[i] - a[0]); For(i, k + 1, n) { FOR(j, k) temp[j] = abs(a[i] - a[i - j - 1]) + res[i - j - 1]; res[i] = *min_element(temp, temp + k); } pr(res[n - 1]); nl; } return 0; }
replace
39
40
39
43
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int dp[n]; dp[0] = 0; for (int i = 1; i < k; i++) { int m = INT_MAX; for (int j = 0; j < i; j++) { m = min(dp[j] + abs(a[j] - a[i]), m); } dp[i] = m; } for (int i = k; i < n; i++) { int m = INT_MAX; for (int j = i - 1; i - j <= k && j >= 0; j--) { m = min(dp[j] + abs(a[j] - a[i]), m); } dp[i] = m; } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int dp[n]; dp[0] = 0; for (int i = 1; i < k && i < n; i++) { int m = INT_MAX; for (int j = 0; j < i; j++) { m = min(dp[j] + abs(a[j] - a[i]), m); } dp[i] = m; } for (int i = k; i < n; i++) { int m = INT_MAX; for (int j = i - 1; i - j <= k && j >= 0; j--) { m = min(dp[j] + abs(a[j] - a[i]), m); } dp[i] = m; } cout << dp[n - 1]; return 0; }
replace
13
14
13
14
0
p03161
C++
Runtime Error
// Frog-1 #include <bits/stdc++.h> #define ll long long #define ff first #define ss second #define pb push_back #define N (ll)(1e7 + 5) #define mod (ll)(1e9 + 7) using namespace std; int main() { ll n, k; cin >> n >> k; vector<ll> dp(n), v(n); for (auto &x : v) cin >> x; dp[1] = abs(v[1] - v[0]); for (ll i = 2; i < n; i++) { dp[i] = abs(v[i] - v[i - 1]) + dp[i - 1]; for (ll j = 2; j <= k; j++) dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j])); } cout << dp[n - 1] << endl; return 0; }
// Frog-1 #include <bits/stdc++.h> #define ll long long #define ff first #define ss second #define pb push_back #define N (ll)(1e7 + 5) #define mod (ll)(1e9 + 7) using namespace std; int main() { ll n, k; cin >> n >> k; vector<ll> dp(n), v(n); for (auto &x : v) cin >> x; dp[1] = abs(v[1] - v[0]); for (ll i = 2; i < n; i++) { dp[i] = abs(v[i] - v[i - 1]) + dp[i - 1]; for (ll j = 2; j <= min(k, i); j++) dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j])); } cout << dp[n - 1] << endl; return 0; }
replace
21
22
21
22
0
p03161
C++
Runtime Error
/* ########################### # Author : Pranay Garg # # College : SGSITS # ########################### */ #include <bits/stdc++.h> #define ll long long int #define ironman \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define MOD 1000000007 #define endl '\n' using namespace std; void fastio() { ironman #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif } ll parent[100005]; ll find(ll a) { std::vector<ll> v; while (parent[a] > 0) { v.push_back(a); a = parent[a]; } for (ll i = 0; i < v.size(); i++) parent[v[i]] = a; return a; } bool prime[100005]; void SieveOfEratosthenes(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } void union1(ll a, ll b) { a = find(a); b = find(b); if (a != b) { parent[a] = min(parent[a], parent[b]); parent[b] = a; } } ll n, k; ll arr[100002]; ll dp[100002]; int main() { memset(dp, -1, sizeof(dp)); fastio(); // ll n; cin >> n >> k; // ll arr[n]; for (ll i = 0; i < n; i++) cin >> arr[i]; dp[0] = 0; for (ll i = 1; i < n; i++) { if (i - 1 >= 0) dp[i] = dp[i - 1] + abs(arr[i] - arr[i - 1]); for (ll j = i - 2; j >= 0 && i - j <= k; j--) { dp[i] = min(dp[i], dp[j] + abs(arr[i] - arr[j])); } } cout << dp[n - 1] << endl; }
/* ########################### # Author : Pranay Garg # # College : SGSITS # ########################### */ #include <bits/stdc++.h> #define ll long long int #define ironman \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define MOD 1000000007 #define endl '\n' using namespace std; void fastio() { ironman #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif } ll parent[100005]; ll find(ll a) { std::vector<ll> v; while (parent[a] > 0) { v.push_back(a); a = parent[a]; } for (ll i = 0; i < v.size(); i++) parent[v[i]] = a; return a; } bool prime[100005]; void SieveOfEratosthenes(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } void union1(ll a, ll b) { a = find(a); b = find(b); if (a != b) { parent[a] = min(parent[a], parent[b]); parent[b] = a; } } ll n, k; ll arr[100002]; ll dp[100002]; int main() { memset(dp, -1, sizeof(dp)); // fastio(); // ll n; cin >> n >> k; // ll arr[n]; for (ll i = 0; i < n; i++) cin >> arr[i]; dp[0] = 0; for (ll i = 1; i < n; i++) { if (i - 1 >= 0) dp[i] = dp[i - 1] + abs(arr[i] - arr[i - 1]); for (ll j = i - 2; j >= 0 && i - j <= k; j--) { dp[i] = min(dp[i], dp[j] + abs(arr[i] - arr[j])); } } cout << dp[n - 1] << endl; }
replace
56
57
56
57
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define deb(x) cout << #x << "=" << x << endl; #define fo(i, n) for (ll i = 0; i < n; i++) #define fo1(i, n) \ for (ll i = 1; i <= n; i++) \ i #define sn(x) cin >> x; #define pn(x) cout << x << "\n" #define ps(x) cout << x << " "; #define pb() cout << "\n"; #define sort(x) sort(x.begin(), x.end()) using namespace std; typedef long long ll; ll INF = INT_MAX; void solve() { ll n, jumps; cin >> n >> jumps; vector<ll> v(n), dp(n); fo(i, n) { cin >> v[i]; dp[i] = INF; } dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (ll i = 2; i < n; i++) { for (ll j = 1; j <= jumps; j++) { dp[i] = min(dp[i], abs(v[i - j] - v[i]) + dp[i - j]); } } cout << dp[n - 1] << endl; } int main() { #ifndef ONLINE_JUDGE freopen("a.in", "r", stdin); freopen("err.txt", "w", stderr); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll test = 1; // cin >> test; while (test--) { solve(); } }
#include <bits/stdc++.h> #define deb(x) cout << #x << "=" << x << endl; #define fo(i, n) for (ll i = 0; i < n; i++) #define fo1(i, n) \ for (ll i = 1; i <= n; i++) \ i #define sn(x) cin >> x; #define pn(x) cout << x << "\n" #define ps(x) cout << x << " "; #define pb() cout << "\n"; #define sort(x) sort(x.begin(), x.end()) using namespace std; typedef long long ll; ll INF = INT_MAX; void solve() { ll n, jumps; cin >> n >> jumps; vector<ll> v(n), dp(n); fo(i, n) { cin >> v[i]; dp[i] = INF; } dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (ll i = 2; i < n; i++) { for (ll j = 1; j <= jumps; j++) { if (j <= i) dp[i] = min(dp[i], abs(v[i - j] - v[i]) + dp[i - j]); } } cout << dp[n - 1] << endl; } int main() { #ifndef ONLINE_JUDGE freopen("a.in", "r", stdin); freopen("err.txt", "w", stderr); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll test = 1; // cin >> test; while (test--) { solve(); } }
replace
34
35
34
36
-6
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, x, y) for (int i = x; i < y; i++) #define rel(i, x, y) for (int i = x - 1; i >= y; i--) #define all(x) x.begin(), x.end() ll inf = 1e18 + 7; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; ll h[n]; rep(i, 0, n) cin >> h[i]; ll dp[100010]; rep(i, 0, 100010) dp[i] = inf; dp[0] = 0; rep(i, 0, n) { rep(j, 1, k + 1) { dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i, x, y) for (int i = x; i < y; i++) #define rel(i, x, y) for (int i = x - 1; i >= y; i--) #define all(x) x.begin(), x.end() ll inf = 1e18 + 7; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; ll h[n]; rep(i, 0, n) cin >> h[i]; ll dp[100010]; rep(i, 0, 100010) dp[i] = inf; dp[0] = 0; rep(i, 0, n) { rep(j, 1, k + 1) { if (i + j >= n) continue; dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } } cout << dp[n - 1] << endl; }
insert
20
20
20
22
0
p03161
C++
Time Limit Exceeded
// dp_b.cc #include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; int main() { int n, k; scanf("%d%d", &n, &k); vector<int> h(n); for (int &x : h) scanf("%d", &x); vector<int> dp(n, INF); dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = 1; j <= i + k; j++) { if (j < n) { dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j])); } } } printf("%d\n", dp.back()); }
// dp_b.cc #include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; int main() { int n, k; scanf("%d%d", &n, &k); vector<int> h(n); for (int &x : h) scanf("%d", &x); 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])); } } } printf("%d\n", dp.back()); }
replace
18
19
18
19
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, *p, *dp; cin >> n; cin >> k; p = new long long[n]; for (long long y = 0; y < n; y++) { cin >> p[y]; } dp = new long long[n]; dp[0] = 0; dp[1] = abs(p[1] - p[0]); for (long long i = 2; i < n; i++) { long long minimo = dp[i - 1] + abs(p[i] - p[i - 1]); for (long long t = 1; t < min(k, n - 1); t++) { minimo = min(minimo, dp[i - (t + 1)] + abs(p[i] - p[i - (t + 1)])); } dp[i] = minimo; } cout << dp[n - 1]; delete[] p; delete[] dp; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, *p, *dp; cin >> n; cin >> k; p = new long long[n]; for (long long y = 0; y < n; y++) { cin >> p[y]; } dp = new long long[n]; dp[0] = 0; dp[1] = abs(p[1] - p[0]); for (long long i = 2; i < n; i++) { long long minimo = dp[i - 1] + abs(p[i] - p[i - 1]); for (long long t = 1; t < min(i, k); t++) { minimo = min(minimo, dp[i - (t + 1)] + abs(p[i] - p[i - (t + 1)])); } dp[i] = minimo; } cout << dp[n - 1]; delete[] p; delete[] dp; return 0; }
replace
16
17
16
17
0
p03161
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; #define rep(i, a) for (int i = 0; i < a; i++) #define pd(a, n) \ for (int i = 0; i < n; i++) \ cout << a[i] << ((i == n - 1) ? "\n" : " ") #define pdd(a, n, m) \ for (int i = 0; i < m; i++) \ pd(a, m) #define mp make_pair #define pb push_back #define ll __int64 #define __int64 long long #define P pair<int, int> #define PP pair<P, int> int n, k; int h[11111]; int dp[111111]; const int INF = 1111111111; int main() { cin >> n >> k; rep(i, n) cin >> h[i]; rep(i, n) dp[i] = INF; dp[0] = 0; rep(i, n) { for (int j = 1; j <= k; j++) dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i])); } cout << dp[n - 1] << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; #define rep(i, a) for (int i = 0; i < a; i++) #define pd(a, n) \ for (int i = 0; i < n; i++) \ cout << a[i] << ((i == n - 1) ? "\n" : " ") #define pdd(a, n, m) \ for (int i = 0; i < m; i++) \ pd(a, m) #define mp make_pair #define pb push_back #define ll __int64 #define __int64 long long #define P pair<int, int> #define PP pair<P, int> int n, k; int h[111111]; int dp[111111]; const int INF = 1111111111; int main() { cin >> n >> k; rep(i, n) cin >> h[i]; rep(i, n) dp[i] = INF; dp[0] = 0; rep(i, n) { for (int j = 1; j <= k; j++) dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i])); } cout << dp[n - 1] << endl; return 0; }
replace
29
30
29
30
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 pos = a[0]; dp[0] = 0; dp[1] = abs(a[0] - a[1]); for (int i = 2; i < n; i++) { dp[i] = dp[i - 1] + abs(a[i - 1] - a[i]); for (int j = 2; j < min(i, k); j++) { dp[i] = min(dp[i], dp[i - k] + abs(a[i - k] - a[i])); } } cout << dp[n - 1] << endl; }
#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 pos = a[0]; dp[0] = 0; dp[1] = abs(a[0] - a[1]); for (int i = 2; i < n; i++) { dp[i] = dp[i - 1] + abs(a[i - 1] - a[i]); for (int j = 2; j <= min(i, k); j++) { dp[i] = min(dp[i], dp[i - j] + abs(a[i - j] - a[i])); } } cout << dp[n - 1] << endl; }
replace
18
20
18
20
0
p03161
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> #include <vector> typedef long long ll; const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const int dr[4] = {-1, 0, 1, 0}; const int dc[4] = {0, 1, 0, -1}; const int INF = 1e9; #define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define SORT(a) sort(a.begin(), a.end()) #define REVERSE(a) reverse(a.begin(), a.end()) int guki(int a) { if (a % 2 == 0) return 0; else return 1; } int gcd(int a, int b) { if (a % b == 0) { return b; } else { return (gcd(b, a % b)); } } int lcm(int a, int b) { int x = gcd(a, b); return (a * b / x); } ll nCr(ll n, ll r) { ll ans = 1; for (ll i = n; i > n - r; i--) { ans *= i; } for (ll i = 1; i <= r; i++) { ans /= i; } return ans; } int nPr(int n, int r) { int ans = 1; for (int i = n; i > n - r; i--) { ans *= i; } return ans; } using namespace std; int main() { int n, k; cin >> n >> k; vector<int> vec(n), dp(n); REP(i, n) { cin >> vec[i]; } dp[0] = 0; dp[1] = abs(vec[0] - vec[1]); for (int i = 2; i < n; i++) { dp[i] = dp[i - 1] + abs(vec[i] - vec[i - 1]); for (int j = 2; 0 <= k - j; j++) { dp[i] = min(dp[i], (dp[i - j] + abs(vec[i] - vec[i - j]))); } } cout << dp[n - 1] << endl; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> #include <vector> typedef long long ll; const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const int dr[4] = {-1, 0, 1, 0}; const int dc[4] = {0, 1, 0, -1}; const int INF = 1e9; #define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define SORT(a) sort(a.begin(), a.end()) #define REVERSE(a) reverse(a.begin(), a.end()) int guki(int a) { if (a % 2 == 0) return 0; else return 1; } int gcd(int a, int b) { if (a % b == 0) { return b; } else { return (gcd(b, a % b)); } } int lcm(int a, int b) { int x = gcd(a, b); return (a * b / x); } ll nCr(ll n, ll r) { ll ans = 1; for (ll i = n; i > n - r; i--) { ans *= i; } for (ll i = 1; i <= r; i++) { ans /= i; } return ans; } int nPr(int n, int r) { int ans = 1; for (int i = n; i > n - r; i--) { ans *= i; } return ans; } using namespace std; int main() { int n, k; cin >> n >> k; vector<int> vec(n), dp(n); REP(i, n) { cin >> vec[i]; } dp[0] = 0; dp[1] = abs(vec[0] - vec[1]); for (int i = 2; i < n; i++) { dp[i] = dp[i - 1] + abs(vec[i] - vec[i - 1]); for (int j = 2; j <= k; j++) { if (i - j >= 0) dp[i] = min(dp[i], (dp[i - j] + abs(vec[i] - vec[i - j]))); } } cout << dp[n - 1] << endl; }
replace
71
73
71
74
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[n]; int dp[n]; for (int i = 0; i < n; i++) { dp[i] = INT_MAX; } for (int i = 0; i < n; i++) { cin >> a[i]; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = 1; j <= k; j++) { dp[i + j] = min(dp[i + j], dp[i] + abs(a[i + j] - a[i])); } } cout << dp[n - 1] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[n]; int dp[n]; for (int i = 0; i < n; i++) { dp[i] = INT_MAX; } for (int i = 0; i < n; i++) { cin >> a[i]; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = 1; j <= k; j++) { if (i + j < n) dp[i + j] = min(dp[i + j], dp[i] + abs(a[i + j] - a[i])); } } cout << dp[n - 1] << "\n"; return 0; }
replace
17
18
17
19
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define for0(i, n) for (long long i = 0; i < n; i++) #define for1(i, n) for (long long i = 1; i <= n; i++) #define forc(i, l, r) for (long long i = l; i <= r; ++i) #define forr0(i, n) for (long long i = n - 1; i >= 0; i--) #define forr1(i, n) for (long long i = n; i >= 1; i--) #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin, (x).rend() using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef vector<ii> vii; typedef long long ll; typedef long long int lli; typedef vector<ll> vll; typedef vector<vll> vvll; typedef double ld; const ll N = 1e5 + 20; const ll mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll i, j, k, n; cin >> n >> k; ll a[N], dp[N]; for0(i, n) cin >> a[i]; for0(i, N) dp[i] = 1e15; dp[0] = 0; for (i = 0; i < n; i++) { for (j = i + 1; j <= i + k; j++) { dp[j] = min(dp[j], dp[i] + (ll)abs(a[j] - a[i])); } } // for0(i,n) cout<<dp[i]<<" "; cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> #define for0(i, n) for (long long i = 0; i < n; i++) #define for1(i, n) for (long long i = 1; i <= n; i++) #define forc(i, l, r) for (long long i = l; i <= r; ++i) #define forr0(i, n) for (long long i = n - 1; i >= 0; i--) #define forr1(i, n) for (long long i = n; i >= 1; i--) #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin, (x).rend() using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef vector<ii> vii; typedef long long ll; typedef long long int lli; typedef vector<ll> vll; typedef vector<vll> vvll; typedef double ld; const ll N = 1e5 + 200; const ll mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll i, j, k, n; cin >> n >> k; ll a[N], dp[N]; for0(i, n) cin >> a[i]; for0(i, N) dp[i] = 1e15; dp[0] = 0; for (i = 0; i < n; i++) { for (j = i + 1; j <= i + k; j++) { dp[j] = min(dp[j], dp[i] + (ll)abs(a[j] - a[i])); } } // for0(i,n) cout<<dp[i]<<" "; cout << dp[n - 1] << endl; return 0; }
replace
27
28
27
28
0
p03161
C++
Runtime Error
// INCLUDE //------------------------------------------ #include <algorithm> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <math.h> #include <string> #include <vector> // DEFINE //------------------------------------------ #define ll long long #define ALLv(a) (a).begin(), (a).end() #define ALL(a, n) (a), (a) + n #define vi vector<long long> #define vd vector<long double> #define vs vector<string> // CONST //------------------------------------------ #define INF 1010000000000000017LL #define MOD 1000000007LL #define EPS 1e-12 #define PI 3.14159265358979323846 // REPEAT //------------------------------------------ #define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++) #define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++) #define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--) #define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--) #define WREP(i, in, j, jn) REP(i, in) REP(j, jn) using namespace std; //------------------------------------------- int main(void) { ll N, K; cin >> N >> K; ll h[N + 1]; REPS(i, N) cin >> h[i]; vi dp(N * 2 + 1, INF); dp[1] = 0; REPS(i, N) { REPS(k, K) dp[i + k] = min(dp[i + k], dp[i] + labs(h[i + k] - h[i])); } cout << dp[N] << endl; return 0; }
// INCLUDE //------------------------------------------ #include <algorithm> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <math.h> #include <string> #include <vector> // DEFINE //------------------------------------------ #define ll long long #define ALLv(a) (a).begin(), (a).end() #define ALL(a, n) (a), (a) + n #define vi vector<long long> #define vd vector<long double> #define vs vector<string> // CONST //------------------------------------------ #define INF 1010000000000000017LL #define MOD 1000000007LL #define EPS 1e-12 #define PI 3.14159265358979323846 // REPEAT //------------------------------------------ #define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++) #define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++) #define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--) #define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--) #define WREP(i, in, j, jn) REP(i, in) REP(j, jn) using namespace std; //------------------------------------------- int main(void) { ll N, K; cin >> N >> K; ll h[N + 1]; REPS(i, N) cin >> h[i]; vi dp(N * 2 + 2, INF); dp[1] = 0; REPS(i, N) { REPS(k, K) dp[i + k] = min(dp[i + k], dp[i] + labs(h[i + k] - h[i])); } cout << dp[N] << endl; return 0; }
replace
40
41
40
41
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define ll long long #define ld long double #define hell 998244353 #define hell1 1000000006 #define pb push_back #define x first #define y second #define MAXL 100005 #define pll pair<ll, ll> #define pii pair<int, int> #define L LLONG_MAX #define all(v) (v).begin(), (v).end() #define tcFile \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); using namespace std; // vector<int>prime(15000005,0); ll mM(ll a, ll b, ll p = hell) { ll res = 0; if (a >= p) a %= p; while (b) { if (b & 1ll) res = (res + a); if (res >= p) res %= p; a = (2 * a); if (a >= p) a %= p; b >>= 1ll; } return res; } ll fpow(ll x, ll y, ll p = hell) { if (x >= p) x = x % p; ll res = 1; while (y) { if (y & 1ll) res *= x; if (res < 0) res += p; if (res >= p) res %= p; y = y >> 1ll; x *= x; if (x < 0) x += p; if (x >= p) x %= p; } return res; } ll inv(ll n, ll p = hell) { return fpow(n, p - 2, p); } ll modmul(ll x, ll y, ll p = hell) { if (x < 0) x += p; if (x >= p) x %= p; if (y < 0) y += p; if (y >= p) y %= p; return (x * y) % p; } ll modadd(ll x, ll y, ll p = hell) { if (x < 0) x += p; if (x >= p) x %= p; if (y < 0) y += p; if (y >= p) y %= p; ll temp = x + y; if (temp >= p) temp %= p; return temp; } /*void sieve(){ prime[0] = -1; prime[1] = 0; for(int i=2;i<=15000000;i++) if(!prime[i]) for(int j=i;j<=15000000;j+=i) if(!prime[j]) prime[j] = i; }*/ int main() { ios int t = 1, tc = 0; // cin >> t; while (t--) { int n, k; cin >> n >> k; vector<int> a(n), dp(n, INT_MAX); for (int i = 0; i < n; i++) cin >> a[i]; dp[0] = 0; for (int i = 1; i < n; i++) for (int j = 1; j <= k; j++) dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j])); cout << dp[n - 1] << "\n"; } }
#include <bits/stdc++.h> #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define ll long long #define ld long double #define hell 998244353 #define hell1 1000000006 #define pb push_back #define x first #define y second #define MAXL 100005 #define pll pair<ll, ll> #define pii pair<int, int> #define L LLONG_MAX #define all(v) (v).begin(), (v).end() #define tcFile \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); using namespace std; // vector<int>prime(15000005,0); ll mM(ll a, ll b, ll p = hell) { ll res = 0; if (a >= p) a %= p; while (b) { if (b & 1ll) res = (res + a); if (res >= p) res %= p; a = (2 * a); if (a >= p) a %= p; b >>= 1ll; } return res; } ll fpow(ll x, ll y, ll p = hell) { if (x >= p) x = x % p; ll res = 1; while (y) { if (y & 1ll) res *= x; if (res < 0) res += p; if (res >= p) res %= p; y = y >> 1ll; x *= x; if (x < 0) x += p; if (x >= p) x %= p; } return res; } ll inv(ll n, ll p = hell) { return fpow(n, p - 2, p); } ll modmul(ll x, ll y, ll p = hell) { if (x < 0) x += p; if (x >= p) x %= p; if (y < 0) y += p; if (y >= p) y %= p; return (x * y) % p; } ll modadd(ll x, ll y, ll p = hell) { if (x < 0) x += p; if (x >= p) x %= p; if (y < 0) y += p; if (y >= p) y %= p; ll temp = x + y; if (temp >= p) temp %= p; return temp; } /*void sieve(){ prime[0] = -1; prime[1] = 0; for(int i=2;i<=15000000;i++) if(!prime[i]) for(int j=i;j<=15000000;j+=i) if(!prime[j]) prime[j] = i; }*/ int main() { ios int t = 1, tc = 0; // cin >> t; while (t--) { int n, k; cin >> n >> k; vector<int> a(n), dp(n, INT_MAX); for (int i = 0; i < n; i++) cin >> a[i]; dp[0] = 0; for (int i = 1; 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 - 1] << "\n"; } }
replace
105
106
105
107
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; int main() { int n, k; cin >> n >> k; vector<int> stone(n); for (int i = 0; i < n; i++) cin >> stone[i]; vector<int> dp(n, INF); dp[0] = 0; for (int i = 0; i < n; i++) for (int j = i + 1; j <= i + k; j++) dp[j] = min(dp[j], dp[i] + abs(stone[j] - stone[i])); cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; int main() { int n, k; cin >> n >> k; vector<int> stone(n); for (int i = 0; i < n; i++) cin >> stone[i]; 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(stone[j] - stone[i])); cout << dp[n - 1]; return 0; }
replace
16
17
16
18
-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 <iostream> using namespace std; int mod(int x) { if (x > 0) return x; return -x; } int main() { int n, k; cin >> n >> k; int h[n], result[n]; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = 0; i <= k; i++) result[i] = mod(h[i] - h[0]); for (int i = k + 1; i < n; i++) { int min = INT_MAX; for (int j = i - k; j < i; j++) { int flag = result[j] + mod(h[i] - h[j]); if (min > flag) min = flag; } result[i] = min; } cout << result[n - 1]; return 0; }
#include <bits/stdc++.h> #include <iostream> using namespace std; int mod(int x) { if (x > 0) return x; return -x; } int main() { int n, k; cin >> n >> k; int h[n], result[n]; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = 0; i <= k && i < n; i++) result[i] = mod(h[i] - h[0]); for (int i = k + 1; i < n; i++) { int min = INT_MAX; for (int j = i - k; j < i; j++) { int flag = result[j] + mod(h[i] - h[j]); if (min > flag) min = flag; } result[i] = min; } cout << result[n - 1]; return 0; }
replace
14
15
14
15
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (long long int i = 0; i < n; ++i) typedef long long int ll; int main() { int n, k; cin >> n >> k; vector<int> h(n); rep(i, n) cin >> h[i]; vector<int> c(n, 1e+9); c[0] = 0; c[1] = abs(h[0] - h[1]); for (int i = 2; i < n; i++) { for (int j = i - 1; j >= i - k && i - j >= 0; j--) { c[i] = min(c[i], c[j] + abs(h[i] - h[j])); } } cout << c[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (long long int i = 0; i < n; ++i) typedef long long int ll; int main() { int n, k; cin >> n >> k; vector<int> h(n); rep(i, n) cin >> h[i]; vector<int> c(n, 1e+9); c[0] = 0; c[1] = abs(h[0] - h[1]); for (int i = 2; i < n; i++) { for (int j = i - 1; j >= i - k && j >= 0; j--) { c[i] = min(c[i], c[j] + abs(h[i] - h[j])); } } cout << c[n - 1] << endl; return 0; }
replace
20
21
20
21
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vp vector<pii> #define vs vector<string> #define all(v) v.begin(), v.end() #define lb lower_bound #define ub upper_bound #define bs binary_search #define ff first #define ss second #define sz(x) ((int)(x).size()) #define rep(i, l, r) for (int i = (l); i < (r); ++i) #define gcd(a, b) __gcd((a), (b)) #define lcm(a, b) ((a) * (b)) / gcd((a), (b)) #define pi 3.14159265358979323846 // 20 decimals #define MOD 1000000007 int32_t main() { IOS #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n, k; cin >> n >> k; vi h(n); for (int &x : h) { cin >> x; } vi dp(n, INT_MAX); 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])); } } } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vp vector<pii> #define vs vector<string> #define all(v) v.begin(), v.end() #define lb lower_bound #define ub upper_bound #define bs binary_search #define ff first #define ss second #define sz(x) ((int)(x).size()) #define rep(i, l, r) for (int i = (l); i < (r); ++i) #define gcd(a, b) __gcd((a), (b)) #define lcm(a, b) ((a) * (b)) / gcd((a), (b)) #define pi 3.14159265358979323846 // 20 decimals #define MOD 1000000007 int32_t main() { IOS // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int n, k; cin >> n >> k; vi h(n); for (int &x : h) { cin >> x; } vi dp(n, INT_MAX); 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])); } } } cout << dp[n - 1]; return 0; }
replace
34
39
34
40
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n; cin >> k; const int INF = 1e9 + 5; vector<int> dp(n + 1, INF); int H[n + 1]; for (int i = 1; i < n + 1; i++) { cin >> H[i]; } dp[1] = 0; dp[2] = abs(H[1] - H[2]); for (int i = 1; i < n + 1; i++) { for (int j = 1; j <= i + k; j++) { if (j < n + 1) { dp[j] = min(dp[j], dp[i] + abs(H[i] - H[j])); } } } cout << dp[n] << endl; } int main() { solve(); }
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n; cin >> k; const int INF = 1e9 + 5; vector<int> dp(n + 1, INF); int H[n + 1]; for (int i = 1; i < n + 1; i++) { cin >> H[i]; } dp[1] = 0; dp[2] = abs(H[1] - H[2]); for (int i = 1; i < n + 1; i++) { for (int j = i + 1; j <= i + k; j++) { if (j < n + 1) { dp[j] = min(dp[j], dp[i] + abs(H[i] - H[j])); } } } cout << dp[n] << endl; } int main() { solve(); }
replace
17
18
17
18
TLE
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 dp[n]; memset(dp, -1, sizeof(dp)); dp[0] = 0; for (int i = 1; i <= k; i++) { dp[i] = abs(arr[0] - arr[i]); } for (int i = k + 1; i < n; i++) { int ans = INT_MAX; for (int j = 1; j <= k; j++) { ans = min(ans, abs(arr[i] - arr[i - j]) + dp[i - j]); } dp[i] = ans; } cout << dp[n - 1] << "\n"; }
#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]; } if (k >= n - 1) { cout << abs(arr[0] - arr[n - 1]) << "\n"; return 0; } int dp[n]; memset(dp, -1, sizeof(dp)); dp[0] = 0; for (int i = 1; i <= k; i++) { dp[i] = abs(arr[0] - arr[i]); } for (int i = k + 1; i < n; i++) { int ans = INT_MAX; for (int j = 1; j <= k; j++) { ans = min(ans, abs(arr[i] - arr[i - j]) + dp[i - j]); } dp[i] = ans; } cout << dp[n - 1] << "\n"; }
insert
11
11
11
17
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define For(i, n) for (ll i = 1; i <= n; ++i) int main() { ll n, k; cin >> n >> k; ll h[n + 1], dp[n + 1]; For(i, n) { cin >> h[i]; dp[i] = 0; } dp[2] = abs(h[2] - h[1]); For(i, k - 2) { dp[i + 2] = dp[1] + abs(h[1] - h[i + 2]); For(j, i + 2) if (dp[j] + abs(h[j] - h[i + 2]) < dp[i + 2]) dp[i + 2] = dp[j] + abs(h[j] - h[i + 2]); } For(i, n - k) { dp[i + k] = dp[i] + abs(h[i] - h[i + k]); for (int j = i; j <= i + k; ++j) if (dp[j] + abs(h[j] - h[i + k]) < dp[i + k]) dp[i + k] = dp[j] + abs(h[j] - h[i + k]); } cout << dp[n]; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define For(i, n) for (ll i = 1; i <= n; ++i) int main() { ll n, k; cin >> n >> k; ll h[n + 1], dp[n + 1]; For(i, n) { cin >> h[i]; dp[i] = 0; } dp[2] = abs(h[2] - h[1]); if (k <= n) { For(i, k - 2) { dp[i + 2] = dp[1] + abs(h[1] - h[i + 2]); For(j, i + 2) if (dp[j] + abs(h[j] - h[i + 2]) < dp[i + 2]) dp[i + 2] = dp[j] + abs(h[j] - h[i + 2]); } For(i, n - k) { dp[i + k] = dp[i] + abs(h[i] - h[i + k]); for (int j = i; j <= i + k; ++j) if (dp[j] + abs(h[j] - h[i + k]) < dp[i + k]) dp[i + k] = dp[j] + abs(h[j] - h[i + k]); } } else { For(i, n - 2) { dp[i + 2] = dp[1] + abs(h[1] - h[i + 2]); For(j, i + 2) if (dp[j] + abs(h[j] - h[i + 2]) < dp[i + 2]) dp[i + 2] = dp[j] + abs(h[j] - h[i + 2]); } } cout << dp[n]; return 0; }
replace
14
24
14
32
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { int n, k; cin >> n >> k; int h[n]; for (int i = 0; i < n; i++) cin >> h[i]; ll ans = 0; int dp[n]; // cost dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; i++) dp[i] = 1e9; for (int i = 2; i < n; i++) { for (int j = i - 1; j >= 0; j--) { if (i - j <= k) { int x = abs(h[i] - h[j]) + dp[j]; dp[i] = min(dp[i], x); } } } cout << dp[n - 1] << '\n'; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { int n, k; cin >> n >> k; int h[n]; for (int i = 0; i < n; i++) cin >> h[i]; ll ans = 0; int dp[n]; // cost dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; i++) dp[i] = 1e9; for (int i = 2; i < n; i++) { for (int j = i - 1; j >= 0; j--) { if (i - j <= k) { int x = abs(h[i] - h[j]) + dp[j]; dp[i] = min(dp[i], x); } else break; } } cout << dp[n - 1] << '\n'; return 0; }
replace
20
21
20
22
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define F first #define S second // cout<<fixed<<setprecision(11); const string nl = "\n"; const ll MOD = 1000000007; const ll ARR_MAX = 1e5 + 1; const ll INF = 1e10 + 1; void solve() { int n, k; cin >> n >> k; ll dp[n + 1]; // dp[i]=minimum cost required to reach rock i-1. ll h[n + 1]; // h[0]=0; for (int i = 0; i < n; i++) { cin >> h[i]; dp[i + 1] = INF; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = 1; j <= k && j < n; j++) { dp[i + j] = min(dp[i + j], dp[i] + max(h[i + j] - h[i], h[i] - h[i + j])); } } cout << dp[n - 1] << nl; } int main() { int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define F first #define S second // cout<<fixed<<setprecision(11); const string nl = "\n"; const ll MOD = 1000000007; const ll ARR_MAX = 1e5 + 1; const ll INF = 1e10 + 1; void solve() { int n, k; cin >> n >> k; ll dp[n + 1]; // dp[i]=minimum cost required to reach rock i-1. ll h[n + 1]; // h[0]=0; for (int i = 0; i < n; i++) { cin >> h[i]; dp[i + 1] = INF; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = 1; j <= k && i + j < n; j++) { dp[i + j] = min(dp[i + j], dp[i] + max(h[i + j] - h[i], h[i] - h[i + j])); } } cout << dp[n - 1] << nl; } int main() { int t = 1; // cin>>t; while (t--) { solve(); } return 0; }
replace
31
32
31
32
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define sd(x) scanf("%d", &x) #define slld(x) scanf("%lld", &x) #define all(x) x.begin(), x.end() #define For(i, st, en) for (int i = st; i < en; i++) #define tr(x) for (auto it = x.begin(); it != x.end(); it++) #define fast \ std::ios::sync_with_stdio(false); \ cin.tie(NULL); #define pb push_back #define ll long long #define int long long #define mp make_pair #define F first #define S second typedef pair<int, int> pii; #define MOD 1000000007 #define INF 1000000000000000007 #define MAXN 5206 signed main() { int a, b, c, d, k, t = 1; cin >> t >> k; vector<int> v; For(i, 0, t) { cin >> a; v.pb(a); } int dp[t]; if (t == 2) cout << v[1] - v[0]; else { dp[0] = 0; dp[1] = abs(v[1] - v[0]); For(j, 2, t) { d = INF; For(op, 1, k + 1) d = min(dp[j - op] + abs(v[j] - v[j - op]), d); dp[j] = d; } cout << dp[t - 1]; } }
#include <bits/stdc++.h> using namespace std; #define sd(x) scanf("%d", &x) #define slld(x) scanf("%lld", &x) #define all(x) x.begin(), x.end() #define For(i, st, en) for (int i = st; i < en; i++) #define tr(x) for (auto it = x.begin(); it != x.end(); it++) #define fast \ std::ios::sync_with_stdio(false); \ cin.tie(NULL); #define pb push_back #define ll long long #define int long long #define mp make_pair #define F first #define S second typedef pair<int, int> pii; #define MOD 1000000007 #define INF 1000000000000000007 #define MAXN 5206 signed main() { int a, b, c, d, k, t = 1; cin >> t >> k; vector<int> v; For(i, 0, t) { cin >> a; v.pb(a); } int dp[t]; if (t == 2) cout << v[1] - v[0]; else { dp[0] = 0; dp[1] = abs(v[1] - v[0]); For(j, 2, t) { d = INF; For(op, 1, k + 1) { if (j - op < 0) break; d = min(dp[j - op] + abs(v[j] - v[j - op]), d); } dp[j] = d; } cout << dp[t - 1]; } }
replace
37
38
37
42
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using vb = vector<bool>; using vvb = vector<vb>; using vd = vector<double>; using vvd = vector<vd>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using pll = pair<ll, ll>; using tll = tuple<ll, ll>; using tlll = tuple<ll, ll, ll>; using vs = vector<string>; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define rep(i, n) range(i, 0, n) #define rrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define range(i, a, n) for (ll i = (a); i < (n); i++) #define LINF ((ll)1ll << 60) #define INF ((int)1 << 30) #define EPS (1e-9) #define MOD (1000000007ll) #define fcout(a) cout << setprecision(a) << fixed #define fs first #define sc second #define PI (3.1415926535897932384) int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class S> S sumvec(vector<S> &a) { return accumulate(all(a), S()); } template <class S> S maxvec(vector<S> &a) { return *max_element(all(a)); } template <class S> S minvec(vector<S> &a) { return *min_element(all(a)); } ll max(int a, ll b) { return max((ll)a, b); } ll max(ll a, int b) { return max(a, (ll)b); } int sgn(const double &r) { return (r > EPS) - (r < -EPS); } // a>0 : sgn(a)>0 int sgn(const double &a, const double &b) { return sgn(a - b); } // b<=c : sgn(b,c)<=0 template <class T> void puta(T &&t) { cout << t << "\n"; } template <class H, class... T> void puta(H &&h, T &&...t) { cout << h << ' '; puta(t...); } template <class S, class T> void tf(bool b, S t, T f) { if (b) puta(t); else puta(f); } void YN(bool b) { tf(b, "YES", "NO"); } void Yn(bool b) { tf(b, "Yes", "No"); } void yn(bool b) { tf(b, "yes", "no"); } template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << "[" << p.first << ", " << p.second << "]"; return os; }; template <class S> auto &operator<<(ostream &os, vector<S> t) { bool a = 1; for (auto s : t) { os << (a ? "" : " ") << s; a = 0; } return os; } template <class S> auto &operator>>(istream &is, vector<S> &t) { for (S &a : t) cin >> a; return is; } vvl comb(ll n, ll r) { vvl v(n + 1, vl(n + 1, 0)); for (ll i = 0; i < v.size(); i++) { v[i][0] = 1; v[i][i] = 1; } for (ll j = 1; j < v.size(); j++) { for (ll k = 1; k < j; k++) { v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]); } } return v; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n, k; cin >> n >> k; vl h(n); cin >> h; rep(i, k) { h.push_back(INF); } vl cost(n + k + 1, INF); cost[0] = 0; /* range(i,1,n){ ll a,b=INF; a=abs(h[i]-h[i-1]) + cost[i-1]; if(i>1) b=abs(h[i]-h[i-2]) + cost[i-2]; //cout<<a<<" "<<b<<" "; cost[i]=min(a,b); //puta(cost[i]); }*/ rep(i, n + k + 1) { range(j, 1, k + 1) { ll a = abs(h[i] - h[i + j]); // cout<<a<<" "; cost[i + j] = min(cost[i + j], cost[i] + a); // cout<<cost[i]<<" "<<cost[i+j]<<endl; } } puta(cost[n - 1]); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vb = vector<bool>; using vvb = vector<vb>; using vd = vector<double>; using vvd = vector<vd>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using pll = pair<ll, ll>; using tll = tuple<ll, ll>; using tlll = tuple<ll, ll, ll>; using vs = vector<string>; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define rep(i, n) range(i, 0, n) #define rrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define range(i, a, n) for (ll i = (a); i < (n); i++) #define LINF ((ll)1ll << 60) #define INF ((int)1 << 30) #define EPS (1e-9) #define MOD (1000000007ll) #define fcout(a) cout << setprecision(a) << fixed #define fs first #define sc second #define PI (3.1415926535897932384) int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class S> S sumvec(vector<S> &a) { return accumulate(all(a), S()); } template <class S> S maxvec(vector<S> &a) { return *max_element(all(a)); } template <class S> S minvec(vector<S> &a) { return *min_element(all(a)); } ll max(int a, ll b) { return max((ll)a, b); } ll max(ll a, int b) { return max(a, (ll)b); } int sgn(const double &r) { return (r > EPS) - (r < -EPS); } // a>0 : sgn(a)>0 int sgn(const double &a, const double &b) { return sgn(a - b); } // b<=c : sgn(b,c)<=0 template <class T> void puta(T &&t) { cout << t << "\n"; } template <class H, class... T> void puta(H &&h, T &&...t) { cout << h << ' '; puta(t...); } template <class S, class T> void tf(bool b, S t, T f) { if (b) puta(t); else puta(f); } void YN(bool b) { tf(b, "YES", "NO"); } void Yn(bool b) { tf(b, "Yes", "No"); } void yn(bool b) { tf(b, "yes", "no"); } template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << "[" << p.first << ", " << p.second << "]"; return os; }; template <class S> auto &operator<<(ostream &os, vector<S> t) { bool a = 1; for (auto s : t) { os << (a ? "" : " ") << s; a = 0; } return os; } template <class S> auto &operator>>(istream &is, vector<S> &t) { for (S &a : t) cin >> a; return is; } vvl comb(ll n, ll r) { vvl v(n + 1, vl(n + 1, 0)); for (ll i = 0; i < v.size(); i++) { v[i][0] = 1; v[i][i] = 1; } for (ll j = 1; j < v.size(); j++) { for (ll k = 1; k < j; k++) { v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]); } } return v; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n, k; cin >> n >> k; vl h(n); cin >> h; rep(i, k * 3) { h.push_back(INF); } vl cost(n + k * 3, INF); cost[0] = 0; /* range(i,1,n){ ll a,b=INF; a=abs(h[i]-h[i-1]) + cost[i-1]; if(i>1) b=abs(h[i]-h[i-2]) + cost[i-2]; //cout<<a<<" "<<b<<" "; cost[i]=min(a,b); //puta(cost[i]); }*/ rep(i, n + k + 1) { range(j, 1, k + 1) { ll a = abs(h[i] - h[i + j]); // cout<<a<<" "; cost[i + j] = min(cost[i + j], cost[i] + a); // cout<<cost[i]<<" "<<cost[i+j]<<endl; } } puta(cost[n - 1]); }
replace
108
110
108
110
0
p03161
C++
Runtime Error
/* Author: Sanskar Agarwal Nick: sanskarag Birla Institute Of Technology, Mesra */ #include <bits/stdc++.h> using namespace std; #define ll long long int #define endl "\n" #define fast \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define F(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++) #define RF(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--) #define INF 100009 #define mod 1000000007 #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) #define ff first #define ss second #define test \ while (t--) \ solve(); void solve() { ll n, k; cin >> n >> k; ll arr[n + 1]; for (ll i = 1; i <= n; ++i) cin >> arr[i]; ll dp[n + 1]; dp[0] = 0; dp[1] = 0; for (ll i = 2; i <= n; ++i) { dp[i] = INT_MAX; ll j = max(i - k, 1LL); for (j; j < i; ++j) { ll temp = dp[j] + abs(arr[i] - arr[j]); dp[i] = min(dp[i], temp); } } cout << dp[n] << endl; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast ll t; // cin >> t; t = 1; test return 0; }
/* Author: Sanskar Agarwal Nick: sanskarag Birla Institute Of Technology, Mesra */ #include <bits/stdc++.h> using namespace std; #define ll long long int #define endl "\n" #define fast \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define F(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++) #define RF(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--) #define INF 100009 #define mod 1000000007 #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) #define ff first #define ss second #define test \ while (t--) \ solve(); void solve() { ll n, k; cin >> n >> k; ll arr[n + 1]; for (ll i = 1; i <= n; ++i) cin >> arr[i]; ll dp[n + 1]; dp[0] = 0; dp[1] = 0; for (ll i = 2; i <= n; ++i) { dp[i] = INT_MAX; ll j = max(i - k, 1LL); for (j; j < i; ++j) { ll temp = dp[j] + abs(arr[i] - arr[j]); dp[i] = min(dp[i], temp); } } cout << dp[n] << endl; } int main() { // #ifndef ONLINE_JUDGE // freopen("../input.txt", "r", stdin); // freopen("../output.txt", "w", stdout); // #endif fast ll t; // cin >> t; t = 1; test return 0; }
replace
46
50
46
50
-11
p03161
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> using namespace std; #include <bits/stdc++.h> #define ll long long #define ld long double #define el '\n' #define rep(i, a, b) for (ll i = (a); i < (b); i++) #define repd(i, a, b) for (ll i = (b)-1; i >= (a); i--) #define large 1000000007 #define pb() push_back() #define MAX 10e+7 int dp[100000]; int hp(int arr[], int n, int k, int i) { int min = INT_MAX; int z = i - k; if (z < 0) z = 0; rep(j, z, i) { if (dp[j] + abs(arr[j] - arr[i]) < min) min = dp[j] + abs(arr[j] - arr[i]); } return min; } void solve() { int n, k; cin >> n >> k; int arr[n]; rep(i, 0, n) { cin >> arr[i]; } dp[0] = 0; dp[1] = abs(arr[1] - arr[0]); rep(i, 2, n) { dp[i] = hp(arr, n, k, i); } // cout<<dp[1]<<el; cout << dp[n - 1] << el; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif long long int t; solve(); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> using namespace std; #include <bits/stdc++.h> #define ll long long #define ld long double #define el '\n' #define rep(i, a, b) for (ll i = (a); i < (b); i++) #define repd(i, a, b) for (ll i = (b)-1; i >= (a); i--) #define large 1000000007 #define pb() push_back() #define MAX 10e+7 int dp[100000]; int hp(int arr[], int n, int k, int i) { int min = INT_MAX; int z = i - k; if (z < 0) z = 0; rep(j, z, i) { if (dp[j] + abs(arr[j] - arr[i]) < min) min = dp[j] + abs(arr[j] - arr[i]); } return min; } void solve() { int n, k; cin >> n >> k; int arr[n]; rep(i, 0, n) { cin >> arr[i]; } dp[0] = 0; dp[1] = abs(arr[1] - arr[0]); rep(i, 2, n) { dp[i] = hp(arr, n, k, i); } // cout<<dp[1]<<el; cout << dp[n - 1] << el; } int main() { long long int t; solve(); return 0; }
replace
39
43
39
40
-11
p03161
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <vector> using namespace std; 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 long long INF = 1LL << 60; int main() { int N, k; long long h[10000]; long long dp[10000]; cin >> N >> k; for (int i = 0; i < N; ++i) { cin >> h[i]; } for (int i = 1; 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] - h[i + j])); } } cout << dp[N - 1] << endl; }
#include <cstdlib> #include <iostream> #include <vector> using namespace std; 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 long long INF = 1LL << 60; int main() { int N, k; long long h[110000]; long long dp[110000]; cin >> N >> k; for (int i = 0; i < N; ++i) { cin >> h[i]; } for (int i = 1; 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] - h[i + j])); } } cout << dp[N - 1] << endl; }
replace
23
25
23
25
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define inf 1e18 #define ll long long int using namespace std; int main(void) { int i, j, n, k; cin >> n >> k; ll a[n]; for (i = 0; i < n; i++) cin >> a[i]; ll dp[n + 1]; for (i = 0; i <= n; i++) dp[i] = inf; dp[1] = 0; dp[2] = abs(a[1] - a[0]); for (i = 3; i <= k; i++) { // cout<<"**"<<endl; for (j = 1; j < i; j++) dp[i] = min(dp[j] + llabs(a[i - 1] - a[j - 1]), dp[i]); } for (i = k + 1; i <= n; i++) { // cout<<"&&"<<endl; for (j = i - k; j < i; j++) dp[i] = min(dp[j] + llabs(a[i - 1] - a[j - 1]), dp[i]); } cout << dp[n]; }
#include <bits/stdc++.h> #define inf 1e18 #define ll long long int using namespace std; int main(void) { int i, j, n, k; cin >> n >> k; ll a[n]; for (i = 0; i < n; i++) cin >> a[i]; ll dp[n + 1]; for (i = 0; i <= n; i++) dp[i] = inf; dp[1] = 0; dp[2] = abs(a[1] - a[0]); for (i = 3; i <= min(n, k); i++) { // cout<<"**"<<endl; for (j = 1; j < i; j++) dp[i] = min(dp[j] + llabs(a[i - 1] - a[j - 1]), dp[i]); } for (i = k + 1; i <= n; i++) { // cout<<"&&"<<endl; for (j = i - k; j < i; j++) dp[i] = min(dp[j] + llabs(a[i - 1] - a[j - 1]), dp[i]); } cout << dp[n]; }
replace
16
17
16
17
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define filename "frog" #define int long long #define oo 1e9 #define N 25 using namespace std; int n, k; int f[N], a[N]; void open() { // freopen(filename".inp","r",stdin) ; // freopen(filename".out","w",stdout) ; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void readin() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; } void process() { for (int i = 1; i <= n; i++) f[i] = oo; f[1] = 0; for (int i = 1; i < n; i++) for (int j = 1; j <= k; j++) { if (i + j > n) break; int pre = f[i] + abs(a[i] - a[j + i]); f[i + j] = min(f[i + j], pre); } } signed main() { open(); readin(); process(); cout << f[n]; }
#include <bits/stdc++.h> #define filename "frog" #define int long long #define oo 1e9 #define N 200005 using namespace std; int n, k; int f[N], a[N]; void open() { // freopen(filename".inp","r",stdin) ; // freopen(filename".out","w",stdout) ; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void readin() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; } void process() { for (int i = 1; i <= n; i++) f[i] = oo; f[1] = 0; for (int i = 1; i < n; i++) for (int j = 1; j <= k; j++) { if (i + j > n) break; int pre = f[i] + abs(a[i] - a[j + i]); f[i + j] = min(f[i + j], pre); } } signed main() { open(); readin(); process(); cout << f[n]; }
replace
4
5
4
5
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(NULL); int n, k; cin >> n >> k; int h[n]; for (int i = 0; i < n; i++) { cin >> h[i]; } int dp[n]; for (int i = 0; i <= k; i++) { if (!i) { dp[i] = 0; } else { dp[i] = abs(h[i] - h[0]); } } for (int i = k + 1; i < n; i++) { dp[i] = INT_MAX; for (int j = i - 1; j >= (i - k); j--) { dp[i] = min(dp[i], (dp[j] + abs(h[i] - h[j]))); } } cout << dp[n - 1]; 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(NULL); int n, k; cin >> n >> k; int h[n]; for (int i = 0; i < n; i++) { cin >> h[i]; } if (n <= k) { int ans = abs(h[0] - h[n - 1]); cout << ans; return 0; } int dp[n]; for (int i = 0; i <= k; i++) { if (!i) { dp[i] = 0; } else { dp[i] = abs(h[i] - h[0]); } } for (int i = k + 1; i < n; i++) { dp[i] = INT_MAX; for (int j = i - 1; j >= (i - k); j--) { dp[i] = min(dp[i], (dp[j] + abs(h[i] - h[j]))); } } cout << dp[n - 1]; return 0; }
insert
17
17
17
23
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; vector<long long> h(N); for (int i = 0; i < N; i++) { cin >> h[i]; } const long long INF = 1e16; long long dp[N]; fill(dp, dp + N, INF); dp[0] = 0; for (int i = 1; i < N; i++) { for (int j = i - K; j < i; j++) { dp[i] = min({dp[i], dp[j] + abs(h[i] - h[j])}); } } cout << dp[N - 1] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; vector<long long> h(N); for (int i = 0; i < N; i++) { cin >> h[i]; } const long long INF = 1e16; long long dp[N]; fill(dp, dp + N, INF); dp[0] = 0; for (int i = 1; i < N; i++) { for (int j = i - K; j < i; j++) { if (j >= 0) { dp[i] = min({dp[i], dp[j] + abs(h[i] - h[j])}); } } } cout << dp[N - 1] << endl; }
replace
18
19
18
21
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } vector<int> dp(n); dp[0] = 0; dp[1] = abs(v[0] - v[1]); int x; for (int i = 2; i < n; i++) { x = 1000000; for (int j = 0; j < k; j++) { x = min(x, abs(v[i] - v[i - j - 1]) + dp[i - j - 1]); } dp[i] = x; } cout << dp[n - 1] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } vector<int> dp(n); dp[0] = 0; dp[1] = abs(v[0] - v[1]); int x; for (int i = 2; i < n; i++) { x = INT_MAX; for (int j = 0; j < k && j < i; j++) { x = min(x, abs(v[i] - v[i - j - 1]) + dp[i - j - 1]); } dp[i] = x; } cout << dp[n - 1] << '\n'; return 0; }
replace
17
19
17
19
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define MAXN 100005 #define ll long long using namespace std; ll dp[MAXN]; int n, k; vector<ll> v; ll solve(int i) { if (i < 0) return 0; if (dp[i] >= 0) { return dp[i]; } ll res1 = -1; for (int j = 1; j <= k; ++j) if (res1 != -1) res1 = min(res1, abs(v[i] - v[i - j]) + solve(i - j)); else res1 = abs(v[i] - v[i - j]) + solve(i - j); dp[i] = res1; return dp[i]; } int main() { ll num; cin >> n >> k; memset(dp, -1, sizeof(dp)); for (int i = 0; i < n; ++i) { cin >> num; v.push_back(num); } dp[0] = 0LL; dp[1] = abs(v[0] - v[1]); cout << solve(n - 1) << endl; return 0; }
#include <bits/stdc++.h> #define MAXN 100005 #define ll long long using namespace std; ll dp[MAXN]; int n, k; vector<ll> v; ll solve(int i) { if (i < 0) return 0; if (dp[i] >= 0) { return dp[i]; } ll res1 = -1; for (int j = 1; j <= min(k, i); ++j) if (res1 != -1) res1 = min(res1, abs(v[i] - v[i - j]) + solve(i - j)); else res1 = abs(v[i] - v[i - j]) + solve(i - j); dp[i] = res1; return dp[i]; } int main() { ll num; cin >> n >> k; memset(dp, -1, sizeof(dp)); for (int i = 0; i < n; ++i) { cin >> num; v.push_back(num); } dp[0] = 0LL; dp[1] = abs(v[0] - v[1]); cout << solve(n - 1) << endl; return 0; }
replace
17
18
17
18
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef long long ll; using namespace std; ll n, h, ans[100005], arr[100005], sto[100005]; ll solve(int ind) { if (ind >= n) return 2e9; if (ind == n - 1) return 0; if (~sto[ind]) return sto[ind]; ll ans = 2e9; for (int i = 1; i <= min(n - ind, h); i++) ans = min(ans, abs(arr[ind] - arr[ind + i]) + solve(ind + i)); return ans; } int main() { cin >> n >> h; memset(sto, -1, sizeof sto); for (int i = 0; i < n; cin >> arr[i++]) ; cout << solve(0); return 0; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; ll n, h, ans[100005], arr[100005], sto[100005]; ll solve(int ind) { if (ind >= n) return 2e9; if (ind == n - 1) return 0; if (~sto[ind]) return sto[ind]; ll ans = 2e9; for (int i = 1; i <= min(n - ind, h); i++) ans = min(ans, abs(arr[ind] - arr[ind + i]) + solve(ind + i)); sto[ind] = ans; return ans; } int main() { cin >> n >> h; memset(sto, -1, sizeof sto); for (int i = 0; i < n; cin >> arr[i++]) ; cout << solve(0); return 0; }
insert
14
14
14
15
TLE
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define fastio \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define range(i, l, h) for (int i = l; i < h; i++) #define endl '\n' #define I INT_MAX #define L INT_MIN #define pb push_back #define ll long long #define ull unsigned long long #define vi vector<int> using namespace std; using std::ios; constexpr int MOD = 1e9 + 7; constexpr int mod = 998244353; ll cost[100005]; int main() { fastio; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif memset(cost, 0, sizeof cost); int n, k; cin >> n >> k; vi a(n + 1); range(i, 1, n + 1) cin >> a[i]; cost[1] = 0; range(i, 1, k + 1) { cost[i] = abs(a[i] - a[1]); range(j, 1, i) { cost[i] = min(cost[i], cost[i - j] + abs(a[i - j] - a[i])); } // cost[t] } range(i, k + 1, n + 1) { cost[i] = I; range(j, 1, k + 1) { if (i - j > 0) cost[i] = min(cost[i], cost[i - j] + abs(a[i - j] - a[i])); } } cout << cost[n]; }
#include <bits/stdc++.h> #define fastio \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define range(i, l, h) for (int i = l; i < h; i++) #define endl '\n' #define I INT_MAX #define L INT_MIN #define pb push_back #define ll long long #define ull unsigned long long #define vi vector<int> using namespace std; using std::ios; constexpr int MOD = 1e9 + 7; constexpr int mod = 998244353; ll cost[100005]; int main() { fastio; memset(cost, 0, sizeof cost); int n, k; cin >> n >> k; vi a(n + 1); range(i, 1, n + 1) cin >> a[i]; cost[1] = 0; range(i, 1, k + 1) { cost[i] = abs(a[i] - a[1]); range(j, 1, i) { cost[i] = min(cost[i], cost[i - j] + abs(a[i - j] - a[i])); } // cost[t] } range(i, k + 1, n + 1) { cost[i] = I; range(j, 1, k + 1) { if (i - j > 0) cost[i] = min(cost[i], cost[i - j] + abs(a[i - j] - a[i])); } } cout << cost[n]; }
delete
22
26
22
22
-11
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define un unordered_map #define us unordered_set #define ll long long #define l long using namespace std; const int mod = 1e9 + 7; const int inf = 1e9 + 3; int main() { l n, k; cin >> n >> k; vector<l> v(n); for (l i = 0; i < n; i++) { cin >> v[i]; } l dp[n]; dp[0] = 0; for (l i = 1; i < n; i++) { l mini = inf; for (int j = 1; j <= k; j++) { mini = min((dp[i - j] + abs(v[i] - v[i - j])), mini); } dp[i] = mini; } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> #define pb push_back #define un unordered_map #define us unordered_set #define ll long long #define l long using namespace std; const int mod = 1e9 + 7; const int inf = 1e9 + 3; int main() { l n, k; cin >> n >> k; vector<l> v(n); for (l i = 0; i < n; i++) { cin >> v[i]; } l dp[n]; dp[0] = 0; for (l i = 1; i < n; i++) { l mini = inf; for (int j = 1; j <= k; j++) { if (i - j < 0) break; mini = min((dp[i - j] + abs(v[i] - v[i - j])), mini); } dp[i] = mini; } cout << dp[n - 1]; return 0; }
insert
23
23
23
25
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int #define ld long double #define pb push_back #define mp make_pair #define mod 998244353 #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #else // online submission #endif IOS int n, k; cin >> n >> k; vector<ll> cost; vector<ll> ans(n, 0); for (int i = 0; i < n; i++) { ll x; cin >> x; cost.pb(x); } ans[1] = abs(cost[1] - cost[0]); for (int i = 2; i < n; i++) { ll mini = INT_MAX; for (int j = 1; j <= k; j++) { mini = min(mini, ans[i - j] + abs(cost[i] - cost[i - j])); } ans[i] = mini; } cout << ans[n - 1]; return 0; }
#include <bits/stdc++.h> #define ll long long int #define ld long double #define pb push_back #define mp make_pair #define mod 998244353 #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #else // online submission #endif IOS int n, k; cin >> n >> k; vector<ll> cost; vector<ll> ans(n, 0); for (int i = 0; i < n; i++) { ll x; cin >> x; cost.pb(x); } ans[1] = abs(cost[1] - cost[0]); for (int i = 2; i < n; i++) { ll mini = INT_MAX; for (int j = 1; j <= k && i - j >= 0; j++) { mini = min(mini, ans[i - j] + abs(cost[i] - cost[i - j])); } ans[i] = mini; } cout << ans[n - 1]; return 0; }
replace
31
32
31
32
-11
p03161
C++
Runtime Error
#include <iostream> #include <limits.h> using namespace std; int dp[100004]; int k; int mincost(int n, int height[]) { if (n == 1 || n == 0) return 0; if (dp[n] != -1) return dp[n]; int cost1 = INT_MAX; for (int i = 1; i <= k; i++) { if (n - i - 1 >= 0 && n - i >= 0) cost1 = min(cost1, abs(height[n - 1] - height[n - i - 1]) + mincost(n - i, height)); } return dp[n] = cost1; } int main() { int n; cin >> n; cin >> k; int height[1000]; for (int i = 0; i < n; i++) cin >> height[i]; for (int i = 0; i < 100004; i++) dp[i] = -1; cout << mincost(n, height); }
#include <iostream> #include <limits.h> using namespace std; int dp[100004]; int k; int mincost(int n, int height[]) { if (n == 1 || n == 0) return 0; if (dp[n] != -1) return dp[n]; int cost1 = INT_MAX; for (int i = 1; i <= k; i++) { if (n - i - 1 >= 0 && n - i >= 0) cost1 = min(cost1, abs(height[n - 1] - height[n - i - 1]) + mincost(n - i, height)); } return dp[n] = cost1; } int main() { int n; cin >> n; cin >> k; int height[100004]; for (int i = 0; i < n; i++) cin >> height[i]; for (int i = 0; i < 100004; i++) dp[i] = -1; cout << mincost(n, height); }
replace
22
23
22
23
0
p03161
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define pb push_back #define INF 0x3f3f3f3f #define LINF 0x3f3f3f3f3f3f3f3f #define ms memset // #define MAXN 10005 // #define MAXM 100005 #define pi 3.14159265359 typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vpii; typedef long long ll; typedef priority_queue<int> pqi; typedef priority_queue<pii, vpii, greater<pii>> dij; int N, K; int H[100005]; int memo[100005]; int solve(int x) { if (x == N - 1) return 0; if (x >= N) return INF; int &state = memo[x]; if (state != -1) return state; int minimo = INF; for (int i = 1; i <= K; i++) { minimo = min(abs(H[x] - H[x + i]) + solve(x + i), minimo); } return minimo; } 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]; } ms(memo, -1, sizeof memo); cout << solve(0) << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define INF 0x3f3f3f3f #define LINF 0x3f3f3f3f3f3f3f3f #define ms memset // #define MAXN 10005 // #define MAXM 100005 #define pi 3.14159265359 typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vpii; typedef long long ll; typedef priority_queue<int> pqi; typedef priority_queue<pii, vpii, greater<pii>> dij; int N, K; int H[100005]; int memo[100005]; int solve(int x) { if (x == N - 1) return 0; if (x >= N) return INF; int &state = memo[x]; if (state != -1) return state; int minimo = INF; for (int i = 1; i <= K; i++) { minimo = min(abs(H[x] - H[x + i]) + solve(x + i), minimo); } return state = minimo; } 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]; } ms(memo, -1, sizeof memo); cout << solve(0) << "\n"; return 0; }
replace
39
40
39
40
TLE
p03161
C++
Runtime Error
/* Jay Solanki */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> typedef long long int ll; #define pii pair<ll, ll> #define pb push_back #define pf push_front #define F first #define S second #define mp make_pair #define For(i, a, b) for (ll i = a; i < b; i++) #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; ll mode = 1e9 + 7; int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { int q = a / m; int t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } ll power(ll x, ll y) { ll res = 1; x = x % mode; while (y > 0) { if (y & 1) res = (res * x) % mode; y = y >> 1; x = (x * x) % mode; } return res; } int main() { FAST; int t = 1; // cin>>t; while (t--) { ll n, c = 0, k = 1, y = 0, x = 0, z = 0; cin >> n >> k; // string s; // cin>>s; vector<ll> v(n), ans(n); For(i, 0, n) cin >> v[i]; ans[0] = 0; for (int i = 1; i < n; i++) { if (i == 1) ans[i] = abs(v[i - 1] - v[i]); else { ans[i] = INT_MAX; for (int j = i - 1; i - j <= k; j--) ans[i] = min(ans[i], ans[j] + abs(v[i] - v[j])); } } cout << ans[n - 1]; } return 0; }
/* Jay Solanki */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> typedef long long int ll; #define pii pair<ll, ll> #define pb push_back #define pf push_front #define F first #define S second #define mp make_pair #define For(i, a, b) for (ll i = a; i < b; i++) #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; ll mode = 1e9 + 7; int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { int q = a / m; int t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } ll power(ll x, ll y) { ll res = 1; x = x % mode; while (y > 0) { if (y & 1) res = (res * x) % mode; y = y >> 1; x = (x * x) % mode; } return res; } int main() { FAST; int t = 1; // cin>>t; while (t--) { ll n, c = 0, k = 1, y = 0, x = 0, z = 0; cin >> n >> k; // string s; // cin>>s; vector<ll> v(n), ans(n); For(i, 0, n) cin >> v[i]; ans[0] = 0; for (int i = 1; i < n; i++) { if (i == 1) ans[i] = abs(v[i - 1] - v[i]); else { ans[i] = INT_MAX; for (int j = i - 1; j >= 0 && i - j <= k; j--) ans[i] = min(ans[i], ans[j] + abs(v[i] - v[j])); } } cout << ans[n - 1]; } return 0; }
replace
72
73
72
73
0
p03161
C++
Runtime Error
#include <iostream> #include <vector> #define FASTIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define INF 1e9 + 5 using namespace std; int get_first_negative(vector<int> &vt) { int low = 0, high = vt.size() - 1; while (low <= high) { int mid = (low + high) / 2; if (vt[mid] < 0 && vt[max(mid - 1, 0)] >= 0) { return mid; } else if (vt[mid] >= 0) { low = mid + 1; } else if (vt[max(mid - 1, 0)] < 0) { high = mid - 1; } } return -1; } int get_first_zero(vector<int> &vt) { int low = 0, high = vt.size() - 1; while (low <= high) { int mid = (low + high) / 2; if (vt[mid] == 0 && vt[max(mid - 1, 0)] == 1) { return mid; } else if (vt[mid] == 1) { low = mid + 1; } else if (vt[mid] == 0) { high = mid - 1; } } return -1; } int min_jumps(vector<int> &stones, int n, int k) { vector<int> dp(n, INF); dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) { dp[i + j] = min(dp[i + j], dp[i] + abs(stones[i] - stones[min(i + j, n - 1)])); } } return dp[n - 1]; } int main() { FASTIO int n, k; cin >> n; cin >> k; vector<int> vt(n); for (int &elem : vt) { cin >> elem; } cout << min_jumps(vt, vt.size(), k); return 0; }
#include <iostream> #include <vector> #define FASTIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define INF 1e9 + 5 using namespace std; int get_first_negative(vector<int> &vt) { int low = 0, high = vt.size() - 1; while (low <= high) { int mid = (low + high) / 2; if (vt[mid] < 0 && vt[max(mid - 1, 0)] >= 0) { return mid; } else if (vt[mid] >= 0) { low = mid + 1; } else if (vt[max(mid - 1, 0)] < 0) { high = mid - 1; } } return -1; } int get_first_zero(vector<int> &vt) { int low = 0, high = vt.size() - 1; while (low <= high) { int mid = (low + high) / 2; if (vt[mid] == 0 && vt[max(mid - 1, 0)] == 1) { return mid; } else if (vt[mid] == 1) { low = mid + 1; } else if (vt[mid] == 0) { high = mid - 1; } } return -1; } int min_jumps(vector<int> &stones, int n, int k) { vector<int> dp(n, INF); dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) { dp[min(i + j, n - 1)] = min(dp[min(i + j, n - 1)], dp[i] + abs(stones[i] - stones[min(i + j, n - 1)])); } } return dp[n - 1]; } int main() { FASTIO int n, k; cin >> n; cin >> k; vector<int> vt(n); for (int &elem : vt) { cin >> elem; } cout << min_jumps(vt, vt.size(), k); return 0; }
replace
45
47
45
48
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define inf (pow(10, 18)) #define ll long long int #define mod 1000000007 #define ntq(z) \ long long int z; \ cin >> z; \ for (long long int i = 0; i < z; i++) #define fi(x9, y9) for (long long int i = x9; i < y9; i++) #define fj(x9, y9) for (long long int j = x9; j < y9; j++) #define fk(x9, y9) for (long long int k = x9; k < y9; k++) #define f(z9) for (long long int i = 0; i < z9; i++) #define endl "\n" #define sz(a) int((a).size()) #define pb push_back #define all(c) (c).begin(), (c).end() #define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) #define present(c, x) ((c).find(x) != (c).end()) #define cpresent(c, x) (find(all(c), x) != (c).end()) #define stoi(s, n) \ stringstream str(s); \ str >> n; #define get_vi(v, n) \ vi v(n); \ f(n) { cin >> v[i]; } typedef vector<ll> vi; typedef vector<pair<ll, ll>> vp; typedef vector<vi> vvi; typedef pair<ll, ll> ii; void printprecise(double l, ll precision) { std::cout << std::fixed; std::cout << std::setprecision(precision); std::cout << l; } ll ncr(ll n, ll k) { ll C[n + 1][k + 1]; ll i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= min(i, k); j++) { if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } return C[n][k]; } ll power_wm(ll x, ll y, ll p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } ll power(ll x, ll y) { ll res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x); // y must be even now y = y >> 1; // y = y/2 x = (x * x); } return res; } ll root(ll x, vi &roots) { if (roots[x] != x) roots[x] = root(roots[x], roots); return roots[x]; } void unoin(ll p, ll q, vi &roots, vi &rank) { ll rp = root(p, roots); ll rq = root(q, roots); if (rank[rq] < rank[rp]) { roots[rq] = rp; } else if (rank[rp] > rank[rq]) { roots[rp] = rq; } else { roots[rp] = rq; rank[rq]++; } } ll dig_sum(ll n) { ll s = 0; while (n) { s += n % 10; n /= 10; } return s; } ll gcd(ll a, ll b) { if (a < b) { a = a + b; b = a - b; a = a - b; } if (b == 0) return a; return gcd(b, a % b); } ll toD(ll x, ll y, ll m) { return x * m + y % m; } pair<ll, ll> twoD(ll p, ll m) { return (make_pair(p / m, p % m)); } vi parentof, vis, dist; vvi g; void dfs(int p) { vis[p] = 1; f(g[p].size()) { if (vis[g[p][i]] == 0) { parentof[g[p][i]] = p; dist[g[p][i]] = dist[p] + 1; dfs(g[p][i]); } } } vvi graph(1000); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n, k; cin >> n >> k; k = min(k, n); vi v(n); f(n) { cin >> v[i]; } vi dp(n, inf); dp[0] = 0; fi(1, n) { fj(max(0ll, i - k), i) { dp[i] = min(dp[i], dp[j] + abs(v[i] - v[j])); } } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; #define inf (pow(10, 18)) #define ll long long int #define mod 1000000007 #define ntq(z) \ long long int z; \ cin >> z; \ for (long long int i = 0; i < z; i++) #define fi(x9, y9) for (long long int i = x9; i < y9; i++) #define fj(x9, y9) for (long long int j = x9; j < y9; j++) #define fk(x9, y9) for (long long int k = x9; k < y9; k++) #define f(z9) for (long long int i = 0; i < z9; i++) #define endl "\n" #define sz(a) int((a).size()) #define pb push_back #define all(c) (c).begin(), (c).end() #define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) #define present(c, x) ((c).find(x) != (c).end()) #define cpresent(c, x) (find(all(c), x) != (c).end()) #define stoi(s, n) \ stringstream str(s); \ str >> n; #define get_vi(v, n) \ vi v(n); \ f(n) { cin >> v[i]; } typedef vector<ll> vi; typedef vector<pair<ll, ll>> vp; typedef vector<vi> vvi; typedef pair<ll, ll> ii; void printprecise(double l, ll precision) { std::cout << std::fixed; std::cout << std::setprecision(precision); std::cout << l; } ll ncr(ll n, ll k) { ll C[n + 1][k + 1]; ll i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= min(i, k); j++) { if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } return C[n][k]; } ll power_wm(ll x, ll y, ll p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } ll power(ll x, ll y) { ll res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x); // y must be even now y = y >> 1; // y = y/2 x = (x * x); } return res; } ll root(ll x, vi &roots) { if (roots[x] != x) roots[x] = root(roots[x], roots); return roots[x]; } void unoin(ll p, ll q, vi &roots, vi &rank) { ll rp = root(p, roots); ll rq = root(q, roots); if (rank[rq] < rank[rp]) { roots[rq] = rp; } else if (rank[rp] > rank[rq]) { roots[rp] = rq; } else { roots[rp] = rq; rank[rq]++; } } ll dig_sum(ll n) { ll s = 0; while (n) { s += n % 10; n /= 10; } return s; } ll gcd(ll a, ll b) { if (a < b) { a = a + b; b = a - b; a = a - b; } if (b == 0) return a; return gcd(b, a % b); } ll toD(ll x, ll y, ll m) { return x * m + y % m; } pair<ll, ll> twoD(ll p, ll m) { return (make_pair(p / m, p % m)); } vi parentof, vis, dist; vvi g; void dfs(int p) { vis[p] = 1; f(g[p].size()) { if (vis[g[p][i]] == 0) { parentof[g[p][i]] = p; dist[g[p][i]] = dist[p] + 1; dfs(g[p][i]); } } } vvi graph(1000); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, k; cin >> n >> k; k = min(k, n); vi v(n); f(n) { cin >> v[i]; } vi dp(n, inf); dp[0] = 0; fi(1, n) { fj(max(0ll, i - k), i) { dp[i] = min(dp[i], dp[j] + abs(v[i] - v[j])); } } cout << dp[n - 1]; return 0; }
delete
152
156
152
152
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03161
C++
Runtime Error
#include <cmath> #include <iostream> using namespace std; typedef long long ll; 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 (a > b) { a = b; return 1; } return 0; } const long long LINF = 1e18; int main() { int N, K; cin >> N >> K; ll h[100000]; for (int i = 0; i < N; ++i) { cin >> h[i]; } ll dp[100000]; for (int i = 0; i < 100000; ++i) { dp[i] = LINF; } dp[0] = 0; for (int i = 0; i < N; ++i) { for (int j = 1; j <= K && j <= N; ++j) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } } cout << dp[N - 1] << endl; }
#include <cmath> #include <iostream> using namespace std; typedef long long ll; 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 (a > b) { a = b; return 1; } return 0; } const long long LINF = 1e18; int main() { int N, K; cin >> N >> K; ll h[100000]; for (int i = 0; i < N; ++i) { cin >> h[i]; } ll dp[100000]; for (int i = 0; i < 100000; ++i) { dp[i] = LINF; } dp[0] = 0; for (int i = 0; i < N; ++i) { for (int j = 1; j <= K && i + j < N; ++j) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); } } cout << dp[N - 1] << endl; }
replace
38
39
38
39
0
p03161
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <vector> 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; } const long long INF = 1LL << 60; int main() { int N, K; cin >> N >> K; long long h[100010]; for (int i = 0; i < N; ++i) cin >> h[i]; long long dp[100010]; 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; ++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; 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; } const long long INF = 1LL << 60; int main() { int N, K; cin >> N >> K; long long h[100010]; for (int i = 0; i < N; ++i) cin >> h[i]; long long dp[100100]; for (int i = 0; i < 100100; ++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] - h[i + j])); } } cout << dp[N - 1] << endl; return 0; }
replace
28
30
28
30
0
p03161
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <limits.h> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> ans(n); int k; cin >> k; vector<int> r(n); int e; cin >> e; r[0] = e; ans[0] = 0; for (int i = 1; i < k; i++) { cin >> e; int add = INT_MAX; for (int j = 0; j < i; j++) { if (ans[j] + abs(e - r[j]) < add) { add = ans[j] + abs(e - r[j]); } } r[i] = e; ans[i] = add; } for (int i = k; i < n; i++) { cin >> e; r[i] = e; int add = INT_MAX; for (int j = i - k; j < i; j++) { if (ans[j] + abs(e - r[j]) < add) { add = ans[j] + abs(e - r[j]); } } ans[i] = add; } cout << ans[n - 1] << endl; return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <limits.h> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> ans(n); int k; cin >> k; vector<int> r(n); int e; cin >> e; r[0] = e; ans[0] = 0; for (int i = 1; i < k && i < n; i++) { cin >> e; int add = INT_MAX; for (int j = 0; j < i; j++) { if (ans[j] + abs(e - r[j]) < add) { add = ans[j] + abs(e - r[j]); } } r[i] = e; ans[i] = add; } for (int i = k; i < n; i++) { cin >> e; r[i] = e; int add = INT_MAX; for (int j = i - k; j < i; j++) { if (ans[j] + abs(e - r[j]) < add) { add = ans[j] + abs(e - r[j]); } } ans[i] = add; } cout << ans[n - 1] << endl; return 0; }
replace
35
36
35
36
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i) #define REPR(i, n) for (ll i = n; i >= 0; --i) #define FOR(i, m, n) for (ll i = m, i##_len = (n); i < i##_len; ++i) #define all(x) (x).begin(), (x).end() 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; } const int INF = 1e9; const ll LLINF = 1e16; ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main(void) { ll n, k; cin >> n >> k; vector<int> h(n); REP(i, n) cin >> h[i]; vector<ll> cost(n, 0); FOR(i, 1, n) { cost[i] = LLINF; FOR(j, i - k, i) { chmin(cost[i], abs(h[i] - h[j]) + cost[j]); } } cout << cost[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i) #define REPR(i, n) for (ll i = n; i >= 0; --i) #define FOR(i, m, n) for (ll i = m, i##_len = (n); i < i##_len; ++i) #define all(x) (x).begin(), (x).end() 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; } const int INF = 1e9; const ll LLINF = 1e16; ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main(void) { ll n, k; cin >> n >> k; vector<int> h(n); REP(i, n) cin >> h[i]; vector<ll> cost(n, 0); FOR(i, 1, n) { cost[i] = LLINF; FOR(j, max(0ll, i - k), i) { chmin(cost[i], abs(h[i] - h[j]) + cost[j]); } } cout << cost[n - 1] << endl; return 0; }
replace
48
49
48
49
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define rep(i, n) FOR(i, 0, n - 1) typedef long long ll; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } const ll INF = 1LL << 60; void solve(long long N, ll K, vector<long long> h) { vector<ll> dp(N + K + 100, INF); // initial point dp[0] = 0; if (K >= N) { K = N; } for (ll i = 1; i < N; ++i) { chmin(dp[i], dp[i - 1] + abs(h[i] - h[i - 1])); if (i > 1) { for (ll k = 1; k <= K; ++k) { chmin(dp[i], dp[i - k] + abs(h[i] - h[i - k])); } } } cout << dp[N - 1] << endl; } // Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You // use the default template now. You can remove this line by using your custom // template) int main() { long long N; cin >> N; long long K; cin >> K; vector<long long> h(N); for (ll i = 0; i < N; i++) { cin >> h[i]; } solve(N, K, move(h)); }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define rep(i, n) FOR(i, 0, n - 1) typedef long long ll; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } const ll INF = 1LL << 60; void solve(long long N, ll K, vector<long long> h) { vector<ll> dp(N + K + 100, INF); // initial point dp[0] = 0; if (K >= N) { K = N; } for (ll i = 0; i < N; ++i) { for (ll k = 1; k <= K; ++k) { chmin(dp[i + k], dp[i] + abs(h[i] - h[i + k])); } } cout << dp[N - 1] << endl; } // Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You // use the default template now. You can remove this line by using your custom // template) int main() { long long N; cin >> N; long long K; cin >> K; vector<long long> h(N); for (ll i = 0; i < N; i++) { cin >> h[i]; } solve(N, K, move(h)); }
replace
31
37
31
34
0
p03161
C++
Runtime Error
/* Educational DP Contest 2019/1/6 * B Frog-2 * 2020.3.29 練習 * Aの変化系.ジャンプ先が最大100から選ぶことになる. */ #include <algorithm> // max #include <iostream> #include <vector> // #include<cmath> using namespace std; int abs(int x) { return (x >= 0) ? x : -x; } // 最長増加部分列(LIS) int n; // 2~10^5 int k; // 1~100 ジャンプ先可能数 vector<int> h; // 数字列 1~10^4 vector<int> cost; // コストのメモ // 入力 void init() { cin >> n; cin >> k; h.assign(n + 1, 0); cost.assign(n + 1, 0); for (int i = 1; i < n + 1; i++) { cin >> h[i]; } return; } /* 再帰関数で実装した場合(Aの場合) int recur(int k){ // メモがある場合 if(cost[k]) return cost[k]; //メモあり // 境界条件 if(k==n) return 0; if(k==n-1) return abs(h[n-1]-h[n]); // 再帰 // メモ化 cost[k] = min( recur(k+1)+abs(h[k]-h[k+1]), recur(k+2)+abs(h[k]-h[k+2]) ); cout << "mem: k=" << k << ", cost=" << cost[k] << endl; return cost[k]; } //*/ //* DPで実装した場合 void dodp() { // 境界条件 cost[n] = 0; int temp; // k未満しか飛べない位置 for (int i = n - 1; i > n - k; i--) { // cout << i << " "; temp = cost[i + 1] + abs(h[i] - h[i + 1]); // k>=1なので for (int j = 2; i + j < n + 1; j++) { temp = min(temp, cost[i + j] + abs(h[i] - h[i + j])); } cost[i] = temp; // cout << "db1: i " << i << " cost: " << temp << endl; } for (int i = n - k; i > 0; i--) { temp = cost[i + 1] + abs(h[i] - h[i + 1]); // k>=1なので for (int j = 2; j < k + 1; j++) { temp = min(temp, cost[i + j] + abs(h[i] - h[i + j])); } cost[i] = temp; // cout << "db2: i " << i << " cost: " << temp << endl; } } //*/ int main(void) { init(); // cout << "test" << endl; dodp(); cout << cost[1] << endl; return 0; }
/* Educational DP Contest 2019/1/6 * B Frog-2 * 2020.3.29 練習 * Aの変化系.ジャンプ先が最大100から選ぶことになる. */ #include <algorithm> // max #include <iostream> #include <vector> // #include<cmath> using namespace std; int abs(int x) { return (x >= 0) ? x : -x; } // 最長増加部分列(LIS) int n; // 2~10^5 int k; // 1~100 ジャンプ先可能数 vector<int> h; // 数字列 1~10^4 vector<int> cost; // コストのメモ // 入力 void init() { cin >> n; cin >> k; h.assign(n + 1, 0); cost.assign(n + 1, 0); for (int i = 1; i < n + 1; i++) { cin >> h[i]; } return; } /* 再帰関数で実装した場合(Aの場合) int recur(int k){ // メモがある場合 if(cost[k]) return cost[k]; //メモあり // 境界条件 if(k==n) return 0; if(k==n-1) return abs(h[n-1]-h[n]); // 再帰 // メモ化 cost[k] = min( recur(k+1)+abs(h[k]-h[k+1]), recur(k+2)+abs(h[k]-h[k+2]) ); cout << "mem: k=" << k << ", cost=" << cost[k] << endl; return cost[k]; } //*/ //* DPで実装した場合 void dodp() { // 境界条件 cost[n] = 0; int temp; // k未満しか飛べない位置 for (int i = n - 1; i > n - k; i--) { // cout << i << " "; temp = cost[i + 1] + abs(h[i] - h[i + 1]); // k>=1なので for (int j = 2; i + j < n + 1; j++) { temp = min(temp, cost[i + j] + abs(h[i] - h[i + j])); } cost[i] = temp; if (i == 1) { break; } // cout << "db1: i " << i << " cost: " << temp << endl; } for (int i = n - k; i > 0; i--) { temp = cost[i + 1] + abs(h[i] - h[i + 1]); // k>=1なので for (int j = 2; j < k + 1; j++) { temp = min(temp, cost[i + j] + abs(h[i] - h[i + j])); } cost[i] = temp; // cout << "db2: i " << i << " cost: " << temp << endl; } } //*/ int main(void) { init(); // cout << "test" << endl; dodp(); cout << cost[1] << endl; return 0; }
insert
59
59
59
62
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fo(i, n) for (int i = 0; i < n; i++) int main() { int n, k, var; cin >> n >> k; long arr[n], temp[n], mi; fo(i, n) cin >> arr[i]; fo(i, n) { var = i - 1; mi = INT_MAX; while (var >= 0 && var >= i - k) { mi = min(abs(arr[i] - arr[var]) + temp[var], mi); var--; } temp[i] = mi == INT_MAX ? 0 : mi; } cout << temp[n - 1]; return 1; }
#include <bits/stdc++.h> using namespace std; #define fo(i, n) for (int i = 0; i < n; i++) int main() { int n, k, var; cin >> n >> k; long arr[n], temp[n], mi; fo(i, n) cin >> arr[i]; fo(i, n) { var = i - 1; mi = INT_MAX; while (var >= 0 && var >= i - k) { mi = min(abs(arr[i] - arr[var]) + temp[var], mi); var--; } temp[i] = mi == INT_MAX ? 0 : mi; } cout << temp[n - 1]; return 0; }
replace
24
25
24
25
1
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) x.begin(), x.end() typedef long long ll; int main() { int n, k; cin >> n >> k; int h[n]; rep(i, n) cin >> h[i]; int dp[n]; rep(i, n) dp[i] = 1e9; dp[0] = 0; 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])); } cout << dp[n - 1] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) x.begin(), x.end() typedef long long ll; int main() { int n, k; cin >> n >> k; int h[110000]; rep(i, n) cin >> h[i]; int dp[n]; rep(i, n) dp[i] = 1e9; dp[0] = 0; 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])); } cout << dp[n - 1] << endl; }
replace
9
10
9
10
0
p03161
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, i, k, c = 0, j; cin >> n >> k; int a[n]; long long dp[n]; for (i = 0; i < n; i++) { cin >> a[i]; } dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (i = 2; i <= k; i++) { c = 0; for (j = i; j >= 1; j--) { if (j == i) { c = dp[j - 1] + abs(a[i] - a[j - 1]); } else { if (dp[j - 1] + abs(a[i] - a[j - 1]) < c) { c = dp[j - 1] + abs(a[i] - a[j - 1]); } } } dp[i] = c; } for (i = k + 1; i < n; i++) { c = 0; for (j = i; j > i - k; j--) { if (j == i) { c = dp[j - 1] + abs(a[i] - a[j - 1]); } else { if (dp[j - 1] + abs(a[i] - a[j - 1]) < c) { c = dp[j - 1] + abs(a[i] - a[j - 1]); } } } dp[i] = c; } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, i, k, c = 0, j; cin >> n >> k; int a[n]; long long dp[n]; for (i = 0; i < n; i++) { cin >> a[i]; } dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (i = 2; i <= min(k, n - 1); i++) { c = 0; for (j = i; j >= 1; j--) { if (j == i) { c = dp[j - 1] + abs(a[i] - a[j - 1]); } else { if (dp[j - 1] + abs(a[i] - a[j - 1]) < c) { c = dp[j - 1] + abs(a[i] - a[j - 1]); } } } dp[i] = c; } for (i = k + 1; i < n; i++) { c = 0; for (j = i; j > i - k; j--) { if (j == i) { c = dp[j - 1] + abs(a[i] - a[j - 1]); } else { if (dp[j - 1] + abs(a[i] - a[j - 1]) < c) { c = dp[j - 1] + abs(a[i] - a[j - 1]); } } } dp[i] = c; } cout << dp[n - 1]; return 0; }
replace
12
13
12
13
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 endl "\n" #define int long long int32_t main() { IOS #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n, k; cin >> n >> k; int a[n], dp[n]; for (int i = 0; i < n; i++) cin >> a[i]; dp[0] = 0; for (int i = 1; i < n; i++) dp[i] = INT_MAX; for (int i = 1; i < n; i++) for (int j = 1; j <= min(k, i); j++) dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j])); cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl "\n" #define int long long int32_t main() { IOS // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int n, k; cin >> n >> k; int a[n], dp[n]; for (int i = 0; i < n; i++) cin >> a[i]; dp[0] = 0; for (int i = 1; i < n; i++) dp[i] = INT_MAX; for (int i = 1; i < n; i++) for (int j = 1; j <= min(k, i); j++) dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j])); cout << dp[n - 1]; return 0; }
replace
10
15
10
16
-11