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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02584 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"
int main() {
long long X, K, D;
cin >> X >> K >> D;
while (1) {
if ((abs(X) <= D && K % 2 == 0) || K == 0) {
break;
}
if (X < 0)
X += D;
else
X -= D;
--K;
}
cout << abs(X) << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"
int main() {
long long X, K, D;
cin >> X >> K >> D;
while (1) {
if ((abs(X) <= D && K % 2 == 0) || K == 0) {
break;
}
if (X < 0) {
if (D <= abs(X)) {
long long buf = min(abs(X) / D, K);
K -= buf;
X = X + buf * D;
} else {
X += D;
--K;
}
} else {
if (D <= X) {
long long buf = min(X / D, K);
K -= buf;
X = X - buf * D;
} else {
X -= D;
--K;
}
}
}
cout << abs(X) << endl;
return 0;
} | replace | 27 | 32 | 27 | 46 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
long x, k, d;
cin >> x >> k >> d;
long kk = abs(x) + d; // 距離
long nn = 0; // 何回でアウトしたか
for (long i = 1; i <= k; i++) {
if (x > 0) {
x -= d;
} else {
x += d;
}
if (kk >= abs(x - 0)) {
kk = abs(x - 0);
nn = i;
} else {
break;
}
}
long nokori = k - nn;
if (nokori % 2 == 0)
cout << kk;
else
cout << abs(kk - d);
return 0;
} | #include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
long x, k, d;
cin >> x >> k >> d;
long kk = abs(x) + d; // 距離
long nn = 0; // 何回でアウトしたか
nn = abs(x) / d;
if (nn > k)
nn = k;
kk = abs(x) - nn * d;
long nokori = k - nn;
if (nokori % 2 == 0)
cout << kk;
else
cout << abs(kk - d);
return 0;
} | replace | 13 | 26 | 13 | 19 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define F first
#define S second
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long double x, k, d;
cin >> x >> k >> d;
ll y = k;
if (y % 2 != 0) {
long double r = (k + 1) / 2;
long double l = (-1) * (r - 1);
long double temp = l;
long double mid;
while (l < r) {
mid = (ll)((l + r) / 2);
if ((2 * mid - 1) * d + x >= 0) {
r = mid;
} else {
l = mid + 1;
}
}
if (l == temp) {
cout << (ll)abs(x + (2 * l - 1) * d) << endl;
} else {
cout << (ll)min(abs(x + (2 * l - 1) * d), abs(x + (2 * l - 3) * d))
<< endl;
}
} else {
long double r = k / 2;
long double l = (-1) * r;
long double temp = l, mid;
while (l < r) {
mid = floor(((l + r) / 2));
if (2 * mid * d + x >= 0) {
r = mid;
} else {
l = mid + 1;
}
}
if (l == temp) {
cout << (ll)abs(x + 2 * l * d) << endl;
} else {
cout << (ll)min(abs(x + 2 * l * d), abs(x + (2 * l - 2) * d)) << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define F first
#define S second
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long double x, k, d;
cin >> x >> k >> d;
ll y = k;
if (y % 2 != 0) {
long double r = (k + 1) / 2;
long double l = (-1) * (r - 1);
long double temp = l;
long double mid;
while (l < r) {
mid = floor((l + r) / 2);
if ((2 * mid - 1) * d + x >= 0) {
r = mid;
} else {
l = mid + 1;
}
}
if (l == temp) {
cout << (ll)abs(x + (2 * l - 1) * d) << endl;
} else {
cout << (ll)min(abs(x + (2 * l - 1) * d), abs(x + (2 * l - 3) * d))
<< endl;
}
} else {
long double r = k / 2;
long double l = (-1) * r;
long double temp = l, mid;
while (l < r) {
mid = floor(((l + r) / 2));
if (2 * mid * d + x >= 0) {
r = mid;
} else {
l = mid + 1;
}
}
if (l == temp) {
cout << (ll)abs(x + 2 * l * d) << endl;
} else {
cout << (ll)min(abs(x + 2 * l * d), abs(x + (2 * l - 2) * d)) << endl;
}
}
}
| replace | 19 | 20 | 19 | 20 | TLE | |
p02584 | Python | Runtime Error | X, K, D = map(int, input().split())
def f(C):
NX = float("inf")
if C > K:
return NX
if K % 2 == 0:
if C % 2 == 0:
NX = abs(abs(X) - C * D)
else:
NX = abs(abs(X) - (C + 1) * D)
if C > 0:
NX = min(NX, abs(abs(X) - (C - 1) * D))
else:
if C % 2 == 0:
NX = abs(NX, abs(X) - (C + 1) * D)
if C > 0:
NX = min(NX, abs(abs(X) - (C - 1) * D))
else:
NX = abs(abs(X) - C * D)
return NX
C = min(abs(X) // D, K)
print(min(f(C), f(C + 1)))
| X, K, D = map(int, input().split())
def f(C):
NX = float("inf")
if C > K:
return NX
if K % 2 == 0:
if C % 2 == 0:
NX = abs(abs(X) - C * D)
else:
NX = abs(abs(X) - (C + 1) * D)
if C > 0:
NX = min(NX, abs(abs(X) - (C - 1) * D))
else:
if C % 2 == 0:
NX = abs(abs(X) - (C + 1) * D)
if C > 0:
NX = min(NX, abs(abs(X) - (C - 1) * D))
else:
NX = abs(abs(X) - C * D)
return NX
C = min(abs(X) // D, K)
print(min(f(C), f(C + 1)))
| replace | 17 | 18 | 17 | 18 | 0 | |
p02584 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "/home/laxsuryavanshi/Desktop/cp/trace.hpp"
#else
#define trace(...)
#endif // LOCAL_DEBUG
int32_t main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
#ifdef LOCAL_INPUT
freopen("in", "r", stdin);
#endif
typedef long long ll;
ll x, k, d;
cin >> x >> k >> d;
if (k / abs(x) * d > 1) {
k -= (abs(x) / d);
auto nearest = abs(x) % d;
if (nearest == 0) {
if (k % 2 == 0) {
cout << 0;
return 0;
} else {
cout << d;
return 0;
}
}
if (x < 0)
nearest = -nearest;
if (k % 2 == 0)
x = nearest;
else {
if (x < 0) {
trace(1);
x = (nearest + d);
} else {
trace(2, x, d);
x = (nearest - d);
}
}
trace(nearest);
} else {
if (x > 0ll)
x -= k * d;
else
x += k * d;
}
cout << abs(x);
}
| #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "/home/laxsuryavanshi/Desktop/cp/trace.hpp"
#else
#define trace(...)
#endif // LOCAL_DEBUG
int32_t main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
#ifdef LOCAL_INPUT
freopen("in", "r", stdin);
#endif
typedef long long ll;
ll x, k, d;
cin >> x >> k >> d;
if (static_cast<double>(k) / abs(x) * d > 1.) {
k -= (abs(x) / d);
auto nearest = abs(x) % d;
if (nearest == 0) {
if (k % 2 == 0) {
cout << 0;
return 0;
} else {
cout << d;
return 0;
}
}
if (x < 0)
nearest = -nearest;
if (k % 2 == 0)
x = nearest;
else {
if (x < 0) {
trace(1);
x = (nearest + d);
} else {
trace(2, x, d);
x = (nearest - d);
}
}
trace(nearest);
} else {
if (x > 0ll)
x -= k * d;
else
x += k * d;
}
cout << abs(x);
}
| replace | 19 | 20 | 19 | 20 | 0 | |
p02584 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
constexpr long long INFLL = 1e18;
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll x, k, d;
cin >> x >> k >> d;
map<ll, ll> check;
bool c = false;
check[x] = k;
if (abs(x) / d > 0) {
ll count = abs(x) / d;
if (k >= count) {
if (x > 0) {
x = x - d * count;
} else {
x = x + d * count;
}
k -= count;
}
}
while (k > 0) {
if (x > 0) {
x -= d;
} else {
x += d;
}
if (check[x] == 0) {
check[x] = k;
} else {
c = true;
k--;
break;
}
k--;
}
if (c) {
if (k % 2 == 0) {
cout << abs(x) << endl;
} else {
if (x > 0) {
x -= d;
} else {
x += d;
}
cout << abs(x) << endl;
}
} else {
cout << abs(x) << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
constexpr long long INFLL = 1e18;
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll x, k, d;
cin >> x >> k >> d;
map<ll, ll> check;
bool c = false;
check[x] = k;
if (abs(x) / d > 0) {
ll count = abs(x) / d;
if (k >= count) {
if (x > 0) {
x = x - d * count;
} else {
x = x + d * count;
}
k -= count;
} else {
if (x > 0) {
x = x - d * k;
} else {
x = x + d * k;
}
k = 0;
}
}
while (k > 0) {
if (x > 0) {
x -= d;
} else {
x += d;
}
if (check[x] == 0) {
check[x] = k;
} else {
c = true;
k--;
break;
}
k--;
}
if (c) {
if (k % 2 == 0) {
cout << abs(x) << endl;
} else {
if (x > 0) {
x -= d;
} else {
x += d;
}
cout << abs(x) << endl;
}
} else {
cout << abs(x) << endl;
}
return 0;
} | insert | 33 | 33 | 33 | 40 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long a, d, c;
cin >> a >> d >> c;
for (long long i = 0; i < d; i++) {
if (abs(a) <= c / 2) {
i += ((d - i - 1) / 2) * 2;
}
if (a > 0)
a -= c;
else
a += c;
}
cout << abs(a) << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long a, d, c;
cin >> a >> d >> c;
a = abs(a);
long long step = min(d, a / c);
d -= step;
a -= step * c;
if (d % 2 == 0)
cout << a << endl;
else
cout << c - a << endl;
} | replace | 6 | 16 | 6 | 14 | TLE | |
p02584 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t X, K, D;
cin >> X >> K >> D;
if (X < 0) {
X = -X;
}
// D未満になるまでにかかる回数
int64_t num = X / D;
assert(num <= K);
if (num >= K) {
cout << X - D * K << endl;
return 0;
}
// D未満になったときの値
int64_t x = X - D * num;
// 残り操作回数
int64_t rem = K - num;
if (rem % 2 == 0) {
cout << x << endl;
} else {
cout << D - x << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t X, K, D;
cin >> X >> K >> D;
if (X < 0) {
X = -X;
}
// D未満になるまでにかかる回数
int64_t num = X / D;
if (num >= K) {
cout << X - D * K << endl;
return 0;
}
// D未満になったときの値
int64_t x = X - D * num;
// 残り操作回数
int64_t rem = K - num;
if (rem % 2 == 0) {
cout << x << endl;
} else {
cout << D - x << endl;
}
} | delete | 12 | 13 | 12 | 12 | 0 | |
p02584 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll ABS(ll a) {
if (a < 0)
return -a;
else
return a;
}
int main() {
ll X, K, D;
cin >> X >> K >> D;
for (ll i = 0; i < K;) {
if (ABS(X + D) > ABS(X - D)) {
X = X - D;
} else
X = X + D;
i++;
if (ABS(X) <= D) {
if ((K - i) % 2 == 0)
break;
else {
if (ABS(X + D) > ABS(X - D)) {
X = X - D;
} else
X = X + D;
break;
}
}
}
cout << ABS(X) << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll ABS(ll a) {
if (a < 0)
return -a;
else
return a;
}
int main() {
ll X, K, D;
cin >> X >> K >> D;
ll i = min(ABS(X) / D, K);
X = ABS(X) - D * i;
for (; i < K;) {
if (ABS(X + D) > ABS(X - D)) {
X = X - D;
} else
X = X + D;
i++;
if (ABS(X) <= D) {
if ((K - i) % 2 == 0)
break;
else {
if (ABS(X + D) > ABS(X - D)) {
X = X - D;
} else
X = X + D;
break;
}
}
}
cout << ABS(X) << endl;
return 0;
}
| replace | 21 | 22 | 21 | 24 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define endl "\n"
typedef long long ll;
const double pi = 3.14159265358979323846;
int ctoi(const char c) {
if ('0' <= c && c <= '9')
return (c - '0');
return -1;
}
vector<int> input(int n) {
vector<int> vec(n);
for (int i = 0; i < n; i++) {
cin >> vec.at(i);
}
return vec;
}
void output(vector<int> vec) {
for (int i = 0; i < int(vec.size()); i++) {
cout << vec[i] << " ";
}
return;
}
vector<vector<int>> input(int n, int m) {
vector<vector<int>> table(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> table.at(i).at(j);
}
}
return table;
}
void output(vector<vector<int>> table) {
for (int i = 0; i < int(table.size()); i++) {
for (int j = 0; j < int(table.at(0).size()); j++) {
cout << table.at(i).at(j) << " ";
}
cout << endl;
}
}
long long perm(int n, int r) {
if (n < r) {
cout << "error" << endl;
return 0;
}
long long perm = 1;
for (int i = n; i > n - r; i--) {
perm *= i;
}
return perm;
}
long long comb(int n, int r) {
if (n < r) {
cout << "error" << endl;
return 0;
}
long long comb = perm(n, r);
for (int i = r; i > 0; i--) {
comb /= i;
}
return comb;
}
long long homo(int n, int r) { return comb(n + r - 1, n - 1); }
long long fact(int n) {
long long fact = 1;
for (int i = n; i > 0; i--) {
fact *= i;
}
return fact;
}
int gcd(int a, int b) {
if (a % b == 0) {
return (b);
} else {
return (gcd(b, a % b));
}
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
bool isprime(int n) {
if (n < 2)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i <= sqrt(n); i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
vector<long long> divisors(long long N) {
vector<long long> res;
for (long long i = 1; i * i <= N; ++i) {
if (N % i == 0) {
res.push_back(i);
// 重複しないならば i の相方である N/i も push
if (N / i != i)
res.push_back(N / i);
}
}
// 小さい順に並び替える
sort(res.begin(), res.end());
return res;
/*long long N;
cin >> N;
vector<long long> res = divisors(N);
for (int i = 0; i < res.size(); ++i) {
cout << res[i] << " ";
}
cout << endl;*/
}
vector<pair<long long, long long>> prime_factorize(long long N) {
vector<pair<long long, long long>> res;
for (long long a = 2; a * a <= N; ++a) {
if (N % a != 0)
continue;
long long ex = 0;
while (N % a == 0) {
++ex;
N /= a;
}
res.push_back({a, ex});
}
if (N != 1)
res.push_back({N, 1});
return res;
}
void Yes(bool f) {
if (f) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
void YES(bool f) {
if (f) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
bool leapyear(int y) {
// 閏年かどうかを判定
if (y % 4 == 0) {
if (y % 100 == 0) {
if (y % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
tuple<int, int, int> dayplus(int y, int m, int d) {
// 次の日を返す
// 31日まである月
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
if (d != 31) {
d++;
return make_tuple(y, m, d);
} else {
d = 1;
if (m == 12) {
m = 1;
y++;
return make_tuple(y, m, d);
} else {
m++;
return make_tuple(y, m, d);
}
}
}
// 30日まである月
else if (m == 4 || m == 6 || m == 9 || m == 11) {
if (d != 30) {
d++;
return make_tuple(y, m, d);
} else {
d = 1;
m++;
return make_tuple(y, m, d);
}
}
// 2月
else {
// 閏年の場合
if (leapyear(y)) {
if (d != 29) {
d++;
return make_tuple(y, m, d);
} else {
d = 1;
m++;
return make_tuple(y, m, d);
}
}
// 閏年ではない場合
else {
if (d != 28) {
d++;
return make_tuple(y, m, d);
} else {
d = 1;
m++;
return make_tuple(y, m, d);
}
}
}
}
tuple<int, int, int> monplus(int y, int m, int d) {
// 月を一つ先に、日を一日にする
d = 1;
if (m == 12) {
m = 1;
y++;
return make_tuple(y, m, d);
} else {
m++;
return make_tuple(y, m, d);
}
}
bool trican(int a, int b, int c) {
if (a == b || b == c || c == a) {
return false;
}
if (a + b > c && b + c > a && c + a > b) {
return true;
} else {
return false;
}
}
int main() {
ll x, k, d;
cin >> x >> k >> d;
for (int i = 0; i < k; ++i) {
x = min(abs(x + d), abs(x - d));
}
cout << x << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define endl "\n"
typedef long long ll;
const double pi = 3.14159265358979323846;
int ctoi(const char c) {
if ('0' <= c && c <= '9')
return (c - '0');
return -1;
}
vector<int> input(int n) {
vector<int> vec(n);
for (int i = 0; i < n; i++) {
cin >> vec.at(i);
}
return vec;
}
void output(vector<int> vec) {
for (int i = 0; i < int(vec.size()); i++) {
cout << vec[i] << " ";
}
return;
}
vector<vector<int>> input(int n, int m) {
vector<vector<int>> table(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> table.at(i).at(j);
}
}
return table;
}
void output(vector<vector<int>> table) {
for (int i = 0; i < int(table.size()); i++) {
for (int j = 0; j < int(table.at(0).size()); j++) {
cout << table.at(i).at(j) << " ";
}
cout << endl;
}
}
long long perm(int n, int r) {
if (n < r) {
cout << "error" << endl;
return 0;
}
long long perm = 1;
for (int i = n; i > n - r; i--) {
perm *= i;
}
return perm;
}
long long comb(int n, int r) {
if (n < r) {
cout << "error" << endl;
return 0;
}
long long comb = perm(n, r);
for (int i = r; i > 0; i--) {
comb /= i;
}
return comb;
}
long long homo(int n, int r) { return comb(n + r - 1, n - 1); }
long long fact(int n) {
long long fact = 1;
for (int i = n; i > 0; i--) {
fact *= i;
}
return fact;
}
int gcd(int a, int b) {
if (a % b == 0) {
return (b);
} else {
return (gcd(b, a % b));
}
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
bool isprime(int n) {
if (n < 2)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i <= sqrt(n); i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
vector<long long> divisors(long long N) {
vector<long long> res;
for (long long i = 1; i * i <= N; ++i) {
if (N % i == 0) {
res.push_back(i);
// 重複しないならば i の相方である N/i も push
if (N / i != i)
res.push_back(N / i);
}
}
// 小さい順に並び替える
sort(res.begin(), res.end());
return res;
/*long long N;
cin >> N;
vector<long long> res = divisors(N);
for (int i = 0; i < res.size(); ++i) {
cout << res[i] << " ";
}
cout << endl;*/
}
vector<pair<long long, long long>> prime_factorize(long long N) {
vector<pair<long long, long long>> res;
for (long long a = 2; a * a <= N; ++a) {
if (N % a != 0)
continue;
long long ex = 0;
while (N % a == 0) {
++ex;
N /= a;
}
res.push_back({a, ex});
}
if (N != 1)
res.push_back({N, 1});
return res;
}
void Yes(bool f) {
if (f) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
void YES(bool f) {
if (f) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
bool leapyear(int y) {
// 閏年かどうかを判定
if (y % 4 == 0) {
if (y % 100 == 0) {
if (y % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
tuple<int, int, int> dayplus(int y, int m, int d) {
// 次の日を返す
// 31日まである月
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
if (d != 31) {
d++;
return make_tuple(y, m, d);
} else {
d = 1;
if (m == 12) {
m = 1;
y++;
return make_tuple(y, m, d);
} else {
m++;
return make_tuple(y, m, d);
}
}
}
// 30日まである月
else if (m == 4 || m == 6 || m == 9 || m == 11) {
if (d != 30) {
d++;
return make_tuple(y, m, d);
} else {
d = 1;
m++;
return make_tuple(y, m, d);
}
}
// 2月
else {
// 閏年の場合
if (leapyear(y)) {
if (d != 29) {
d++;
return make_tuple(y, m, d);
} else {
d = 1;
m++;
return make_tuple(y, m, d);
}
}
// 閏年ではない場合
else {
if (d != 28) {
d++;
return make_tuple(y, m, d);
} else {
d = 1;
m++;
return make_tuple(y, m, d);
}
}
}
}
tuple<int, int, int> monplus(int y, int m, int d) {
// 月を一つ先に、日を一日にする
d = 1;
if (m == 12) {
m = 1;
y++;
return make_tuple(y, m, d);
} else {
m++;
return make_tuple(y, m, d);
}
}
bool trican(int a, int b, int c) {
if (a == b || b == c || c == a) {
return false;
}
if (a + b > c && b + c > a && c + a > b) {
return true;
} else {
return false;
}
}
int main() {
ll x, k, d;
cin >> x >> k >> d;
x = abs(x);
if (x / d >= k) {
cout << x - d * k << endl;
} else {
if ((x / d) % 2 == k % 2) {
cout << abs(x % d) << endl;
} else {
cout << abs(x % d - d) << endl;
}
}
}
| replace | 245 | 250 | 245 | 256 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef __int128 LL;
inline __int128 read() {
__int128 x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void print(__int128 x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9)
print(x / 10);
putchar(x % 10 + '0');
}
int main() {
LL x, k, d;
while (x = read(), k = read(), d = read()) {
if (x < 0)
x = -x;
if (x >= 0) {
if ((x - k * d) >= 0) {
print(x - k * d);
} else {
int t = (x / d);
if ((k - t) % 2)
print(-(x - d * t - d));
else
print(x - d * t);
}
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef __int128 LL;
inline __int128 read() {
__int128 x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void print(__int128 x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9)
print(x / 10);
putchar(x % 10 + '0');
}
int main() {
LL x, k, d;
x = read(), k = read(), d = read();
if (x < 0)
x = -x;
if (x >= 0) {
if ((x - k * d) >= 0) {
print(x - k * d);
} else {
int t = (x / d);
if ((k - t) % 2)
print(-(x - d * t - d));
else
print(x - d * t);
}
}
return 0;
} | replace | 29 | 42 | 29 | 42 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
int MAX_INT = 1e9;
// 最大公約数
long long gcd(long long a, long long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// 最小公倍数
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long X, K, D;
cin >> X >> K >> D;
X = abs(X);
long long num = abs(X) / D;
if (K >= num) {
K -= num;
X -= num * D;
}
for (K; K > 0; --K) {
if (X <= 0) {
break;
}
X -= D;
}
if (K % 2 == 0) {
cout << abs(X) << endl;
} else {
cout << min(abs(X - D), abs(X + D)) << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
int MAX_INT = 1e9;
// 最大公約数
long long gcd(long long a, long long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// 最小公倍数
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long X, K, D;
cin >> X >> K >> D;
X = abs(X);
long long num = abs(X) / D;
if (K >= num) {
K -= num;
X -= num * D;
} else {
X -= K * D;
K = 0;
}
for (K; K > 0; --K) {
if (X <= 0) {
break;
}
X -= D;
}
if (K % 2 == 0) {
cout << abs(X) << endl;
} else {
cout << min(abs(X - D), abs(X + D)) << endl;
}
return 0;
}
| insert | 30 | 30 | 30 | 33 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <new>
#include <queue>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define loop(i, x, z) for (int i = x; i < z; i++)
using ll = unsigned long long int;
#define mod 1000000007
/*bool cmp(pair<ll,ll>a,pair<ll,ll>b){
return a.second<b.second;
}*/
int main() {
ll x, k, d;
cin >> x >> k >> d;
x = llabs(x);
while (k > 0 && llabs(x) > d / 2) {
k--;
x = x - d;
}
if (k % 2 == 0) {
cout << llabs(x);
} else {
x = min(llabs(x - d), llabs(x + d));
cout << x;
}
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <new>
#include <queue>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define loop(i, x, z) for (int i = x; i < z; i++)
using ll = unsigned long long int;
#define mod 1000000007
/*bool cmp(pair<ll,ll>a,pair<ll,ll>b){
return a.second<b.second;
}*/
int main() {
ll x, k, d;
cin >> x >> k >> d;
x = llabs(x);
if (d == 1) {
if (x >= k) {
x = x - k;
k = 0;
} else {
k = k - x;
x = 0;
}
}
while (k > 0 && llabs(x) > d / 2) {
k--;
x = x - d;
}
if (k % 2 == 0) {
cout << llabs(x);
} else {
x = min(llabs(x - d), llabs(x + d));
cout << x;
}
return 0;
}
| insert | 24 | 24 | 24 | 33 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define _start \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define pb push_back
#define ss second
#define ff first
#define ll long long
#define int long long
#define mk make_pair
#define ld long double
#define vi vector<int>
#define pi pair<int, int>
#define mod 1000000007
#define pqg priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define forn(n) for (int i = 0; i < n; i++)
#define w(t) \
int t; \
cin >> t; \
while (t--)
#define kick(t) \
int t; \
cin >> t; \
for (int p = 0; p < t; p++)
// typedef tree<ll, null_type, less<ll>, rb_tree_tag,
// tree_order_statistics_node_update> oset;
ll powmod(ll base, ll exp, ll MOD = mod) {
ll res = 1;
while (exp > 0) {
if (exp % 2 == 1)
res = (res * base) % MOD;
base = (base * base) % MOD;
exp /= 2;
}
return (res % MOD);
}
ll mul_inv(ll a, ll b = mod) {
ll t1 = a, t2 = b, t3, v1 = 1, v2 = 0, v3;
while (t2 != 1) {
ll x = t1 / t2;
t3 = t1 - x * t2;
v3 = v1 - x * v2;
t1 = t2, t2 = t3;
v1 = v2, v2 = v3;
}
return (v2 + b) % b;
}
int32_t main() {
_start
int x,
k, d;
cin >> x >> k >> d;
x = abs(x);
while (x - d >= 0 && k) {
x -= d;
k--;
}
if (!k)
cout << x << endl;
else {
if (k % 2 == 0) {
cout << x << endl;
} else {
cout << min(x + d, abs(x - d)) << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define _start \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define pb push_back
#define ss second
#define ff first
#define ll long long
#define int long long
#define mk make_pair
#define ld long double
#define vi vector<int>
#define pi pair<int, int>
#define mod 1000000007
#define pqg priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define forn(n) for (int i = 0; i < n; i++)
#define w(t) \
int t; \
cin >> t; \
while (t--)
#define kick(t) \
int t; \
cin >> t; \
for (int p = 0; p < t; p++)
// typedef tree<ll, null_type, less<ll>, rb_tree_tag,
// tree_order_statistics_node_update> oset;
ll powmod(ll base, ll exp, ll MOD = mod) {
ll res = 1;
while (exp > 0) {
if (exp % 2 == 1)
res = (res * base) % MOD;
base = (base * base) % MOD;
exp /= 2;
}
return (res % MOD);
}
ll mul_inv(ll a, ll b = mod) {
ll t1 = a, t2 = b, t3, v1 = 1, v2 = 0, v3;
while (t2 != 1) {
ll x = t1 / t2;
t3 = t1 - x * t2;
v3 = v1 - x * v2;
t1 = t2, t2 = t3;
v1 = v2, v2 = v3;
}
return (v2 + b) % b;
}
int32_t main() {
_start
int x,
k, d;
cin >> x >> k >> d;
x = abs(x);
int temp = x / d;
if (temp >= k) {
x -= (k * d);
k = 0;
} else {
x -= (temp * d);
k -= temp;
}
if (!k)
cout << x << endl;
else {
if (k % 2 == 0) {
cout << x << endl;
} else {
cout << min(x + d, abs(x - d)) << endl;
}
}
} | replace | 62 | 66 | 62 | 69 | TLE | |
p02584 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <unistd.h>
#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define dame \
{ \
puts("-1"); \
return 0; \
}
#define show(x) cerr << #x << " = " << x << endl;
using namespace std;
using ll = long long;
using ld = long double;
using pl = pair<ll, ll>;
ll LINF = 1000000000000000000;
typedef vector<ll> vl;
typedef vector<pl> vp;
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;
}
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int root(int x) {
if (d[x] < 0)
return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -d[root(x)]; }
int factor_size() {
int ans = 0;
rep(i, d.size()) {
if (d[i] < 0) {
ans++;
}
}
return ans;
}
};
ll d[201][201];
void warshall_floyd(ll n) {
rep(k, n) {
rep(i, n) {
rep(j, n) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); }
}
}
}
struct edge {
ll to, cost;
};
struct dijkstra {
ll V;
vector<vector<edge>> G;
vector<ll> d;
dijkstra(ll n) { init(n); }
void init(ll n) {
V = n;
G.resize(V);
d.resize(V);
rep(i, V) { d[i] = LINF; }
}
void add_edge(ll s, ll t, ll cost) {
edge e;
e.to = t, e.cost = cost;
G[s].push_back(e);
}
void run(ll s) {
rep(i, V) { d[i] = LINF; }
d[s] = 0;
priority_queue<pl, vector<pl>, greater<pl>> que;
que.push(pl(0, s));
while (!que.empty()) {
pl p = que.top();
que.pop();
ll v = p.second;
if (d[v] < p.first)
continue;
for (auto e : G[v]) {
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(pl(d[e.to], e.to));
}
}
}
}
};
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const ll mod = 1000000007;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
} comb(200005);
// Sieve of Eratosthenes
// https://youtu.be/UTVg7wzMWQc?t=2774
struct Sieve {
int n;
vector<int> f, primes;
Sieve(int n = 1) : n(n), f(n + 1) {
f[0] = f[1] = -1;
for (ll i = 2; i <= n; ++i) {
if (f[i])
continue;
primes.push_back(i);
f[i] = i;
for (ll j = i * i; j <= n; j += i) {
if (!f[j])
f[j] = i;
}
}
}
bool isPrime(int x) { return f[x] == x; }
vector<int> factorList(int x) {
vector<int> res;
while (x != 1) {
res.push_back(f[x]);
x /= f[x];
}
return res;
}
vector<pl> factor(int x) {
vector<int> fl = factorList(x);
if (fl.size() == 0)
return {};
vector<pl> res(1, pl(fl[0], 0));
for (int p : fl) {
if (res.back().first == p) {
res.back().second++;
} else {
res.emplace_back(p, 1);
}
}
return res;
}
vector<pair<ll, int>> factor(ll x) {
vector<pair<ll, int>> res;
for (int p : primes) {
int y = 0;
while (x % p == 0)
x /= p, ++y;
if (y != 0)
res.emplace_back(p, y);
}
if (x != 1)
res.emplace_back(x, 1);
return res;
}
};
int main() {
ll n, k;
cin >> n >> k;
vector<ll> p(n);
vector<ll> c(n);
rep(i, n) cin >> p[i];
rep(i, n) cin >> c[i];
vector<bool> visited(n, 0);
ll ans = -1e10;
ll idx = 0;
bool ret = false;
rep(i, n) {
if (visited[i])
continue;
vector<ll> s;
s.pb(0);
ll idx = i;
while (true) {
s.pb(s[s.size() - 1] + c[p[idx] - 1]);
visited[idx] = true;
idx = p[idx] - 1;
if (idx == i)
break;
}
ll x = s.size() - 1;
rep1(j, x) s.pb(s[x] + s[j]);
// rep(j, s.size()) show(s[j]);
ll tmp1 = -1e10;
ll tmp2 = -1e10;
ll hasu = k % x;
ll case1 = k / x;
if (hasu == 0)
hasu = x;
ll threshold = hasu;
// if(case1==0) threshold=hasu;
// else threshold = 100000000000000000;
// xrep(j, s.size()) show(s[j]);
for (ll l = 0; l < s.size(); l++) {
for (ll r = l + 1; r <= x + l; r++) {
if (r - l > threshold or l == r or r >= s.size())
continue;
// show(s[r] - s[l]);
// show(s.size());
// show(r);
// show(l);
if (tmp1 < s[r] - s[l])
tmp1 = s[r] - s[l];
}
}
for (ll l = 0; l < s.size(); l++) {
for (ll r = l + 1; r <= x + l; r++) {
// show(s[r] - s[l]);
if (r - l <= threshold or l == r or r >= s.size())
continue;
if (tmp2 < s[r] - s[l])
tmp2 = s[r] - s[l];
}
}
show(tmp1);
show(tmp2);
if (s[x] < 0) {
chmax(ans, tmp1);
if (k / x > 0)
chmax(ans, tmp2);
} else {
if (hasu != x)
tmp1 += k / x * s[x];
else
tmp1 += (k / x - 1) * s[x];
tmp2 += (k / x - 1) * s[x];
chmax(ans, tmp1);
if (k / x > 0)
chmax(ans, tmp2);
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <unistd.h>
#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define dame \
{ \
puts("-1"); \
return 0; \
}
#define show(x) cerr << #x << " = " << x << endl;
using namespace std;
using ll = long long;
using ld = long double;
using pl = pair<ll, ll>;
ll LINF = 1000000000000000000;
typedef vector<ll> vl;
typedef vector<pl> vp;
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;
}
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int root(int x) {
if (d[x] < 0)
return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -d[root(x)]; }
int factor_size() {
int ans = 0;
rep(i, d.size()) {
if (d[i] < 0) {
ans++;
}
}
return ans;
}
};
ll d[201][201];
void warshall_floyd(ll n) {
rep(k, n) {
rep(i, n) {
rep(j, n) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); }
}
}
}
struct edge {
ll to, cost;
};
struct dijkstra {
ll V;
vector<vector<edge>> G;
vector<ll> d;
dijkstra(ll n) { init(n); }
void init(ll n) {
V = n;
G.resize(V);
d.resize(V);
rep(i, V) { d[i] = LINF; }
}
void add_edge(ll s, ll t, ll cost) {
edge e;
e.to = t, e.cost = cost;
G[s].push_back(e);
}
void run(ll s) {
rep(i, V) { d[i] = LINF; }
d[s] = 0;
priority_queue<pl, vector<pl>, greater<pl>> que;
que.push(pl(0, s));
while (!que.empty()) {
pl p = que.top();
que.pop();
ll v = p.second;
if (d[v] < p.first)
continue;
for (auto e : G[v]) {
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(pl(d[e.to], e.to));
}
}
}
}
};
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const ll mod = 1000000007;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
} comb(200005);
// Sieve of Eratosthenes
// https://youtu.be/UTVg7wzMWQc?t=2774
struct Sieve {
int n;
vector<int> f, primes;
Sieve(int n = 1) : n(n), f(n + 1) {
f[0] = f[1] = -1;
for (ll i = 2; i <= n; ++i) {
if (f[i])
continue;
primes.push_back(i);
f[i] = i;
for (ll j = i * i; j <= n; j += i) {
if (!f[j])
f[j] = i;
}
}
}
bool isPrime(int x) { return f[x] == x; }
vector<int> factorList(int x) {
vector<int> res;
while (x != 1) {
res.push_back(f[x]);
x /= f[x];
}
return res;
}
vector<pl> factor(int x) {
vector<int> fl = factorList(x);
if (fl.size() == 0)
return {};
vector<pl> res(1, pl(fl[0], 0));
for (int p : fl) {
if (res.back().first == p) {
res.back().second++;
} else {
res.emplace_back(p, 1);
}
}
return res;
}
vector<pair<ll, int>> factor(ll x) {
vector<pair<ll, int>> res;
for (int p : primes) {
int y = 0;
while (x % p == 0)
x /= p, ++y;
if (y != 0)
res.emplace_back(p, y);
}
if (x != 1)
res.emplace_back(x, 1);
return res;
}
};
int main() {
ll x, k, d;
cin >> x >> k >> d;
if (x < 0)
x *= (-1);
if (ld(x / d) > k) {
ll ans = x - k * d;
cout << ans << endl;
return 0;
}
ll y = x / d;
k -= y;
ll cd = x - y * d;
// if (x - y * d > d - (x - y * d)) {
// k--;
// cd = d - cd;
// }
ll ans;
if (k % 2 == 0)
ans = cd;
else
ans = d - cd;
cout << ans << endl;
return 0;
}
| replace | 246 | 338 | 246 | 273 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define printInt(a) printf("%d\n", a)
#define printll(a) printf("%lld\n", a)
#define printdbl(a) printf("%lf\n", a)
#define printYes() printf("Yes\n")
#define printNo() printf("No\n")
#define scanll(a) scanf("%lld", &a)
#define scanInt(a) scanf("%d", &a)
#define scan2Int(a, b) scanf("%d %d", &a, &b)
#define scan3Int(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define scan4Int(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
typedef long long ll;
const int INF = 1001001001;
using namespace std;
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
return;
}
int gdc(int a, int b) {
int r;
if (a < b)
swap(&a, &b);
r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
int factorial(int a) {
int ans = 1;
for (int i = 1; i <= a; ++i)
ans *= i;
return ans;
}
int main() {
ll x, k, d;
scanf("%lld %lld %lld", &x, &k, &d);
x = abs(x);
while (x > 0 && k > 0) {
x -= d;
k--;
}
d *= k % 2;
x = abs(x - d) < abs(x + d) ? abs(x - d) : abs(x + d);
printll(x);
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define printInt(a) printf("%d\n", a)
#define printll(a) printf("%lld\n", a)
#define printdbl(a) printf("%lf\n", a)
#define printYes() printf("Yes\n")
#define printNo() printf("No\n")
#define scanll(a) scanf("%lld", &a)
#define scanInt(a) scanf("%d", &a)
#define scan2Int(a, b) scanf("%d %d", &a, &b)
#define scan3Int(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define scan4Int(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
typedef long long ll;
const int INF = 1001001001;
using namespace std;
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
return;
}
int gdc(int a, int b) {
int r;
if (a < b)
swap(&a, &b);
r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
int factorial(int a) {
int ans = 1;
for (int i = 1; i <= a; ++i)
ans *= i;
return ans;
}
int main() {
ll x, k, d;
scanf("%lld %lld %lld", &x, &k, &d);
x = abs(x);
ll tmp = x / d;
if (tmp <= k) {
x -= d * tmp;
k -= tmp;
} else {
x -= d * k;
k = 0;
}
d *= k % 2;
x = abs(x - d) < abs(x + d) ? abs(x - d) : abs(x + d);
printll(x);
return 0;
} | replace | 44 | 47 | 44 | 51 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << " ; ";
err(++it, args...);
cout << endl;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------*/
/*Let's start.............*/
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define srt(v) sort(v.begin(), v.end())
#define inf 200000000000000000
#define mxn 100009
#define MOD 1000000007
#define pi acos(-1)
int main() {
// freopen("input.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
while (1) {
ll X, a, b;
cin >> X >> a >> b;
X = abs(X);
ll keep = (X / b);
if (a <= keep) {
cout << X - (a * b) << endl;
} else if (X == 0) {
if (a % 2 == 0)
cout << 0 << endl;
else
cout << b << endl;
} else {
ll ans = 0;
ll rem = X % b;
if (b % 2 == 0)
ans = b - rem;
else
ans = rem;
cout << ans << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << " ; ";
err(++it, args...);
cout << endl;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------*/
/*Let's start.............*/
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define srt(v) sort(v.begin(), v.end())
#define inf 200000000000000000
#define mxn 100009
#define MOD 1000000007
#define pi acos(-1)
int main() {
// freopen("input.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
ll X, a, b;
cin >> X >> a >> b;
X = abs(X);
ll keep = (X / b);
if (X == 0) {
if (a % 2 == 0)
cout << 0 << endl;
else
cout << b << endl;
} else if (a <= keep) {
cout << X - (a * b) << endl;
} else {
ll rem = (X - (X / b) * b);
ll keep = (b - rem);
ll peek = (X / b);
ll ans = 0;
if (peek % 2 == (a % 2))
ans = rem;
else
ans = keep;
cout << ans << endl;
}
return 0;
}
| replace | 34 | 55 | 34 | 56 | TLE | |
p02584 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
long long x;
long long k;
long long d;
cin >> x >> k >> d;
long long mostnear = abs(x) % d;
if (mostnear / d + k <= abs(x) / d) {
cout << abs(x) - (k * d);
} else if ((abs(k) % abs(x / d) == 1))
cout << mostnear;
else
cout << abs(mostnear - d);
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long x;
long long k;
long long d;
cin >> x >> k >> d;
long long mostnear = abs(x) % d;
if (mostnear / d + k <= abs(x) / d) {
cout << abs(x) - (k * d);
} else if (abs(x / d) % 2 == 0 && k % 2 == 0)
cout << mostnear;
else if (abs(x / d) % 2 == 1 && k % 2 == 1)
cout << mostnear;
else
cout << abs(mostnear - d);
} | replace | 10 | 11 | 10 | 13 | 0 | |
p02584 | C++ | Time Limit Exceeded | #include <cmath>
#include <ctype.h>
#include <iostream>
#include <limits.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
// 最大公約数
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
// 最小公倍数
int lcm(int a, int b) { return a / gcd(a, b) * b; }
// 素数判定
bool isPrime(int x) {
if (x < 2)
return 0;
else if (x == 2)
return 1;
if (x % 2 == 0)
return 0;
for (int i = 3; i * i <= x; i += 2) {
if (x % i == 0)
return 0;
}
return 1;
}
// 桁ごとの足し算
int digsum(int n) {
int res = 0;
while (n > 0) {
res += n % 10;
n /= 10;
}
return res;
}
// 約数列挙
vector<int> enum_div(int n) {
vector<int> ret;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
return ret;
}
// 累乗の計算とあまり,0で累乗そのまま出力
long long modpow(long long a, long long n, long long mod) {
if (mod == 0)
mod = __LONG_LONG_MAX__;
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll X;
ll K;
ll D;
int main() {
cin >> X >> K >> D;
for (int i = 0; i < K; ++i) {
ll X2;
if (X > 0)
X2 = X - D;
else
X2 = X + D;
if ((X2 <= 0 && 0 <= X) || (X <= 0 && 0 <= X2)) {
if ((K - 1 - i) % 2)
break;
else {
X = X2;
break;
}
}
X = X2;
}
cout << abs(X) << endl;
} | #include <cmath>
#include <ctype.h>
#include <iostream>
#include <limits.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
// 最大公約数
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
// 最小公倍数
int lcm(int a, int b) { return a / gcd(a, b) * b; }
// 素数判定
bool isPrime(int x) {
if (x < 2)
return 0;
else if (x == 2)
return 1;
if (x % 2 == 0)
return 0;
for (int i = 3; i * i <= x; i += 2) {
if (x % i == 0)
return 0;
}
return 1;
}
// 桁ごとの足し算
int digsum(int n) {
int res = 0;
while (n > 0) {
res += n % 10;
n /= 10;
}
return res;
}
// 約数列挙
vector<int> enum_div(int n) {
vector<int> ret;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
return ret;
}
// 累乗の計算とあまり,0で累乗そのまま出力
long long modpow(long long a, long long n, long long mod) {
if (mod == 0)
mod = __LONG_LONG_MAX__;
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll X;
ll K;
ll D;
int main() {
cin >> X >> K >> D;
X = abs(X);
ll num = X / D;
if (X % D > 0)
num++;
if (num > K)
X -= D * K;
else {
X -= D * num;
if ((K - num) % 2 == 1)
X += D;
}
cout << abs(X) << endl;
} | replace | 78 | 93 | 78 | 89 | TLE | |
p02584 | C++ | Time Limit Exceeded | // ABC 175 C
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int x, d, k, ans, fl, flfl, flflfl = 0, fm;
cin >> x >> k >> d;
fl = 1;
if (k % 2 == 0) {
fl = 0;
}
if (x < 0) {
x *= -1;
}
flfl = 0;
if (d >= x) {
flfl = 1;
}
// cout << flfl<<endl;
if (flfl == 0) {
int i = 1;
while (i >= 0) {
if (d * i >= x) {
fm = i - 1;
i = -2;
}
i++;
}
if (fm % 2 != 0) {
flflfl = 1;
}
// cout << fm <<endl;
if (fm <= k) {
if (fl == 0) {
if (flflfl == 0) {
ans = x - fm * d;
} else {
ans = (fm + 1) * d - x;
}
} else {
if (flflfl == 0) {
ans = (fm + 1) * d - x;
} else {
ans = x - fm * d;
}
}
} else {
ans = x - k * d;
}
} else {
if (fl == 0) {
ans = x;
} else {
if (x == d) {
ans = 0;
} else {
ans = d - x;
}
}
// cout << "haha";
}
cout << ans;
} | // ABC 175 C
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int x, d, k, ans, fl, flfl, flflfl = 0, fm;
cin >> x >> k >> d;
fl = 1;
if (k % 2 == 0) {
fl = 0;
}
if (x < 0) {
x *= -1;
}
flfl = 0;
if (d >= x) {
flfl = 1;
}
// cout << flfl<<endl;
if (flfl == 0) {
fm = x / d;
if (fm % 2 != 0) {
flflfl = 1;
}
// cout << fm <<endl;
if (fm <= k) {
if (fl == 0) {
if (flflfl == 0) {
ans = x - fm * d;
} else {
ans = (fm + 1) * d - x;
}
} else {
if (flflfl == 0) {
ans = (fm + 1) * d - x;
} else {
ans = x - fm * d;
}
}
} else {
ans = x - k * d;
}
} else {
if (fl == 0) {
ans = x;
} else {
if (x == d) {
ans = 0;
} else {
ans = d - x;
}
}
// cout << "haha";
}
cout << ans;
} | replace | 20 | 28 | 20 | 21 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <iostream>
long long abs(long long a) { return (a < 0) ? -a : a; }
int sgn(long long a) { return (a < 0) ? -1 : 1; }
int main() {
long long X, K, D;
std::cin >> X >> K >> D;
while (K > 0) {
if (abs(X - sgn(X) * D) < abs(X)) {
long long times = abs(X) / D;
if (K < times)
times = K;
K -= times;
X -= sgn(X) * times * D;
} else {
if (K > 1) {
K %= 2;
} else {
X -= sgn(X) * D;
K--;
}
}
}
std::cout << abs(X) << '\n';
return 0;
} | #include <iostream>
long long abs(long long a) { return (a < 0) ? -a : a; }
int sgn(long long a) { return (a < 0) ? -1 : 1; }
int main() {
long long X, K, D;
std::cin >> X >> K >> D;
while (K > 0) {
if (abs(X - sgn(X) * D) < abs(X)) {
long long times = abs(X) / D;
if (times == 0)
times = 1;
if (K < times)
times = K;
K -= times;
X -= sgn(X) * times * D;
} else {
if (K > 1) {
K %= 2;
} else {
X -= sgn(X) * D;
K--;
}
}
}
std::cout << abs(X) << '\n';
return 0;
} | insert | 13 | 13 | 13 | 15 | TLE | |
p02584 | C++ | Runtime Error | #include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define int long long
#define INF 1e18
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define endl "\n"
#define GCD(x, y) (__gcd((x), (y)))
#define LCM(x, y) (((x) / __gcd((x), (y))) * (y))
#define debug(x) cout << #x << " is " << (x) << endl
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define sp(ans, p) fixed << setprecision(p) << ans;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// constexpr int N=;
void solve() {
int in, t, d;
cin >> in >> t >> d;
int ff = 0;
if (in < 0) {
in *= -1;
ff = 1;
}
int mulp = in / d;
mulp = min(mulp, t);
int rem = (t - mulp) % 2;
int prefinal = in - d * mulp;
if (rem == 0) {
cout << prefinal << endl;
} else {
cout << min(abs(prefinal + d), abs(prefinal - d)) << endl;
}
// cout << << " " << (t - mulp) % 2 << endl;
return;
}
signed main() {
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// sieve();
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
} | #include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define int long long
#define INF 1e18
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define endl "\n"
#define GCD(x, y) (__gcd((x), (y)))
#define LCM(x, y) (((x) / __gcd((x), (y))) * (y))
#define debug(x) cout << #x << " is " << (x) << endl
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i, a, b) for (long long i = a; i < b; i++)
#define sp(ans, p) fixed << setprecision(p) << ans;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// constexpr int N=;
void solve() {
int in, t, d;
cin >> in >> t >> d;
int ff = 0;
if (in < 0) {
in *= -1;
ff = 1;
}
int mulp = in / d;
mulp = min(mulp, t);
int rem = (t - mulp) % 2;
int prefinal = in - d * mulp;
if (rem == 0) {
cout << prefinal << endl;
} else {
cout << min(abs(prefinal + d), abs(prefinal - d)) << endl;
}
// cout << << " " << (t - mulp) % 2 << endl;
return;
}
signed main() {
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// sieve();
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
} | replace | 61 | 62 | 61 | 62 | 0 | |
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<ll> vcl;
ll gcd(ll a, ll b) {
if (b > a)
swap(a, b);
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
ll rep_jijo(ll n, ll x) {
if (x == 0)
return 1;
if (x % 2 == 0) {
ll t = rep_jijo(n, x / 2);
return t * t % mod;
}
return n * rep_jijo(n, x - 1) % mod;
}
ll c2(ll n) { return n * (n - 1) / 2; }
int main() {
ll x, k, d, ans;
cin >> x >> k >> d;
if (x < 0)
x = -x;
ll y = x;
ll i = 0;
while (y > d) {
y = y - d;
i++;
}
if (i >= k)
ans = x - k * d;
else if ((k - i) % 2 == 0) {
ans = x - i * d;
} else
ans = abs(x - i * d - d);
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<ll> vcl;
ll gcd(ll a, ll b) {
if (b > a)
swap(a, b);
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
ll rep_jijo(ll n, ll x) {
if (x == 0)
return 1;
if (x % 2 == 0) {
ll t = rep_jijo(n, x / 2);
return t * t % mod;
}
return n * rep_jijo(n, x - 1) % mod;
}
ll c2(ll n) { return n * (n - 1) / 2; }
int main() {
ll x, k, d, ans;
cin >> x >> k >> d;
if (x < 0)
x = -x;
ll y = x;
ll i = 0;
i = y / d;
if (i >= k)
ans = x - k * d;
else if ((k - i) % 2 == 0) {
ans = x - i * d;
} else
ans = abs(x - i * d - d);
cout << ans << endl;
} | replace | 38 | 42 | 38 | 40 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
long long x, pos, plus_min, minus_min;
unsigned long k, d;
cin >> x >> k >> d;
pos = x;
plus_min = pow(10, 15) + 1;
minus_min = -pow(10, 15) - 1;
for (unsigned long i = 1; i <= k; i++) {
if (pos < 0) {
pos += d;
if (pos == plus_min) {
if ((k - i) % 2) {
cout << -minus_min;
return 0;
} else {
cout << plus_min;
return 0;
}
}
if (pos >= 0 && pos < plus_min)
plus_min = pos;
} else {
pos -= d;
if (pos == minus_min) {
if ((k - i) % 2) {
cout << plus_min;
return 0;
} else {
cout << -minus_min;
return 0;
}
}
if (pos < 0 && pos > minus_min)
minus_min = pos;
}
}
cout << abs(pos);
return 0;
} | #include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
long long x, pos, plus_min, minus_min;
unsigned long k, d;
cin >> x >> k >> d;
pos = x;
plus_min = pow(10, 15) + 1;
minus_min = -pow(10, 15) - 1;
long long quotient = abs(x) / d, mod = abs(x) % d;
if (k * d <= abs(x)) {
if (x < 0) {
cout << -x - k * d;
} else {
cout << x - k * d;
}
return 0;
} else {
k -= quotient;
pos = x < 0 ? -mod : mod;
}
for (unsigned long i = 1; i <= k; i++) {
if (pos < 0) {
pos += d;
if (pos == plus_min) {
if ((k - i) % 2) {
cout << -minus_min;
return 0;
} else {
cout << plus_min;
return 0;
}
}
if (pos >= 0 && pos < plus_min)
plus_min = pos;
} else {
pos -= d;
if (pos == minus_min) {
if ((k - i) % 2) {
cout << plus_min;
return 0;
} else {
cout << -minus_min;
return 0;
}
}
if (pos < 0 && pos > minus_min)
minus_min = pos;
}
}
cout << abs(pos);
return 0;
} | insert | 12 | 12 | 12 | 25 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, n) for (long long i = 0; i < (long long)(n); i++)
using Graph = vector<vector<int>>;
using ll = long long;
//----------素因数分解----------
vector<ll> prime_factorization(ll n) {
vector<ll> factors(n + 1, 0);
rep3(i, n + 1) {
if (i <= 1)
continue;
while (n % i == 0) {
n /= i;
factors[i]++;
}
}
return factors;
}
int main() {
ll x, k, d;
cin >> x >> k >> d;
rep3(i, k) {
if (abs(x + d) <= abs(x - d)) {
x += d;
} else {
x -= d;
}
if (abs(x) <= abs(d)) {
if ((k - 1 - i) % 2 == 0) {
cout << abs(x) << endl;
return 0;
} else {
cout << abs(d) - abs(x) << endl;
return 0;
}
}
}
cout << abs(x) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, n) for (long long i = 0; i < (long long)(n); i++)
using Graph = vector<vector<int>>;
using ll = long long;
//----------素因数分解----------
vector<ll> prime_factorization(ll n) {
vector<ll> factors(n + 1, 0);
rep3(i, n + 1) {
if (i <= 1)
continue;
while (n % i == 0) {
n /= i;
factors[i]++;
}
}
return factors;
}
int main() {
ll x, k, d;
cin >> x >> k >> d;
if (abs(x) % d == 0) {
ll p = abs(x) / d;
if (k > p) {
x = 0;
k -= p;
} else {
cout << abs(x) - k * d << endl;
return 0;
}
}
rep3(i, k) {
if (abs(x + d) <= abs(x - d)) {
x += d;
} else {
x -= d;
}
if (abs(x) <= abs(d)) {
if ((k - 1 - i) % 2 == 0) {
cout << abs(x) << endl;
return 0;
} else {
cout << abs(d) - abs(x) << endl;
return 0;
}
}
}
cout << abs(x) << endl;
return 0;
} | insert | 25 | 25 | 25 | 35 | TLE | |
p02584 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define foi(i, n) for (int i = 0; i < n; i++)
#define Foi(i, k, n) \
for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define fol(i, n) for (ll i = 0; i < n; i++)
#define Fol(i, k, n) \
for (ll i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vi g[N];
int a[N];
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1)
result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m) {
int i, u, v;
while (m--) {
cin >> u >> v;
g[u - 1].pb(v - 1);
g[v - 1].pb(u - 1);
}
}
void dfs(int u, int par) {
for (int v : g[u]) {
if (v == par)
continue;
dfs(v, u);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long x, k, d;
cin >> x >> k >> d;
x = abs(x);
while (k--) {
x -= d;
if (x - d < 0)
break;
}
if (k % 2 && k > 0)
x = min(x + d, abs(x - d));
cout << abs(x);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define foi(i, n) for (int i = 0; i < n; i++)
#define Foi(i, k, n) \
for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define fol(i, n) for (ll i = 0; i < n; i++)
#define Fol(i, k, n) \
for (ll i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vi g[N];
int a[N];
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1)
result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m) {
int i, u, v;
while (m--) {
cin >> u >> v;
g[u - 1].pb(v - 1);
g[v - 1].pb(u - 1);
}
}
void dfs(int u, int par) {
for (int v : g[u]) {
if (v == par)
continue;
dfs(v, u);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long x, k, d;
cin >> x >> k >> d;
x = abs(x);
ll quo = x / d;
if (quo < k) {
k -= quo;
x %= d;
} else {
x -= k * d;
k = 0;
}
if (k % 2 && k > 0)
x = min(x + d, abs(x - d));
cout << abs(x);
return 0;
} | replace | 83 | 87 | 83 | 90 | TLE | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, k;
cin >> n >> k;
vector<int> p(1 + n);
vector<ll> c(1 + n);
for (int i = 1; i <= n; ++i)
cin >> p[i];
for (int i = 1; i <= n; ++i)
cin >> c[i];
// ループのスコア計算は前処理
vector<ll> loop_score(1 + n);
vector<int> loop_size(1 + n);
vector<bool> loop_checked(1 + n, false);
for (int start_point = 1; start_point <= n; start_point++) {
if (!loop_checked[start_point]) {
ll score = c[start_point];
int loop_size_tmp = 1;
for (int point = p[start_point]; point != start_point; point = p[point]) {
score += c[point];
loop_size_tmp += 1;
}
loop_score[start_point] = score;
loop_size[start_point] = loop_size_tmp;
loop_checked[start_point] = true;
for (int point = p[start_point]; point != start_point; point = p[point]) {
loop_score[point] = score;
loop_size[point] = loop_size_tmp;
loop_checked[point] = true;
}
}
}
ll ans = -1e10;
for (int start_point = 1; start_point <= n; start_point++) {
// 最後の2周弱はケチらず数える
ll current_score = 0, max_score = -1e10;
int k_tmp = k, point = start_point;
if (loop_size[start_point] * 2 <= k && loop_score[start_point] > 0) {
current_score =
(k / loop_size[start_point] - 1) * loop_score[start_point];
max_score = current_score;
k_tmp = k - (k / loop_size[start_point] - 1) * loop_size[start_point];
}
for (int i = 0; i < k_tmp; ++i) {
point = p[point];
current_score += c[point];
max_score = max(current_score, max_score);
}
ans = max(ans, max_score);
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, k;
cin >> n >> k;
vector<int> p(1 + n);
vector<ll> c(1 + n);
for (int i = 1; i <= n; ++i)
cin >> p[i];
for (int i = 1; i <= n; ++i)
cin >> c[i];
// ループのスコア計算は前処理
vector<ll> loop_score(1 + n);
vector<int> loop_size(1 + n);
vector<bool> loop_checked(1 + n, false);
for (int start_point = 1; start_point <= n; start_point++) {
if (!loop_checked[start_point]) {
ll score = c[start_point];
int loop_size_tmp = 1;
for (int point = p[start_point]; point != start_point; point = p[point]) {
score += c[point];
loop_size_tmp += 1;
}
loop_score[start_point] = score;
loop_size[start_point] = loop_size_tmp;
loop_checked[start_point] = true;
for (int point = p[start_point]; point != start_point; point = p[point]) {
loop_score[point] = score;
loop_size[point] = loop_size_tmp;
loop_checked[point] = true;
}
}
}
ll ans = -1e10;
for (int start_point = 1; start_point <= n; start_point++) {
// 最後の2周弱はケチらず数える
ll current_score = 0, max_score = -1e10;
int k_tmp = k, point = start_point;
if (loop_size[start_point] * 2 <= k) {
if (loop_score[start_point] > 0) {
current_score =
(k / loop_size[start_point] - 1) * loop_score[start_point];
max_score = current_score;
}
k_tmp = k - (k / loop_size[start_point] - 1) * loop_size[start_point];
}
for (int i = 0; i < k_tmp; ++i) {
point = p[point];
current_score += c[point];
max_score = max(current_score, max_score);
}
ans = max(ans, max_score);
}
cout << ans;
return 0;
}
| replace | 42 | 46 | 42 | 48 | TLE | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, k;
cin >> n >> k;
vector<int> p(n);
vector<ll> c(n);
for (auto &e : p) {
cin >> e;
e--;
}
for (auto &e : c)
cin >> e;
vector<ll> s1(n), s2(n), d1(n), d2(n);
for (int i = 0; i < n; i++) {
if (d1[i])
continue;
d1[i] = 1;
for (int j = i; !d2[p[j]]; j = p[j]) {
if (!d1[p[j]]) {
s1[p[j]] = s1[j] + c[p[j]];
d1[p[j]] = d1[j] + 1;
} else {
s2[p[j]] = (d2[j] ? s2[j] : s1[j]) + c[p[j]];
d2[p[j]] = (d2[j] ? d2[j] : d1[j]) + 1;
}
}
}
ll r = -1e18;
for (int i = 0; i < n; i++) {
ll ri = 0;
for (int j = i, ki = k; ki;) {
if (d2[j] && 2 * (d2[j] - d1[j]) <= ki) {
ri += s2[j] - s1[j];
ki -= d2[j] - d1[j];
} else {
ri += c[p[j]];
ki--;
j = p[j];
}
r = max(r, ri);
ri = max(ri, 0ll);
}
}
cout << r << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, k;
cin >> n >> k;
vector<int> p(n);
vector<ll> c(n);
for (auto &e : p) {
cin >> e;
e--;
}
for (auto &e : c)
cin >> e;
vector<ll> s1(n), s2(n), d1(n), d2(n);
for (int i = 0; i < n; i++) {
if (d1[i])
continue;
d1[i] = 1;
for (int j = i; !d2[p[j]]; j = p[j]) {
if (!d1[p[j]]) {
s1[p[j]] = s1[j] + c[p[j]];
d1[p[j]] = d1[j] + 1;
} else {
s2[p[j]] = (d2[j] ? s2[j] : s1[j]) + c[p[j]];
d2[p[j]] = (d2[j] ? d2[j] : d1[j]) + 1;
}
}
}
ll r = -1e18;
for (int i = 0; i < n; i++) {
ll ri = 0;
for (int j = i, ki = k; ki;) {
if (d2[j] && 2 * (d2[j] - d1[j]) <= ki) {
ll t = ki / (d2[j] - d1[j]) - 1;
ri += t * (s2[j] - s1[j]);
ki -= t * (d2[j] - d1[j]);
} else {
ri += c[p[j]];
ki--;
j = p[j];
}
r = max(r, ri);
ri = max(ri, 0ll);
}
}
cout << r << endl;
}
| replace | 37 | 39 | 37 | 40 | TLE | |
p02585 | C++ | Time Limit Exceeded | #pragma GCC target("avx")
// #pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define DB double
#define LD long double
#define ST string
#define BS bitset
#define PA pair<LL, LL>
#define VE vector
#define VL vector<LL>
#define VP vector<pair<LL, LL>>
#define VVL vector<vector<LL>>
#define PQ priority_queue
#define PQS priority_queue<LL, vector<LL>, greater<LL>>
#define FI first
#define SE second
#define PB push_back
#define POB pop_back
#define PF push_front
#define POF pop_front
#define EB emplace_back
#define MP make_pair
#define TS to_string
#define TU to_ullong
#define BPL __builtin_popcountll
#define FOR(i, a, n) for (i = a; i < n; ++i)
#define FORR(i, a, n) for (i = n - 1; i >= a; --i)
#define rep(i, n) FOR(i, 0, n)
#define repr(i, n) FORR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define UB(a, n) *upper_bound(ALL(a), n)
#define UBn(a, n) upper_bound(ALL(a), n) - a.begin()
#define LB(a, n) *lower_bound(ALL(a), n)
#define LBn(a, n) lower_bound(ALL(a), n) - a.begin()
#define INF 1145141919810364364LL
#define PI 3.14159265358979
#define MOD 1000000007
// #define MOD 998244353
#define ERR 0.00000001
#define FAST \
cin.tie(0); \
ios::sync_with_stdio(false)
void Yn(LL a) {
if (a)
printf("Yes\n");
else
printf("No\n");
}
void YN(LL a) {
if (a)
printf("YES\n");
else
printf("NO\n");
}
LL pwmn(LL a, LL n) {
LL ans = 1;
while (ans < a)
ans *= n;
return ans;
}
LL dig(LL n) {
LL ret = 0;
while (n)
n /= 10, ++ret;
return ret;
}
LL GCD(LL a, LL b) {
LL c = 1, tmp = max(a, b);
b = min(a, b);
a = tmp;
while (c != 0) {
c = a % b;
a = b;
b = c;
}
return a;
}
LL LCM(LL a, LL b) { return a * b / GCD(a, b); }
int lcmp(const void *a, const void *b) {
if (*(LL *)a > *(LL *)b)
return 1;
if (*(LL *)a < *(LL *)b)
return -1;
return 0;
}
int lcmpr(const void *a, const void *b) {
if (*(LL *)a > *(LL *)b)
return -1;
if (*(LL *)a < *(LL *)b)
return 1;
return 0;
}
int ccmp(const void *a, const void *b) { return *(char *)a - *(char *)b; }
int ccmpr(const void *a, const void *b) { return *(char *)b - *(char *)a; }
int scmp(const void *a, const void *b) { return strcmp((char *)a, (char *)b); }
int scmpr(const void *a, const void *b) { return strcmp((char *)b, (char *)a); }
LL cmod(LL a, LL m) {
if (a % m < 0)
return a % m + abs(m);
else
return a % m;
}
LL DIV(LL a, LL d, LL m) {
LL l = m, x = 1, y = 0, k;
while (l) {
k = d / l;
d -= k * l;
swap(l, d);
x -= k * y;
swap(x, y);
}
return cmod(a * cmod(x, m), m);
}
LL POW(LL a, LL n, LL m) {
LL ans = 1;
while (n > 0) {
if (n & 1)
ans = ans * a % m;
a = a * a % m;
n >>= 1;
}
return ans;
}
VL fact, finv, inv;
void comi(LL n) {
LL i;
fact.resize(max(2LL, n + 1));
finv.resize(max(2LL, n + 1));
inv.resize(max(2LL, n + 1));
fact[0] = fact[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
FOR(i, 2, n + 1) {
fact[i] = fact[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
LL com(LL n, LL k) {
if (n < k || n < 0 || k < 0)
return 0;
return fact[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
bool cmps(pair<LL, LL> a, pair<LL, LL> b) {
if (a.SE != b.SE)
return a.SE < b.SE;
return a.FI < b.FI;
}
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define MI modint<MOD>
template <int mod> struct modint {
int x;
modint() : x(0) {}
modint(LL n) : x(n >= 0 ? n % mod : (mod - (-n) % mod) % mod) {}
modint &operator+=(const modint &n) {
if ((x += n.x) >= mod)
x -= mod;
return *this;
}
modint &operator-=(const modint &n) {
if ((x += mod - n.x) >= mod)
x -= mod;
return *this;
}
modint &operator++() {
*this += 1;
return *this;
}
modint &operator--() {
*this -= 1;
return *this;
}
modint &operator*=(const modint &n) {
x = (int)((LL)x * n.x % mod);
return *this;
}
modint &operator/=(const modint &n) {
*this *= n.inv();
return *this;
}
modint operator-() const { return modint(-x); }
modint operator+(const modint &n) const { return modint(*this) += n; }
modint operator-(const modint &n) const { return modint(*this) -= n; }
modint operator++(int) {
modint ret(*this);
*this += 1;
return ret;
}
modint operator--(int) {
modint ret(*this);
*this -= 1;
return ret;
}
modint operator*(const modint &n) const { return modint(*this) *= n; }
modint operator/(const modint &n) const { return modint(*this) /= n; }
bool operator<(const modint &n) const { return x < n.x; }
bool operator>(const modint &n) const { return x > n.x; }
bool operator<=(const modint &n) const { return x <= n.x; }
bool operator>=(const modint &n) const { return x >= n.x; }
bool operator!=(const modint &n) const { return x != n.x; }
bool operator==(const modint &n) const { return x == n.x; }
friend istream &operator>>(istream &is, modint &n) {
LL l;
is >> l;
n = modint<mod>(l);
return is;
}
friend ostream &operator<<(ostream &os, const modint &n) { return os << n.x; }
int getmod() { return mod; }
modint inv() const {
int a = x, b = mod, c = 1, d = 0, n;
while (b) {
n = a / b;
swap(a -= n * b, b);
swap(c -= n * d, d);
}
return modint(c);
}
modint pow(LL n) const {
modint ret(1), m(x);
while (n) {
if (n & 1)
ret *= m;
m *= m;
n >>= 1;
}
return ret;
}
};
int main() {
FAST;
LL i, j, ans = -INF, N, K, a, k, s, r, f, b, c;
cin >> N >> K;
VL P(N), C(N);
rep(i, N) {
cin >> P[i];
--P[i];
}
rep(i, N) cin >> C[i];
rep(i, N) {
VL v(N * 100, INF), p(N * 100, INF), q(N * 100, INF);
v[i] = p[0] = j = f = s = r = b = 0;
a = -INF;
k = q[0] = i;
while (j < K) {
++j;
k = P[k];
q[j] = k;
chmax(a, p[j - 1] + C[k]);
p[j] = p[j - 1] + C[k];
if (v[k] != INF) {
f = 1;
break;
}
v[k] = j;
}
if (f == 0) {
chmax(ans, a);
continue;
}
s = p[j] - p[v[q[j]]];
/*if(s<0){
chmax(ans,a);
continue;
}*/
c = K;
r = j - v[q[j]];
b = p[j];
c -= j;
b += s * (c / r);
c %= r;
chmax(a, b + a - p[j]);
k = q[j];
j = 0;
while (j < c) {
++j;
k = P[k];
b += C[k];
chmax(a, b);
}
chmax(ans, a);
}
cout << ans << endl;
}
| #pragma GCC target("avx")
// #pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define DB double
#define LD long double
#define ST string
#define BS bitset
#define PA pair<LL, LL>
#define VE vector
#define VL vector<LL>
#define VP vector<pair<LL, LL>>
#define VVL vector<vector<LL>>
#define PQ priority_queue
#define PQS priority_queue<LL, vector<LL>, greater<LL>>
#define FI first
#define SE second
#define PB push_back
#define POB pop_back
#define PF push_front
#define POF pop_front
#define EB emplace_back
#define MP make_pair
#define TS to_string
#define TU to_ullong
#define BPL __builtin_popcountll
#define FOR(i, a, n) for (i = a; i < n; ++i)
#define FORR(i, a, n) for (i = n - 1; i >= a; --i)
#define rep(i, n) FOR(i, 0, n)
#define repr(i, n) FORR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define UB(a, n) *upper_bound(ALL(a), n)
#define UBn(a, n) upper_bound(ALL(a), n) - a.begin()
#define LB(a, n) *lower_bound(ALL(a), n)
#define LBn(a, n) lower_bound(ALL(a), n) - a.begin()
#define INF 1145141919810364364LL
#define PI 3.14159265358979
#define MOD 1000000007
// #define MOD 998244353
#define ERR 0.00000001
#define FAST \
cin.tie(0); \
ios::sync_with_stdio(false)
void Yn(LL a) {
if (a)
printf("Yes\n");
else
printf("No\n");
}
void YN(LL a) {
if (a)
printf("YES\n");
else
printf("NO\n");
}
LL pwmn(LL a, LL n) {
LL ans = 1;
while (ans < a)
ans *= n;
return ans;
}
LL dig(LL n) {
LL ret = 0;
while (n)
n /= 10, ++ret;
return ret;
}
LL GCD(LL a, LL b) {
LL c = 1, tmp = max(a, b);
b = min(a, b);
a = tmp;
while (c != 0) {
c = a % b;
a = b;
b = c;
}
return a;
}
LL LCM(LL a, LL b) { return a * b / GCD(a, b); }
int lcmp(const void *a, const void *b) {
if (*(LL *)a > *(LL *)b)
return 1;
if (*(LL *)a < *(LL *)b)
return -1;
return 0;
}
int lcmpr(const void *a, const void *b) {
if (*(LL *)a > *(LL *)b)
return -1;
if (*(LL *)a < *(LL *)b)
return 1;
return 0;
}
int ccmp(const void *a, const void *b) { return *(char *)a - *(char *)b; }
int ccmpr(const void *a, const void *b) { return *(char *)b - *(char *)a; }
int scmp(const void *a, const void *b) { return strcmp((char *)a, (char *)b); }
int scmpr(const void *a, const void *b) { return strcmp((char *)b, (char *)a); }
LL cmod(LL a, LL m) {
if (a % m < 0)
return a % m + abs(m);
else
return a % m;
}
LL DIV(LL a, LL d, LL m) {
LL l = m, x = 1, y = 0, k;
while (l) {
k = d / l;
d -= k * l;
swap(l, d);
x -= k * y;
swap(x, y);
}
return cmod(a * cmod(x, m), m);
}
LL POW(LL a, LL n, LL m) {
LL ans = 1;
while (n > 0) {
if (n & 1)
ans = ans * a % m;
a = a * a % m;
n >>= 1;
}
return ans;
}
VL fact, finv, inv;
void comi(LL n) {
LL i;
fact.resize(max(2LL, n + 1));
finv.resize(max(2LL, n + 1));
inv.resize(max(2LL, n + 1));
fact[0] = fact[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
FOR(i, 2, n + 1) {
fact[i] = fact[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
LL com(LL n, LL k) {
if (n < k || n < 0 || k < 0)
return 0;
return fact[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
bool cmps(pair<LL, LL> a, pair<LL, LL> b) {
if (a.SE != b.SE)
return a.SE < b.SE;
return a.FI < b.FI;
}
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define MI modint<MOD>
template <int mod> struct modint {
int x;
modint() : x(0) {}
modint(LL n) : x(n >= 0 ? n % mod : (mod - (-n) % mod) % mod) {}
modint &operator+=(const modint &n) {
if ((x += n.x) >= mod)
x -= mod;
return *this;
}
modint &operator-=(const modint &n) {
if ((x += mod - n.x) >= mod)
x -= mod;
return *this;
}
modint &operator++() {
*this += 1;
return *this;
}
modint &operator--() {
*this -= 1;
return *this;
}
modint &operator*=(const modint &n) {
x = (int)((LL)x * n.x % mod);
return *this;
}
modint &operator/=(const modint &n) {
*this *= n.inv();
return *this;
}
modint operator-() const { return modint(-x); }
modint operator+(const modint &n) const { return modint(*this) += n; }
modint operator-(const modint &n) const { return modint(*this) -= n; }
modint operator++(int) {
modint ret(*this);
*this += 1;
return ret;
}
modint operator--(int) {
modint ret(*this);
*this -= 1;
return ret;
}
modint operator*(const modint &n) const { return modint(*this) *= n; }
modint operator/(const modint &n) const { return modint(*this) /= n; }
bool operator<(const modint &n) const { return x < n.x; }
bool operator>(const modint &n) const { return x > n.x; }
bool operator<=(const modint &n) const { return x <= n.x; }
bool operator>=(const modint &n) const { return x >= n.x; }
bool operator!=(const modint &n) const { return x != n.x; }
bool operator==(const modint &n) const { return x == n.x; }
friend istream &operator>>(istream &is, modint &n) {
LL l;
is >> l;
n = modint<mod>(l);
return is;
}
friend ostream &operator<<(ostream &os, const modint &n) { return os << n.x; }
int getmod() { return mod; }
modint inv() const {
int a = x, b = mod, c = 1, d = 0, n;
while (b) {
n = a / b;
swap(a -= n * b, b);
swap(c -= n * d, d);
}
return modint(c);
}
modint pow(LL n) const {
modint ret(1), m(x);
while (n) {
if (n & 1)
ret *= m;
m *= m;
n >>= 1;
}
return ret;
}
};
int main() {
FAST;
LL i, j, ans = -INF, N, K, a, k, s, r, f, b, c;
cin >> N >> K;
VL P(N), C(N);
rep(i, N) {
cin >> P[i];
--P[i];
}
rep(i, N) cin >> C[i];
rep(i, N) {
VL v(N + N, INF), p(N + N, INF), q(N + N, INF);
v[i] = p[0] = j = f = s = r = b = 0;
a = -INF;
k = q[0] = i;
while (j < K) {
++j;
k = P[k];
q[j] = k;
chmax(a, p[j - 1] + C[k]);
p[j] = p[j - 1] + C[k];
if (v[k] != INF) {
f = 1;
break;
}
v[k] = j;
}
if (f == 0) {
chmax(ans, a);
continue;
}
s = p[j] - p[v[q[j]]];
/*if(s<0){
chmax(ans,a);
continue;
}*/
c = K;
r = j - v[q[j]];
b = p[j];
c -= j;
b += s * (c / r);
c %= r;
chmax(a, b + a - p[j]);
k = q[j];
j = 0;
while (j < c) {
++j;
k = P[k];
b += C[k];
chmax(a, b);
}
chmax(ans, a);
}
cout << ans << endl;
}
| replace | 263 | 264 | 263 | 264 | TLE | |
p02585 | Python | Time Limit Exceeded | N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
P = [None] + P
C = [None] + C
all_max = C[1]
for st in range(1, N + 1):
scores = []
visit = set()
p = st
while p not in visit and len(visit) < K:
next_p = P[p]
scores.append(C[next_p])
visit.add(p)
p = next_p
num_elem = len(scores)
all_sum = sum(scores)
q, r = divmod(K, num_elem)
max_ = scores[0]
temp = scores[0]
max_r = scores[0]
temp_r = scores[0]
for x in range(1, num_elem):
if x < r:
temp_r += scores[x]
max_r = max(max_r, temp_r)
temp += scores[x]
max_ = max(max_, temp)
temp_max = scores[0]
if all_sum > 0 and q > 0:
if r > 0:
temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r)
else:
temp_max = all_sum * (q - 1) + max_
else:
temp_max = max_
all_max = max(all_max, temp_max)
print(all_max)
| N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
P = [None] + P
C = [None] + C
all_max = C[1]
for st in range(1, N + 1):
scores = []
visit = set()
p = st
while p not in visit and len(visit) < K:
next_p = P[p]
scores.append(C[next_p])
visit.add(p)
p = next_p
num_elem = len(scores)
all_sum = sum(scores)
q, r = divmod(K, num_elem)
max_ = scores[0]
temp = scores[0]
max_r = scores[0]
temp_r = scores[0]
for x in range(1, num_elem):
if x < r:
temp_r += scores[x]
max_r = max(max_r, temp_r)
temp += scores[x]
max_ = max(max_, temp)
temp_max = scores[0]
if all_sum > 0 and q > 0:
if r > 0:
temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r)
else:
temp_max = all_sum * (q - 1) + max_
else:
temp_max = max_
all_max = max(all_max, temp_max)
print(all_max)
| insert | 48 | 48 | 48 | 49 | TLE | |
p02585 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> p(n), c(n);
rep(i, n) {
cin >> p[i];
p[i]--;
}
rep(i, n) cin >> c[i];
ll ans = -1000000000;
rep(i, n) {
ll loop_num = 1000000000;
ll loop_score = -1000000000;
ll score = 0;
vector<ll> scores;
ll next = p[i];
rep(j, k) {
score += c[next];
next = p[next];
scores.push_back(score);
if (next == p[i]) {
loop_num = j + 1;
loop_score = score;
break;
}
}
ll a1 = 0, a2 = 0;
if (loop_num > 0) {
a1 = k / loop_num; // 一周できる回数
a2 = k % loop_num; // 一周繰り返した後の余り
}
if (a1 > 0 && loop_score > 0) {
rep(j, loop_num) {
ll p = 0;
if (j >= a2)
p = -1;
ans = max(ans, scores[j] + loop_score * (a1 + p));
}
} else {
rep(j, loop_num) { ans = max(ans, scores[j]); }
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> p(n), c(n);
rep(i, n) {
cin >> p[i];
p[i]--;
}
rep(i, n) cin >> c[i];
ll ans = -1000000000;
rep(i, n) {
ll loop_num = k;
ll loop_score = -1000000000;
ll score = 0;
vector<ll> scores;
ll next = p[i];
rep(j, k) {
score += c[next];
next = p[next];
scores.push_back(score);
if (next == p[i]) {
loop_num = j + 1;
loop_score = score;
break;
}
}
ll a1 = 0, a2 = 0;
if (loop_num > 0) {
a1 = k / loop_num; // 一周できる回数
a2 = k % loop_num; // 一周繰り返した後の余り
}
if (a1 > 0 && loop_score > 0) {
rep(j, loop_num) {
ll p = 0;
if (j >= a2)
p = -1;
ans = max(ans, scores[j] + loop_score * (a1 + p));
}
} else {
rep(j, loop_num) { ans = max(ans, scores[j]); }
}
}
cout << ans << endl;
return 0;
} | replace | 21 | 22 | 21 | 22 | -11 | |
p02585 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int n, k;
cin >> n >> k;
vector<int> p(n), c(n);
rep(i, n) cin >> p[i];
rep(i, n) cin >> c[i];
rep(i, n) p[i]--;
ll ans = -1e18;
rep(si, n) {
int x = si;
vector<int> s;
ll tot = 0;
while (1) {
x = p[x];
s.push_back(c[x]);
tot += c[x];
if (x == si)
break;
}
int l = s.size();
ll t = 0;
rep(i, k) {
t += s[i];
if (i + 1 > k)
break;
ll now = t;
if (tot > 0) {
ll e = (k - (i + 1)) / l;
now += tot * e;
}
ans = max(ans, now);
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int n, k;
cin >> n >> k;
vector<int> p(n), c(n);
rep(i, n) cin >> p[i];
rep(i, n) cin >> c[i];
rep(i, n) p[i]--;
ll ans = -1e18;
rep(si, n) {
int x = si;
vector<int> s;
ll tot = 0;
while (1) {
x = p[x];
s.push_back(c[x]);
tot += c[x];
if (x == si)
break;
}
int l = s.size();
ll t = 0;
rep(i, l) {
t += s[i];
if (i + 1 > k)
break;
ll now = t;
if (tot > 0) {
ll e = (k - (i + 1)) / l;
now += tot * e;
}
ans = max(ans, now);
}
}
cout << ans << endl;
return 0;
}
| replace | 27 | 28 | 27 | 28 | 0 | |
p02585 | C++ | Time Limit Exceeded | #include <iostream>
#include <limits.h>
#include <queue>
#include <vector>
using namespace std;
int main(void) {
int N;
long long K;
cin >> N >> K;
vector<int> P(N + 1); // move
for (int i = 1; i < N + 1; i++) {
cin >> P[i];
}
vector<long long> C(N + 1); // point
for (int i = 1; i < N + 1; i++) {
cin >> C[i];
}
long long max_point = LLONG_MIN;
long long tmp_point = 0;
int current_p = 0;
max_point = LLONG_MIN;
for (int m_first = 1; m_first < N + 1; m_first++) {
tmp_point = 0;
current_p = m_first;
// cout << "m_first: " << m_first << ", current: " << current_p << endl;
for (int total = 1; total <= K; total++) {
// cout << "total: " << total << ", current: " << current_p << endl;
// cout << "tmp_point: " << tmp_point << endl;
tmp_point += C[P[current_p]];
if (max_point <= tmp_point)
max_point = tmp_point;
current_p = P[current_p];
if (m_first == current_p && tmp_point <= 0) {
tmp_point += C[P[current_p]];
if (max_point <= tmp_point)
max_point = tmp_point;
break;
}
}
// cout << "" << endl;
}
cout << max_point << endl;
return 0;
}
| #include <iostream>
#include <limits.h>
#include <queue>
#include <vector>
using namespace std;
int main(void) {
int N;
long long K;
cin >> N >> K;
vector<int> P(N + 1); // move
for (int i = 1; i < N + 1; i++) {
cin >> P[i];
}
vector<long long> C(N + 1); // point
for (int i = 1; i < N + 1; i++) {
cin >> C[i];
}
long long max_point = LLONG_MIN;
long long tmp_point = 0;
int current_p = 0;
max_point = LLONG_MIN;
for (int m_first = 1; m_first < N + 1; m_first++) {
tmp_point = 0;
current_p = m_first;
// cout << "m_first: " << m_first << ", current: " << current_p << endl;
for (int total = 1; total <= K; total++) {
// cout << "total: " << total << ", current: " << current_p << endl;
// cout << "tmp_point: " << tmp_point << endl;
tmp_point += C[P[current_p]];
if (max_point <= tmp_point)
max_point = tmp_point;
current_p = P[current_p];
if (m_first == current_p && tmp_point <= 0) {
tmp_point += C[P[current_p]];
if (max_point <= tmp_point)
max_point = tmp_point;
break;
} else if (m_first == current_p && tmp_point > 0) {
int cycle_num = K / total;
int amari = K % total;
// cout << "cycle_num: " << cycle_num << endl;
// cout << "amari: " << amari << endl;
// cout << "tmp_potnt: " << tmp_point << endl;
tmp_point = tmp_point * (cycle_num - 1);
if (max_point <= tmp_point)
max_point = tmp_point;
for (int l = 0; l < amari + total; l++) {
// cout << "tmp_point:" << tmp_point << endl;
tmp_point += C[P[current_p]];
if (max_point <= tmp_point)
max_point = tmp_point;
current_p = P[current_p];
}
break;
}
}
// cout << "" << endl;
}
cout << max_point << endl;
return 0;
} | insert | 43 | 43 | 43 | 60 | TLE | |
p02585 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)
using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;
const long long INF = 1000000000000000001;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;
using namespace std;
// namespace mp = boost::multiprecision;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
ll k;
cin >> n >> k;
int *p = new int[n];
rep(i, n) cin >> p[i];
ll *c = new ll[n];
rep(i, n) cin >> c[i];
ll ans = INF * (-1);
ll sum;
ll *sum_max = new ll[n + 1];
rep(i, n) {
sum = 0;
sum_max[0] = 0;
ll counter = 0;
int j = i;
do {
++counter;
j = p[j] - 1;
sum += c[j];
sum_max[counter] = max(sum, sum_max[counter - 1]);
} while (i != j);
if (k <= counter || sum < 0) {
if (sum_max[k] == 0) {
sum = 0;
sum_max[0] = c[p[i] - 1];
counter = 0;
j = i;
do {
++counter;
j = p[j] - 1;
sum += c[j];
sum_max[counter] = max(sum, sum_max[counter - 1]);
} while (i != j);
}
ans = max(ans, sum_max[min(k, counter)]);
} else {
if (sum + sum_max[k % counter] >= sum_max[counter]) {
ans = max(ans, sum * (k / counter) + sum_max[k % counter]);
} else {
ans = max(ans, sum * max((ll)0, k / counter - 1) + sum_max[counter]);
}
}
}
cout << ans << "\n";
return 0;
}
| #include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)
using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;
const long long INF = 1000000000000000001;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;
using namespace std;
// namespace mp = boost::multiprecision;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
ll k;
cin >> n >> k;
int *p = new int[n];
rep(i, n) cin >> p[i];
ll *c = new ll[n];
rep(i, n) cin >> c[i];
ll ans = INF * (-1);
ll sum;
ll *sum_max = new ll[n + 1];
rep(i, n) {
sum = 0;
sum_max[0] = 0;
ll counter = 0;
int j = i;
do {
++counter;
j = p[j] - 1;
sum += c[j];
sum_max[counter] = max(sum, sum_max[counter - 1]);
} while (i != j);
if (k <= counter || sum < 0) {
if (sum_max[min(k, counter)] == 0) {
sum = 0;
sum_max[0] = c[p[i] - 1];
counter = 0;
j = i;
do {
++counter;
j = p[j] - 1;
sum += c[j];
sum_max[counter] = max(sum, sum_max[counter - 1]);
} while (i != j);
}
ans = max(ans, sum_max[min(k, counter)]);
} else {
if (sum + sum_max[k % counter] >= sum_max[counter]) {
ans = max(ans, sum * (k / counter) + sum_max[k % counter]);
} else {
ans = max(ans, sum * max((ll)0, k / counter - 1) + sum_max[counter]);
}
}
}
cout << ans << "\n";
return 0;
}
| replace | 75 | 76 | 75 | 76 | 0 | |
p02585 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
template <class T> inline void chmin(T &a, T b) {
if (a > b) {
a = b;
}
}
template <class T> inline void chmax(T &a, T b) {
if (a < b) {
a = b;
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
ll N, K;
cin >> N >> K;
vector<ll> P(N);
vector<ll> C(N);
REP(i, N) cin >> P[i];
REP(i, N) P[i]--;
REP(i, N) cin >> C[i];
ll max = -2000000000;
REP(i, N) {
if (P[i] == -1)
continue;
vector<ll> A(0);
A.pb(C[i]);
ll bef = P[i];
P[i] = -1;
while (1) {
A.pb(C[bef]);
ll befo = bef;
bef = P[bef];
P[befo] = -1;
if (bef < 0)
break;
if (P[bef] == -1)
break;
}
ll score = 0;
ll nokori = K;
ll allsum = 0;
REP(l, A.size()) allsum += A[l];
if (allsum <= 0) {
nokori = min(K, (ll)A.size() - 1);
} else {
if (nokori / A.size() > 1) {
score += (nokori / A.size() - 1) * allsum;
nokori %= A.size();
nokori += A.size();
}
}
ll siz = A.size();
REP(l, siz) { A.pb(A[l]); }
REP(l, siz) { A.pb(A[l]); }
vector<ll> rui(A.size() + 1);
REP(i, A.size()) rui[i + 1] = A[i] + rui[i];
ll maxpls = -2000000000;
REP(l, A.size() * 2 / 3) {
REPS(al, nokori) { chmax(maxpls, rui[l + al] - rui[l]); }
}
score += maxpls;
chmax(max, score);
}
cout << max << "\n";
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
template <class T> inline void chmin(T &a, T b) {
if (a > b) {
a = b;
}
}
template <class T> inline void chmax(T &a, T b) {
if (a < b) {
a = b;
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
ll N, K;
cin >> N >> K;
vector<ll> P(N);
vector<ll> C(N);
REP(i, N) cin >> P[i];
REP(i, N) P[i]--;
REP(i, N) cin >> C[i];
ll max = -2000000000;
REP(i, N) {
if (P[i] == -1)
continue;
vector<ll> A(0);
A.pb(C[i]);
ll bef = P[i];
P[i] = -1;
while (1) {
A.pb(C[bef]);
ll befo = bef;
bef = P[bef];
P[befo] = -1;
if (bef < 0)
break;
if (P[bef] == -1)
break;
}
ll score = 0;
ll nokori = K;
ll allsum = 0;
REP(l, A.size()) allsum += A[l];
if (allsum <= 0) {
nokori = min(K, (ll)A.size() - 1);
} else {
if (nokori / A.size() > 1) {
score += (nokori / A.size() - 1) * allsum;
nokori %= A.size();
nokori += A.size();
}
}
ll siz = A.size();
REP(l, siz) { A.pb(A[l]); }
REP(l, siz) { A.pb(A[l]); }
vector<ll> rui(A.size() + 1);
REP(i, A.size()) rui[i + 1] = A[i] + rui[i];
ll maxpls = -2000000000;
REP(l, A.size() * 2 / 3) {
REPS(al, nokori) {
if (l + al >= A.size())
continue;
chmax(maxpls, rui[l + al] - rui[l]);
}
}
score += maxpls;
chmax(max, score);
}
cout << max << "\n";
} | replace | 78 | 79 | 78 | 83 | 0 | |
p02585 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)
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;
}
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert
#define debug(a) cerr << (a) << endl
#define dbrep(a, n) \
rep(_i, n) cerr << (a[_i]) << " "; \
cerr << endl
#define dbrep2(a, n, m) \
rep(_i, n) { \
rep(_j, m) cerr << (a[_i][_j]) << " "; \
cerr << endl; \
}
using namespace std;
template <class A, class B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << "(" << p.fi << "," << p.se << ")";
}
template <class A, class B> istream &operator>>(istream &is, pair<A, B> &p) {
return is >> p.fi >> p.se;
}
/* Some Libraries */
//-------------------------------------------------
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
vi P(N);
vll C(N);
rep(i, N) cin >> P[i], P[i]--;
rep(i, N) cin >> C[i];
ll ans = (ll)-1e18;
rep(i, N) {
ll cand = 0;
vll vec;
vec.pb(C[i]);
int cur = P[i];
while (cur != i) {
vec.pb(C[cur]);
cur = P[cur];
}
int V = vec.size();
vll sum(V + 1);
rep(i, V) sum[i + 1] = sum[i] + vec[i];
ll S = sum[V];
rep(i, V) chmax(sum[i + 2], sum[i + 1]);
if (S > 0) {
// Max
int t = K / V;
cand = S * t + sum[K % V];
// Max-1
if (t > 0) {
t = K / V - 1;
chmax(cand, S * t + sum[V]);
}
} else {
cand = sum[min(K, V)];
}
chmax(ans, cand);
}
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)
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;
}
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert
#define debug(a) cerr << (a) << endl
#define dbrep(a, n) \
rep(_i, n) cerr << (a[_i]) << " "; \
cerr << endl
#define dbrep2(a, n, m) \
rep(_i, n) { \
rep(_j, m) cerr << (a[_i][_j]) << " "; \
cerr << endl; \
}
using namespace std;
template <class A, class B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << "(" << p.fi << "," << p.se << ")";
}
template <class A, class B> istream &operator>>(istream &is, pair<A, B> &p) {
return is >> p.fi >> p.se;
}
/* Some Libraries */
//-------------------------------------------------
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
vi P(N);
vll C(N);
rep(i, N) cin >> P[i], P[i]--;
rep(i, N) cin >> C[i];
ll ans = (ll)-1e18;
rep(i, N) {
ll cand = 0;
vll vec;
vec.pb(C[i]);
int cur = P[i];
while (cur != i) {
vec.pb(C[cur]);
cur = P[cur];
}
int V = vec.size();
vll sum(V + 1);
rep(i, V) sum[i + 1] = sum[i] + vec[i];
ll S = sum[V];
rep(i, V - 1) chmax(sum[i + 2], sum[i + 1]);
if (S > 0) {
// Max
int t = K / V;
cand = S * t + sum[K % V];
// Max-1
if (t > 0) {
t = K / V - 1;
chmax(cand, S * t + sum[V]);
}
} else {
cand = sum[min(K, V)];
}
chmax(ans, cand);
}
cout << ans << "\n";
return 0;
}
| replace | 85 | 86 | 85 | 86 | 0 | |
p02585 | Python | Runtime Error | import sys
readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)
def main():
n, k = map(int, readline().split())
p = list(map(int, readline().split()))
p = [x - 1 for x in p]
c = list(map(int, readline().split()))
ans = -INF
for i in range(n):
cur = i
nx = -1
cnt = 0
score_dict = dict()
score_dict[0] = 0
score = 0
rem = k
while rem > 0 and nx != i:
cnt += 1
rem -= 1
nx = p[cur]
score += c[nx]
score_dict[cnt] = score
ans = max(ans, score)
cur = nx
if rem > 0 and score > 0:
loop = (rem // cnt) - 1
if loop > 0:
score += score * loop
rem -= loop * cnt
for j in range(rem + 1):
st = score + score_dict[j]
ans = max(ans, st)
print(ans)
if __name__ == "__main__":
main()
| import sys
readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)
def main():
n, k = map(int, readline().split())
p = list(map(int, readline().split()))
p = [x - 1 for x in p]
c = list(map(int, readline().split()))
ans = -INF
for i in range(n):
cur = i
nx = -1
cnt = 0
score_dict = dict()
score_dict[0] = 0
score = 0
rem = k
while rem > 0 and nx != i:
cnt += 1
rem -= 1
nx = p[cur]
score += c[nx]
score_dict[cnt] = score
ans = max(ans, score)
cur = nx
if rem > 0 and score > 0:
loop = (rem - 1) // cnt
if loop > 0:
score += score * loop
rem -= loop * cnt
for j in range(rem + 1):
st = score + score_dict[j]
ans = max(ans, st)
print(ans)
if __name__ == "__main__":
main()
| replace | 33 | 34 | 33 | 34 | 0 | |
p02585 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, K;
vector<int> P, C;
map<int, vector<pair<ll, int>>> memo;
vector<pair<ll, int>> f(int k) {
if (memo.count(k))
return memo[k];
if (k == 1) {
vector<pair<ll, int>> r(N);
for (int i = 0; i < N; ++i) {
r[i] = {C[P[i]], P[i]};
}
return memo[k] = r;
} else {
auto a = f(k / 2);
auto b = f(k - k / 2);
vector<pair<ll, int>> r(N);
for (int i = 0; i < N; ++i) {
auto x = a[i];
auto y = b[x.second];
r[i] = {x.first + y.first, y.second};
}
return memo[k] = r;
}
}
int main() {
cin >> N >> K;
P.resize(N);
C.resize(N);
for (auto &x : P) {
cin >> x;
--x;
}
for (auto &x : C)
cin >> x;
auto res = -(1LL << 62);
for (int kk = 1; kk <= K; ++kk) {
if (kk == N + 1 && kk < K - N)
kk = K - N;
for (auto x : f(kk)) {
res = max(res, x.first);
}
}
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, K;
vector<int> P, C;
map<int, vector<pair<ll, int>>> memo;
vector<pair<ll, int>> f(int k) {
if (memo.count(k))
return memo[k];
if (k == 1) {
vector<pair<ll, int>> r(N);
for (int i = 0; i < N; ++i) {
r[i] = {C[P[i]], P[i]};
}
return memo[k] = r;
} else {
int x = k / 2;
if (N < K - N && k > K - N) {
x = K - N;
}
int y = k - x;
auto a = f(x);
auto b = f(y);
vector<pair<ll, int>> r(N);
for (int i = 0; i < N; ++i) {
auto x = a[i];
auto y = b[x.second];
r[i] = {x.first + y.first, y.second};
}
return memo[k] = r;
}
}
int main() {
cin >> N >> K;
P.resize(N);
C.resize(N);
for (auto &x : P) {
cin >> x;
--x;
}
for (auto &x : C)
cin >> x;
auto res = -(1LL << 62);
for (int kk = 1; kk <= K; ++kk) {
if (kk == N + 1 && kk < K - N)
kk = K - N;
for (auto x : f(kk)) {
res = max(res, x.first);
}
}
cout << res << endl;
}
| replace | 16 | 18 | 16 | 23 | MLE | |
p02585 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define RFOR(i, a, b) for (int i = b - 1; i >= a; --i)
#define all(x) begin(x), end(x)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
os << p.first << ":" << p.second;
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
REP(i, SZ(v)) {
if (i)
os << ", ";
os << v[i];
}
return os << "]";
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
os << "[";
for (auto it = mp.begin(); it != mp.end(); ++it) {
if (it != mp.begin()) {
os << ", ";
}
os << *it;
}
return os << "]";
}
// edit
struct Solve {
int N, K;
vector<ll> cscore;
vector<int> csize;
vector<bool> visit;
vector<int> P;
vector<ll> C;
void dfs(int v, int &sz, ll &score) {
if (visit[v]) {
// TODO
return;
}
visit[v] = true;
sz++;
score += C[v];
dfs(P[v], sz, score);
csize[v] = sz;
cscore[v] = score;
}
ll dfs2(int v, int sz) {
if (sz == 0) {
return 0ll;
}
ll ret = max(0ll, dfs2(P[v], sz - 1)) + C[P[v]];
return ret;
}
void solve() {
cin >> N >> K;
cscore.resize(N, -1);
csize.resize(N, -1);
visit.resize(N, false);
P.resize(N);
C.resize(N);
REP(i, N) cin >> P[i];
REP(i, N) P[i]--;
REP(i, N) cin >> C[i];
REP(i, N) {
int sz = 0;
ll score = 0;
if (!visit[i])
dfs(i, sz, score);
}
ll ans = -LINF;
for (int i = 0; i < N; ++i) {
if (cscore[i] <= 0) {
ll score = 0;
ll add = dfs2(i, K);
score += add;
chmax(ans, score);
} else {
ll div = max((ll)K / csize[i] - 1, 0ll);
ll score = div * cscore[i];
ll nokori = K - div * csize[i];
ll add = dfs2(i, nokori);
score += add;
chmax(ans, score);
}
}
cout << ans << endl;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
// std::ifstream in("input.txt");
// std::cin.rdbuf(in.rdbuf());
Solve().solve();
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define RFOR(i, a, b) for (int i = b - 1; i >= a; --i)
#define all(x) begin(x), end(x)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
os << p.first << ":" << p.second;
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
REP(i, SZ(v)) {
if (i)
os << ", ";
os << v[i];
}
return os << "]";
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
os << "[";
for (auto it = mp.begin(); it != mp.end(); ++it) {
if (it != mp.begin()) {
os << ", ";
}
os << *it;
}
return os << "]";
}
// edit
struct Solve {
int N, K;
vector<ll> cscore;
vector<int> csize;
vector<bool> visit;
vector<int> P;
vector<ll> C;
void dfs(int v, int &sz, ll &score) {
if (visit[v]) {
// TODO
return;
}
visit[v] = true;
sz++;
score += C[v];
dfs(P[v], sz, score);
csize[v] = sz;
cscore[v] = score;
}
ll dfs2(int v, int sz) {
if (sz == 0) {
return 0ll;
}
ll ret = max(0ll, dfs2(P[v], sz - 1)) + C[P[v]];
return ret;
}
void solve() {
cin >> N >> K;
cscore.resize(N, -1);
csize.resize(N, -1);
visit.resize(N, false);
P.resize(N);
C.resize(N);
REP(i, N) cin >> P[i];
REP(i, N) P[i]--;
REP(i, N) cin >> C[i];
REP(i, N) {
int sz = 0;
ll score = 0;
if (!visit[i])
dfs(i, sz, score);
}
ll ans = -LINF;
for (int i = 0; i < N; ++i) {
if (cscore[i] <= 0) {
ll score = 0;
ll add = dfs2(i, min(N, K));
score += add;
chmax(ans, score);
} else {
ll div = max((ll)K / csize[i] - 1, 0ll);
ll score = div * cscore[i];
ll nokori = K - div * csize[i];
ll add = dfs2(i, nokori);
score += add;
chmax(ans, score);
}
}
cout << ans << endl;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
// std::ifstream in("input.txt");
// std::cin.rdbuf(in.rdbuf());
Solve().solve();
return 0;
}
| replace | 158 | 159 | 158 | 159 | 0 | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v) \
sort(all(v)); \
v.resize(unique(all(v)) - v.begin());
#define INF (ll)1e9 + 100
#define LLINF (ll)1e18
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
mt19937
rng(chrono::steady_clock::now()
.time_since_epoch()
.count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) {
++y;
return (rng() % (y - x)) + x;
} // inclusivesss
ll n, k, p[5005], c[5005], ans = -LLINF;
ll f(ll x) {
ll res = -LLINF, cur = 0;
FOR(i, 1, k) {
x = p[x];
cur += c[x];
res = max(res, cur);
}
return res;
}
int main() {
fastio;
cin >> n >> k;
FOR(i, 1, n) cin >> p[i];
FOR(i, 1, n) cin >> c[i];
FOR(i, 1, n) ans = max(ans, f(i));
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v) \
sort(all(v)); \
v.resize(unique(all(v)) - v.begin());
#define INF (ll)1e9 + 100
#define LLINF (ll)1e18
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
mt19937
rng(chrono::steady_clock::now()
.time_since_epoch()
.count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) {
++y;
return (rng() % (y - x)) + x;
} // inclusivesss
ll n, k, p[5005], c[5005], ans = -LLINF;
ll f(ll x) {
ll res = -LLINF, cur = 0, st = x, num = 0;
while (num < k) {
num++;
st = p[st];
cur += c[st];
res = max(res, cur);
if (st == x)
break;
}
if (num == k)
return res;
cur *= (k / num - 1);
res = max(res, cur);
FOR(i, 1, num + k % num) {
st = p[st];
cur += c[st];
res = max(res, cur);
}
return res;
}
int main() {
fastio;
cin >> n >> k;
FOR(i, 1, n) cin >> p[i];
FOR(i, 1, n) cin >> c[i];
FOR(i, 1, n) ans = max(ans, f(i));
cout << ans;
}
| replace | 38 | 42 | 38 | 54 | TLE | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N, K;
cin >> N >> K;
vector<ll> P(N), C(N);
for (auto &x : P)
cin >> x, x--;
for (auto &x : C)
cin >> x;
ll res = -1e18;
for (int i = 0; i < N; i++) {
vector<ll> sum;
ll t = 0;
int idx = i;
do {
t += C[idx];
sum.push_back(t);
idx = P[idx];
} while (idx - i);
for (int j = 0; j < min<int>(K, sum.size()); j++)
res = max(res, sum[j]);
ll s = sum.back() * (K / sum.size());
ll k = K % sum.size();
for (int j = 0; j < k, sum.size(); j++) {
res = max(res, s + sum[j]);
}
}
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N, K;
cin >> N >> K;
vector<ll> P(N), C(N);
for (auto &x : P)
cin >> x, x--;
for (auto &x : C)
cin >> x;
ll res = -1e18;
for (int i = 0; i < N; i++) {
vector<ll> sum;
ll t = 0;
int idx = i;
do {
t += C[idx];
sum.push_back(t);
idx = P[idx];
} while (idx - i);
for (int j = 0; j < min<int>(K, sum.size()); j++)
res = max(res, sum[j]);
ll sz = sum.size();
for (int j = 0; j < sz; j++) {
if (j < K % sz) {
res = max(res, K / sz * sum.back() + sum[j]);
} else {
int cnt = K / sz - 1;
if (cnt < 0)
break;
res = max(res, cnt * sum.back() + sum[j]);
}
}
}
cout << res << endl;
}
| replace | 23 | 27 | 23 | 33 | TLE | |
p02585 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);
int main(void) {
ll n, k;
ll ans = -INF;
cin >> n >> k;
vector<ll> p(n), c(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
}
for (int i = 0; i < n; i++) {
cin >> c[i];
}
for (int i = 0; i < n; i++) {
ll cycle_sum = 0;
ll next_i = i;
ll cycle_cnt = 0;
while (true) {
cycle_cnt++;
next_i = p[next_i];
cycle_sum += c[next_i];
if (next_i == i) {
break;
}
}
next_i = i;
ll cnt = 0;
ll path = 0;
while (true) {
cnt++;
path += c[next_i];
if (cnt > k) {
break;
}
ll score = path + max(0ll, cycle_sum) * ((k - cnt) / cycle_cnt);
ans = max(ans, score);
next_i = p[next_i];
}
}
cout << ans << endl;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);
int main(void) {
ll n, k;
ll ans = -INF;
cin >> n >> k;
vector<ll> p(n), c(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
}
for (int i = 0; i < n; i++) {
cin >> c[i];
}
for (int i = 0; i < n; i++) {
ll cycle_sum = 0;
ll next_i = i;
ll cycle_cnt = 0;
while (true) {
cycle_cnt++;
next_i = p[next_i];
cycle_sum += c[next_i];
if (next_i == i) {
break;
}
}
next_i = i;
ll cnt = 0;
ll path = 0;
while (true) {
cnt++;
path += c[next_i];
if (cnt > k) {
break;
}
ll score = path + max(0ll, cycle_sum) * ((k - cnt) / cycle_cnt);
ans = max(ans, score);
next_i = p[next_i];
if (next_i == i) {
break;
}
}
}
cout << ans << endl;
}
| insert | 71 | 71 | 71 | 75 | TLE | |
p02585 | C++ | Runtime Error | #include "list"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// const ll mod = 998244353;
const ll mod = 1000000007;
const int inf = 1e9;
const ll linf = 1e18;
const int DX[] = {1, -1, 0, 0};
const int DY[] = {0, 0, 1, -1};
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep_from(i, j, n) for (ll i = (j); i < (n); ++i)
#define rep_rev(i, n) for (ll i = n - 1; i >= 0; --i)
#define rep_from_rev(i, j, n) for (ll i = n - 1; i >= j; --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) ll(x.size())
template <typename T> inline T chmax(T &a, const T b) {
return a = (a < b) ? b : a;
}
template <typename T> inline T chmin(T &a, const T b) {
return a = (a > b) ? b : a;
}
ll power(ll base, ll exponent) {
if (exponent % 2) {
return power(base, exponent - 1) * base % mod;
} else if (exponent) {
ll root_ans = power(base, exponent / 2);
return root_ans * root_ans % mod;
} else {
return 1;
}
}
ll inverse(ll x) { return power(x, mod - 2); }
ll gcd(ll a, ll b) {
if (a < b)
gcd(b, a);
ll r;
while (r = a % b) {
a = b;
b = r;
}
return b;
}
template <typename T> ll sum(T begin, T end) {
return accumulate(begin, end, 0ll);
}
template <typename T> struct combination {
vector<ll> fact, inv;
combination(int sz) : fact(sz + 1), inv(sz + 1) {
fact[0] = 1;
for (int i = 1; i <= sz; i++) {
fact[i] = fact[i - 1] * i % mod;
}
inv[sz] = power(fact[sz], mod - 2);
for (int i = sz - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
T C(int p, int q) const {
if (q < 0 || p < q)
return 0;
return T(fact[p] * inv[q] % mod * inv[p - q] % mod);
}
T P(int p, int q) const {
if (p < 0 || p < q)
return 0;
return T(fact[p] * inv[p - q] % mod);
}
};
using Pair = pair<int, int>;
using LPair = pair<ll, ll>;
template <ll Modulus> struct ModInt {
ll a;
constexpr ModInt(const ll x = 0) noexcept : a((x % mod + mod) % mod) {}
constexpr ll &value() noexcept { return a; }
constexpr const ll &value() const noexcept { return a; }
constexpr ModInt operator+(const ModInt x) const noexcept {
return ModInt(*this) += x;
}
constexpr ModInt operator-(const ModInt x) const noexcept {
return ModInt(*this) -= x;
}
constexpr ModInt operator*(const ModInt x) const noexcept {
return ModInt(*this) *= x;
}
constexpr ModInt operator/(const ModInt x) const noexcept {
return ModInt(*this) /= x;
}
constexpr ModInt &operator+=(const ModInt x) noexcept {
a += x.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr ModInt &operator-=(const ModInt x) noexcept {
if (a < x.a) {
a += Modulus;
}
a -= x.a;
return *this;
}
constexpr ModInt &operator*=(const ModInt x) noexcept {
a = a * x.a % Modulus;
return *this;
}
constexpr ModInt &operator/=(ModInt x) noexcept {
ll exp = Modulus - 2;
while (exp) {
if (exp % 2) {
*this *= x;
}
x *= x;
exp /= 2;
}
return *this;
}
constexpr ModInt operator-() noexcept { return ModInt(-a); }
friend ostream &operator<<(ostream &os, const ModInt &m) {
os << m.a;
return os;
}
};
using mint = ModInt<mod>;
struct UnionFind {
vector<int> par;
vector<int> next;
UnionFind(int n) : par(n, -1), next(n) { iota(all(next), 0); }
int root(int x) {
if (par[x] < 0)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
swap(next[x], next[y]);
}
private:
void unite_list(int x, int y) {}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int size(int x) { return -par[root(x)]; }
};
map<ll, int> primeFactorization(ll a) {
map<ll, int> mp;
for (ll i = 2; i * i <= a; i++) {
while (a % i == 0) {
mp[i]++;
a /= i;
}
}
if (a > 1)
mp[a]++;
return mp;
}
int main() {
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll n, k;
cin >> n >> k;
vector<int> p(n), c(n);
rep(i, n) cin >> p[i];
rep(i, n) cin >> c[i];
rep(i, n) p[i]--;
vector<int> used(n);
vector<vector<int>> cyc;
rep(i, n) {
int x = i;
if (used[x])
continue;
vector<int> a;
while (!used[x]) {
used[x] = 1;
a.push_back(x);
x = p[x];
}
cyc.push_back(a);
}
ll ans = -linf;
for (auto v : cyc) {
vector<ll> cum(2 * sz(v) + 1);
rep(i, 2 * sz(v)) cum[i + 1] = cum[i] + c[v[i % sz(v)]];
vector<ll> dp(sz(v) + 1, -linf);
rep(i, sz(v)) rep_from(j, i, 2 * sz(v))
chmax(dp[j - i + 1], cum[j + 1] - cum[i]);
rep_from(i, 1, sz(v)) chmax(dp[i + 1], dp[i]);
ll cnt = k / sz(v);
ll loop = 0;
for (int x : v)
loop += c[x];
chmax(ans, dp[min(sz(v), k)]);
if (k > sz(v)) {
chmax(ans, dp[min(sz(v), k)] + loop * (cnt - 1));
}
chmax(ans, dp[k % sz(v)] + loop * cnt);
}
cout << ans << endl;
} | #include "list"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// const ll mod = 998244353;
const ll mod = 1000000007;
const int inf = 1e9;
const ll linf = 1e18;
const int DX[] = {1, -1, 0, 0};
const int DY[] = {0, 0, 1, -1};
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep_from(i, j, n) for (ll i = (j); i < (n); ++i)
#define rep_rev(i, n) for (ll i = n - 1; i >= 0; --i)
#define rep_from_rev(i, j, n) for (ll i = n - 1; i >= j; --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) ll(x.size())
template <typename T> inline T chmax(T &a, const T b) {
return a = (a < b) ? b : a;
}
template <typename T> inline T chmin(T &a, const T b) {
return a = (a > b) ? b : a;
}
ll power(ll base, ll exponent) {
if (exponent % 2) {
return power(base, exponent - 1) * base % mod;
} else if (exponent) {
ll root_ans = power(base, exponent / 2);
return root_ans * root_ans % mod;
} else {
return 1;
}
}
ll inverse(ll x) { return power(x, mod - 2); }
ll gcd(ll a, ll b) {
if (a < b)
gcd(b, a);
ll r;
while (r = a % b) {
a = b;
b = r;
}
return b;
}
template <typename T> ll sum(T begin, T end) {
return accumulate(begin, end, 0ll);
}
template <typename T> struct combination {
vector<ll> fact, inv;
combination(int sz) : fact(sz + 1), inv(sz + 1) {
fact[0] = 1;
for (int i = 1; i <= sz; i++) {
fact[i] = fact[i - 1] * i % mod;
}
inv[sz] = power(fact[sz], mod - 2);
for (int i = sz - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
T C(int p, int q) const {
if (q < 0 || p < q)
return 0;
return T(fact[p] * inv[q] % mod * inv[p - q] % mod);
}
T P(int p, int q) const {
if (p < 0 || p < q)
return 0;
return T(fact[p] * inv[p - q] % mod);
}
};
using Pair = pair<int, int>;
using LPair = pair<ll, ll>;
template <ll Modulus> struct ModInt {
ll a;
constexpr ModInt(const ll x = 0) noexcept : a((x % mod + mod) % mod) {}
constexpr ll &value() noexcept { return a; }
constexpr const ll &value() const noexcept { return a; }
constexpr ModInt operator+(const ModInt x) const noexcept {
return ModInt(*this) += x;
}
constexpr ModInt operator-(const ModInt x) const noexcept {
return ModInt(*this) -= x;
}
constexpr ModInt operator*(const ModInt x) const noexcept {
return ModInt(*this) *= x;
}
constexpr ModInt operator/(const ModInt x) const noexcept {
return ModInt(*this) /= x;
}
constexpr ModInt &operator+=(const ModInt x) noexcept {
a += x.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr ModInt &operator-=(const ModInt x) noexcept {
if (a < x.a) {
a += Modulus;
}
a -= x.a;
return *this;
}
constexpr ModInt &operator*=(const ModInt x) noexcept {
a = a * x.a % Modulus;
return *this;
}
constexpr ModInt &operator/=(ModInt x) noexcept {
ll exp = Modulus - 2;
while (exp) {
if (exp % 2) {
*this *= x;
}
x *= x;
exp /= 2;
}
return *this;
}
constexpr ModInt operator-() noexcept { return ModInt(-a); }
friend ostream &operator<<(ostream &os, const ModInt &m) {
os << m.a;
return os;
}
};
using mint = ModInt<mod>;
struct UnionFind {
vector<int> par;
vector<int> next;
UnionFind(int n) : par(n, -1), next(n) { iota(all(next), 0); }
int root(int x) {
if (par[x] < 0)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
swap(next[x], next[y]);
}
private:
void unite_list(int x, int y) {}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int size(int x) { return -par[root(x)]; }
};
map<ll, int> primeFactorization(ll a) {
map<ll, int> mp;
for (ll i = 2; i * i <= a; i++) {
while (a % i == 0) {
mp[i]++;
a /= i;
}
}
if (a > 1)
mp[a]++;
return mp;
}
int main() {
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll n, k;
cin >> n >> k;
vector<int> p(n), c(n);
rep(i, n) cin >> p[i];
rep(i, n) cin >> c[i];
rep(i, n) p[i]--;
vector<int> used(n);
vector<vector<int>> cyc;
rep(i, n) {
int x = i;
if (used[x])
continue;
vector<int> a;
while (!used[x]) {
used[x] = 1;
a.push_back(x);
x = p[x];
}
cyc.push_back(a);
}
ll ans = -linf;
for (auto v : cyc) {
vector<ll> cum(2 * sz(v) + 1);
rep(i, 2 * sz(v)) cum[i + 1] = cum[i] + c[v[i % sz(v)]];
vector<ll> dp(2 * sz(v) + 1, -linf);
rep(i, sz(v)) rep_from(j, i, 2 * sz(v))
chmax(dp[j - i + 1], cum[j + 1] - cum[i]);
rep_from(i, 1, sz(v)) chmax(dp[i + 1], dp[i]);
ll cnt = k / sz(v);
ll loop = 0;
for (int x : v)
loop += c[x];
chmax(ans, dp[min(sz(v), k)]);
if (k > sz(v)) {
chmax(ans, dp[min(sz(v), k)] + loop * (cnt - 1));
}
chmax(ans, dp[k % sz(v)] + loop * cnt);
}
cout << ans << endl;
} | replace | 240 | 241 | 240 | 241 | 0 | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll n, k;
ll r;
cin >> n >> k;
vector<vector<ll>> v(n);
vector<ll> p(n), c(n), siz(n, 0);
for (ll &i : p)
cin >> i, --i;
for (ll &i : c)
cin >> i;
r = c[0];
for (ll i = 0; i < n; ++i) {
ll cur = i, sum = 0;
do {
++siz[i];
v[i].push_back(sum += c[cur]);
cur = p[cur];
} while (cur != i);
}
for (ll i = 0; i < n; ++i) {
ll kt = k, s = 0, tail = v[i].back();
if (kt > siz[i] * 2 && tail > 0) {
s = tail * (k / siz[i] - 1);
kt = k % siz[i] + siz[i];
}
for (ll j = 0; j < kt; ++j)
r = max(r, s + v[i][j % siz[i]] + tail * (j >= siz[i]));
}
cout << r;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll n, k;
ll r;
cin >> n >> k;
vector<vector<ll>> v(n);
vector<ll> p(n), c(n), siz(n, 0);
for (ll &i : p)
cin >> i, --i;
for (ll &i : c)
cin >> i;
r = c[0];
for (ll i = 0; i < n; ++i) {
ll cur = i, sum = 0;
do {
++siz[i];
v[i].push_back(sum += c[cur]);
cur = p[cur];
} while (cur != i);
}
for (ll i = 0; i < n; ++i) {
ll kt = k, s = 0, tail = v[i].back();
if (k > siz[i] * 2) {
if (tail > 0) {
s = tail * (k / siz[i] - 1);
kt = k % siz[i] + siz[i];
} else
kt = siz[i];
}
for (ll j = 0; j < kt; ++j)
r = max(r, s + v[i][j % siz[i]] + tail * (j >= siz[i]));
}
cout << r;
} | replace | 26 | 29 | 26 | 32 | TLE | |
p02585 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
// #pragma warning(disable : 4996)
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif
#include <limits.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
// #define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) \
REP(i, v.size()) cout << v[i] << " \n"[i == (int)v.size() - 1]
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
// int dx[4] = { 0,0,-1,1 }, dy[4] = { 1,-1,0,0 };
void solve() {
int N, K;
cin >> N >> K;
vector<int> P(N), C(N);
REP(i, N) {
cin >> P[i];
P[i]--;
}
REP(i, N) cin >> C[i];
ll ans = -LINF;
vector<int> cnt(N), sum(N);
REP(i, N) {
int pos = i;
int res = 0;
REP(_, N) {
pos = P[pos];
res += C[pos];
cnt[i]++;
if (pos == i)
break;
}
sum[i] = res;
}
REP(i, N) {
ll res = 0;
if (sum[i] > 0 && K > cnt[i] * 2) {
ll c = K / cnt[i] - 1;
res += c * sum[i];
int rem = K - c * cnt[i];
if (c > 0)
chmax(ans, res);
int pos = i;
REP(_, rem) {
pos = P[pos];
res += C[pos];
chmax(ans, res);
}
} else {
int pos = i;
REP(_, K) {
pos = P[pos];
res += C[pos];
chmax(ans, res);
}
}
}
print(ans);
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
// int q;
// cin >> q;
// while (q--)
solve();
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
// #pragma warning(disable : 4996)
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif
#include <limits.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
// #define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) \
REP(i, v.size()) cout << v[i] << " \n"[i == (int)v.size() - 1]
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
// int dx[4] = { 0,0,-1,1 }, dy[4] = { 1,-1,0,0 };
void solve() {
int N, K;
cin >> N >> K;
vector<int> P(N), C(N);
REP(i, N) {
cin >> P[i];
P[i]--;
}
REP(i, N) cin >> C[i];
ll ans = -LINF;
vector<int> cnt(N), sum(N);
REP(i, N) {
int pos = i;
int res = 0;
REP(_, N) {
pos = P[pos];
res += C[pos];
cnt[i]++;
if (pos == i)
break;
}
sum[i] = res;
}
REP(i, N) {
ll res = 0;
if (K > cnt[i] * 2) {
if (sum[i] > 0 && K > cnt[i] * 2) {
ll c = K / cnt[i] - 1;
res += c * sum[i];
int rem = K - c * cnt[i];
if (c > 0)
chmax(ans, res);
int pos = i;
REP(_, rem) {
pos = P[pos];
res += C[pos];
chmax(ans, res);
}
} else {
int pos = i;
REP(_, min(N, K)) {
pos = P[pos];
res += C[pos];
chmax(ans, res);
}
}
} else {
int pos = i;
REP(_, K) {
pos = P[pos];
res += C[pos];
chmax(ans, res);
}
}
}
print(ans);
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
// int q;
// cin >> q;
// while (q--)
solve();
}
| replace | 104 | 115 | 104 | 124 | TLE | |
p02585 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MOD (long long)(1e9 + 7)
using namespace std;
long long n, k, vis[5005];
long long p[5005], c[5005], sum[5005], ans = -1e18;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> p[i];
for (int i = 1; i <= n; i++)
cin >> c[i];
for (int i = 1; i <= n; i++) {
long long s = c[i];
vector<long long> ti;
ti.push_back(s);
for (int j = p[i]; j != i; j = p[i]) {
s += c[j];
ti.push_back(s);
}
int len = ti.size();
for (int j = 0; j < len; j++) {
long long rest = k - j - 1;
if (rest < 0)
break;
ans = max(ans, rest / len * max(0LL, s) + ti[j]);
}
}
cout << ans;
}
| #include <bits/stdc++.h>
#define MOD (long long)(1e9 + 7)
using namespace std;
long long n, k, vis[5005];
long long p[5005], c[5005], sum[5005], ans = -1e18;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> p[i];
for (int i = 1; i <= n; i++)
cin >> c[i];
for (int i = 1; i <= n; i++) {
long long s = c[i];
vector<long long> ti;
ti.push_back(s);
for (int j = p[i]; j != i; j = p[j]) {
s += c[j];
ti.push_back(s);
}
int len = ti.size();
for (int j = 0; j < len; j++) {
long long rest = k - j - 1;
if (rest < 0)
break;
ans = max(ans, rest / len * max(0LL, s) + ti[j]);
}
}
cout << ans;
}
| replace | 18 | 19 | 18 | 19 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02585 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e2 + 9;
const double ep = 1e-7;
const int mod = 998244353;
const double pi = acos(-1);
const int INF = -1e9;
#define mk make_pair
#define PII pair<int, int>
#define PIII pair<int, PII>
#define PIL pair<int, ll>
#define PLI pair<ll, int>
#define eb emplace_back
typedef long long ll;
ll n, k, p[maxn], c[maxn], ans = -1e9;
int main() {
scanf("%lld%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%lld", &p[i]);
for (int i = 1; i <= n; i++)
scanf("%lld", &c[i]);
for (int i = 1; i <= n; i++) {
ll now = i, num = 0, sum = 0;
do {
now = p[now];
sum += c[now];
num++;
} while (now != i);
now = i;
ll ss = 0;
for (int j = 1; j <= min(k, num); j++) {
now = p[now];
ss += c[now];
ans = max(ans, ss);
ans = max(ans, ss + sum * ((k - j) / num));
}
}
printf("%lld\n", ans);
} | #include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 9;
const double ep = 1e-7;
const int mod = 998244353;
const double pi = acos(-1);
const int INF = -1e9;
#define mk make_pair
#define PII pair<int, int>
#define PIII pair<int, PII>
#define PIL pair<int, ll>
#define PLI pair<ll, int>
#define eb emplace_back
typedef long long ll;
ll n, k, p[maxn], c[maxn], ans = -1e9;
int main() {
scanf("%lld%lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%lld", &p[i]);
for (int i = 1; i <= n; i++)
scanf("%lld", &c[i]);
for (int i = 1; i <= n; i++) {
ll now = i, num = 0, sum = 0;
do {
now = p[now];
sum += c[now];
num++;
} while (now != i);
now = i;
ll ss = 0;
for (int j = 1; j <= min(k, num); j++) {
now = p[now];
ss += c[now];
ans = max(ans, ss);
ans = max(ans, ss + sum * ((k - j) / num));
}
}
printf("%lld\n", ans);
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pii pair<int, int>
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0);
#define fio \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define vll vector<long long int>
#define vpii vector<pair<int, int>>
#define dsc [](int a, int b) -> bool { return a > b; }
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define MAX 1000000000000000000
#define test \
int t; \
cin >> t;
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
using namespace std;
ll nxt() {
ll x;
cin >> x;
return x;
}
ll it;
void dfs(int i, int j) {
it++;
if (i + 1 < 3000)
dfs(i + 1, j);
if (j + 1 < 3000)
dfs(i, j + 1);
}
int main() {
#ifndef ONLINE_JUDGE
fio
#endif
IOS
ll n = nxt(),
k = nxt();
vector<ll> p(n), c(n);
generate(all(p), nxt);
generate(all(c), nxt);
vector<bool> visited(n, 0);
vector<vector<ll>> groups;
ll res = LLONG_MIN;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
visited[i] = true;
vector<ll> g;
int j = i;
while (!visited[p[j] - 1]) {
j = p[j] - 1;
visited[j] = true;
g.pb(c[j]);
}
g.pb(c[i]);
groups.pb(g);
}
}
for (vector<ll> g : groups) {
ll m = g.size();
for (int i = 0; i < m; i++) {
ll tmp = 0;
ll kk = k;
int j = i;
while (kk--) {
tmp += g[j % m];
j++;
res = max(res, tmp);
}
}
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pii pair<int, int>
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0);
#define fio \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define vll vector<long long int>
#define vpii vector<pair<int, int>>
#define dsc [](int a, int b) -> bool { return a > b; }
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define MAX 1000000000000000000
#define test \
int t; \
cin >> t;
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
using namespace std;
ll nxt() {
ll x;
cin >> x;
return x;
}
ll it;
void dfs(int i, int j) {
it++;
if (i + 1 < 3000)
dfs(i + 1, j);
if (j + 1 < 3000)
dfs(i, j + 1);
}
int main() {
#ifndef ONLINE_JUDGE
fio
#endif
IOS
ll n = nxt(),
k = nxt();
vector<ll> p(n), c(n);
generate(all(p), nxt);
generate(all(c), nxt);
vector<bool> visited(n, 0);
vector<vector<ll>> groups;
ll res = LLONG_MIN;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
visited[i] = true;
vector<ll> g;
int j = i;
while (!visited[p[j] - 1]) {
j = p[j] - 1;
visited[j] = true;
g.pb(c[j]);
}
g.pb(c[i]);
groups.pb(g);
}
}
for (vector<ll> g : groups) {
ll m = g.size();
if (m > k) {
for (int i = 0; i < m; i++) {
ll kk = k;
int j = i;
ll tmp = 0;
while (kk--) {
tmp += g[j % m];
j++;
res = max(res, tmp);
}
}
} else {
for (int i = 0; i < m; i++) {
ll kk = m;
int j = i;
ll tmp = 0;
while (kk--) {
tmp += g[j % m];
j++;
res = max(res, tmp);
}
}
ll tot = 0;
for (int i = 0; i < m; i++) {
tot += g[i];
res = max(res, tot);
}
if (k / m > 1) {
ll ttot = max(tot, (k / m - 1) * tot);
for (int i = 0; i < m; i++) {
ll kk = m;
int j = i;
ll tmp = 0;
while (kk--) {
tmp += g[j % m];
j++;
res = max(res, tmp);
res = max(res, ttot + tmp);
}
}
}
tot = max(tot, (k / m) * tot);
res = max(res, tot);
ll rem = k % m;
for (int i = 0; i < m; i++) {
ll kk = rem;
int j = i;
ll tmp = 0;
while (kk--) {
tmp += g[j % m];
j++;
res = max(res, tmp);
res = max(res, tot + tmp);
}
}
}
}
cout << res << endl;
return 0;
} | replace | 73 | 81 | 73 | 130 | TLE | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;
#define endl '\n'
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char c) { return string(1, c); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (int i = int(N) - 1; i >= 0; --i) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef COMPETITIVE_PROGRAMMING_LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...) \
if (0) { \
}
#endif
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll N, K;
cin >> N >> K;
vector<ll> P(N), C(N);
for (auto &_a : P)
cin >> _a, --_a;
for (auto &_a : C)
cin >> _a;
vector<ll> visited(N);
vector<ll> sum(N);
ll ans = *max_element(C.begin(), C.end());
for (ll start = 0; start < N; ++start) {
fill(visited.begin(), visited.end(), -1);
fill(sum.begin(), sum.end(), 0);
ll pos = start;
ll score = 0;
ll k = 0;
while (k < K) {
pos = P[pos];
if (visited[pos] != -1) {
// on the last cycle we don't need to perform full cycle
if (score - sum[pos] > 0) {
ll n_step_in_cycle = k - visited[pos];
ll n_step_remain = K - k;
ll n_cycle = n_step_remain / n_step_in_cycle;
// dbg(pos, visited[pos], sum[pos], score, n_step_remain, K,
// k);
n_cycle = max(0LL, n_cycle - 1);
k += n_cycle * n_step_in_cycle;
score += n_cycle * (score - sum[pos]);
ans = max(ans, score);
while (k < K) {
score += C[pos];
ans = max(ans, score);
++k;
pos = P[pos];
}
} else {
// continue while positive, do one last loop in case start of cycle is
// positive
ll idx = 0;
while (k < K && idx < 2 * N) {
score += C[pos];
ans = max(ans, score);
pos = P[pos];
++idx;
++k;
}
}
} else {
sum[pos] = score;
score += C[pos];
ans = max(ans, score);
visited[pos] = k;
}
++k;
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;
#define endl '\n'
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char c) { return string(1, c); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (int i = int(N) - 1; i >= 0; --i) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef COMPETITIVE_PROGRAMMING_LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...) \
if (0) { \
}
#endif
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll N, K;
cin >> N >> K;
vector<ll> P(N), C(N);
for (auto &_a : P)
cin >> _a, --_a;
for (auto &_a : C)
cin >> _a;
vector<ll> visited(N);
vector<ll> sum(N);
ll ans = *max_element(C.begin(), C.end());
for (ll start = 0; start < N; ++start) {
fill(visited.begin(), visited.end(), -1);
fill(sum.begin(), sum.end(), 0);
ll pos = start;
ll score = 0;
ll k = 0;
while (k < K) {
pos = P[pos];
if (visited[pos] != -1) {
// on the last cycle we don't need to perform full cycle
if (score - sum[pos] > 0) {
ll n_step_in_cycle = k - visited[pos];
ll n_step_remain = K - k;
ll n_cycle = n_step_remain / n_step_in_cycle;
// dbg(pos, visited[pos], sum[pos], score, n_step_remain, K,
// k);
n_cycle = max(0LL, n_cycle - 1);
k += n_cycle * n_step_in_cycle;
score += n_cycle * (score - sum[pos]);
ans = max(ans, score);
while (k < K) {
score += C[pos];
ans = max(ans, score);
++k;
pos = P[pos];
}
} else {
// continue while positive, do one last loop in case start of cycle is
// positive
ll idx = 0;
while (k < K && idx < 2 * N) {
score += C[pos];
ans = max(ans, score);
pos = P[pos];
++idx;
++k;
}
}
break;
} else {
sum[pos] = score;
score += C[pos];
ans = max(ans, score);
visited[pos] = k;
}
++k;
}
}
cout << ans << endl;
}
| insert | 148 | 148 | 148 | 149 | TLE | |
p02585 | Python | Runtime Error | n, k = [int(z) for z in input().strip().split()]
p = [int(z) - 1 for z in input().strip().split()]
c = [int(z) for z in input().strip().split()]
visited = [False for _ in range(n)]
cycle = []
for start in range(n):
if visited[start]:
continue
now = start
route = []
while not visited[now]:
visited[now] = True
route.append(now)
now = p[now]
cycle.append(route)
min_c = min(c)
max_value = min_c
for route in cycle:
full_value = sum([c[i] for i in route])
double_route = route + route
v = [c[i] for i in double_route]
max_sum_step = []
for step in range(1, len(route)):
s = sum(v[:step])
max_sum = max(s, min_c)
for idx in range(step, step + len(route) - 1):
s += v[idx]
if idx >= step:
s -= v[idx - step]
max_sum = max(s, max_sum)
max_sum_step.append(max_sum)
addition = k % len(route)
if full_value > 0:
value1 = int(k / len(route)) * full_value + max(max_sum_step[:addition])
value2 = (int(k / len(route)) - 1) * full_value + max(max_sum_step)
value = max(value1, value2)
else:
value = max(max_sum_step[: min(k, len(max_sum_step))])
max_value = max(max_value, value)
print(max_value)
| n, k = [int(z) for z in input().strip().split()]
p = [int(z) - 1 for z in input().strip().split()]
c = [int(z) for z in input().strip().split()]
visited = [False for _ in range(n)]
cycle = []
for start in range(n):
if visited[start]:
continue
now = start
route = []
while not visited[now]:
visited[now] = True
route.append(now)
now = p[now]
cycle.append(route)
min_c = min(c)
max_value = min_c
for route in cycle:
full_value = sum([c[i] for i in route])
double_route = route + route
v = [c[i] for i in double_route]
max_sum_step = []
for step in range(1, len(route)):
s = sum(v[:step])
max_sum = max(s, min_c)
for idx in range(step, step + len(route) - 1):
s += v[idx]
if idx >= step:
s -= v[idx - step]
max_sum = max(s, max_sum)
max_sum_step.append(max_sum)
addition = k % len(route)
if full_value > 0:
addition_value = 0 if addition == 0 else max(max_sum_step[:addition])
value1 = int(k / len(route)) * full_value + addition_value
value2 = (int(k / len(route)) - 1) * full_value + max(max_sum_step)
value = max(value1, value2)
else:
value = max(max_sum_step[: min(k, len(max_sum_step))])
max_value = max(max_value, value)
print(max_value)
| replace | 35 | 36 | 35 | 37 | 0 | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> p(n);
vector<ll> c(n);
for (ll i = 0; i < n; i++) {
ll _p;
cin >> _p;
p[i] = _p - 1;
}
for (ll i = 0; i < n; i++) {
cin >> c[i];
}
ll best = -INFINITY;
for (ll start = 0; start < n; start++) {
set<ll> steped;
ll score = 0;
ll mode = 0;
ll turnparloop = INFINITY;
ll scoreparloop = 0;
ll loopstart = -1;
ll _turnls = 0;
ll _scorels = 0;
ll now = start;
ll turn = 0;
for (; turn < k; turn++) {
if (mode == 0 && steped.count(now)) {
mode = 1;
_turnls = turn;
_scorels = score;
loopstart = now;
} else if (mode == 1 && loopstart == now) {
mode = 2;
scoreparloop = score - _scorels;
turnparloop = turn - _turnls;
break;
}
if (mode == 0)
steped.insert(now);
now = p[now];
score += c[now];
best = max(best, score);
}
if (scoreparloop > 0) {
ll loops = max(0ll, (k - turn - 1) / turnparloop - 1);
score += scoreparloop * loops;
turn += turnparloop * loops;
best = max(best, score);
for (; turn < k; turn++) {
now = p[now];
score += c[now];
best = max(best, score);
}
}
}
cout << best;
return 0;
} | #include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> p(n);
vector<ll> c(n);
for (ll i = 0; i < n; i++) {
ll _p;
cin >> _p;
p[i] = _p - 1;
}
for (ll i = 0; i < n; i++) {
cin >> c[i];
}
ll best = -INFINITY;
for (ll start = 0; start < n; start++) {
unordered_set<ll> steped;
ll score = 0;
ll mode = 0;
ll turnparloop = INFINITY;
ll scoreparloop = 0;
ll loopstart = -1;
ll _turnls = 0;
ll _scorels = 0;
ll now = start;
ll turn = 0;
for (; turn < k; turn++) {
if (mode == 0 && steped.count(now)) {
mode = 1;
_turnls = turn;
_scorels = score;
loopstart = now;
} else if (mode == 1 && loopstart == now) {
mode = 2;
scoreparloop = score - _scorels;
turnparloop = turn - _turnls;
break;
}
if (mode == 0)
steped.insert(now);
now = p[now];
score += c[now];
best = max(best, score);
}
if (scoreparloop > 0) {
ll loops = max(0ll, (k - turn - 1) / turnparloop - 1);
score += scoreparloop * loops;
turn += turnparloop * loops;
best = max(best, score);
for (; turn < k; turn++) {
now = p[now];
score += c[now];
best = max(best, score);
}
}
}
cout << best;
return 0;
}
| replace | 19 | 20 | 19 | 20 | TLE | |
p02585 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5010;
ll c[N], p[N], q[N << 1], u[N << 1];
vector<ll> V[N];
bool vis[N];
int main() {
ll n, k;
scanf("%lld %lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%lld", &p[i]);
for (int i = 1; i <= n; i++)
scanf("%lld", &c[i]);
int now = 0;
for (int i = 1; i <= n; i++) {
if (vis[i])
continue;
int x = i;
now++;
while (!vis[x]) {
vis[x] = true;
V[now].push_back(c[x]);
x = p[x];
}
}
ll ans = -2e18;
for (int i = 1; i <= now; i++) {
ll sum = 0, sum1 = 0, k1 = k, siz = V[i].size();
int num = 0;
for (auto it : V[i])
q[++num] = it;
for (auto it : V[i])
q[++num] = it;
for (int j = 1; j <= k1; j++) {
ll po = 0, mx = -2e18, no = 0;
for (int l = 1; l <= num; l++) {
no += q[l];
if (l - po > j)
no -= q[++po];
mx = max(mx, no);
}
u[j] = mx;
ans = max(ans, mx);
}
for (auto it : V[i])
sum += it;
ll t = (k1 - 1) / siz;
k1 -= t * siz;
if (sum > 0)
sum1 += t * sum;
for (int j = 1; j <= k1; j++)
ans = max(ans, sum1 + u[j]);
}
printf("%lld\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5010;
ll c[N], p[N], q[N << 1], u[N << 1];
vector<ll> V[N];
bool vis[N];
int main() {
ll n, k;
scanf("%lld %lld", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%lld", &p[i]);
for (int i = 1; i <= n; i++)
scanf("%lld", &c[i]);
int now = 0;
for (int i = 1; i <= n; i++) {
if (vis[i])
continue;
int x = i;
now++;
while (!vis[x]) {
vis[x] = true;
V[now].push_back(c[x]);
x = p[x];
}
}
ll ans = -2e18;
for (int i = 1; i <= now; i++) {
ll sum = 0, sum1 = 0, k1 = k, siz = V[i].size();
int num = 0;
for (auto it : V[i])
q[++num] = it;
for (auto it : V[i])
q[++num] = it;
for (int j = 1; j <= min(siz, k1); j++) {
ll po = 0, mx = -2e18, no = 0;
for (int l = 1; l <= num; l++) {
no += q[l];
if (l - po > j)
no -= q[++po];
mx = max(mx, no);
}
u[j] = mx;
ans = max(ans, mx);
}
for (auto it : V[i])
sum += it;
ll t = (k1 - 1) / siz;
k1 -= t * siz;
if (sum > 0)
sum1 += t * sum;
for (int j = 1; j <= k1; j++)
ans = max(ans, sum1 + u[j]);
}
printf("%lld\n", ans);
return 0;
}
| replace | 35 | 36 | 35 | 36 | 0 | |
p02585 | C++ | Time Limit Exceeded | #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
int n, k;
int p[5000];
int c[5000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
rep(i, n) cin >> p[i], p[i]--;
rep(i, n) cin >> c[i];
long long ans = *max_element(c, c + n);
rep(i, n) {
unordered_map<int, pair<int, long long>> vis;
vis.reserve(n);
int cur = i;
long long s = 0;
rep(j, k) {
s += c[cur];
cur = p[cur];
ans = max(ans, s);
auto it = vis.find(cur);
if (it != vis.end()) {
int cyc = j - it->second.first;
long long cycs = s - it->second.second;
ans = max(ans, s + (k - j - 1) / cyc * cycs);
}
vis[cur] = make_pair(j, s);
}
}
cout << ans << endl;
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
int n, k;
int p[5000];
int c[5000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
rep(i, n) cin >> p[i], p[i]--;
rep(i, n) cin >> c[i];
long long ans = *max_element(c, c + n);
rep(i, n) {
unordered_map<int, pair<int, long long>> vis;
vis.reserve(n);
int cur = i;
long long s = 0;
rep(j, min(2 * n, k)) {
s += c[cur];
cur = p[cur];
ans = max(ans, s);
auto it = vis.find(cur);
if (it != vis.end()) {
int cyc = j - it->second.first;
long long cycs = s - it->second.second;
ans = max(ans, s + (k - j - 1) / cyc * cycs);
}
vis[cur] = make_pair(j, s);
}
}
cout << ans << endl;
return 0;
}
| replace | 48 | 49 | 48 | 49 | TLE | |
p02585 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define INPUT freopen("input.txt", "r", stdin);
#define ll long long
#define ld long double
#define len(s) s.size()
#define fl(st, en, in) for (int i = st; i < en; i += in)
#define rfl(st, en, de) for (int i = st; i < en; i -= de)
#define fll fl(0, n, 1)
#define cy cout << "Yes" << endl;
#define cn cout << "No" << endl;
#define ci(s) cin >> s
#define co(s) cout << s << endl
#define vl vector<ll>
#define pb(x) push_back(x)
#define ms(arr, x) memset(arr, x, sizeof(arr))
#define inf INT_MAX
int main() {
INPUT;
ll n, m;
ci(n);
ci(m);
ll ar[n];
ll cost[n];
ll lt[n];
ll ans = -inf;
ms(lt, 0);
fll { ci(ar[i]); }
fll { ci(cost[i]); }
fll {
ll st = ar[i];
ll en = ar[i];
ll cnt = 0;
vl vec;
// co("looping...");
ll sum = 0;
while (true) {
sum += cost[st - 1];
vec.pb(sum);
ll tm = ar[st - 1];
cnt++;
if (tm == en) {
break;
}
st = tm;
}
lt[i] = cnt; // f
ll temp_ans = -inf;
// cout << "count = " << cnt << endl;
if (m <= cnt) {
fl(0, m, 1) { temp_ans = max(temp_ans, vec[i]); }
} else {
ll d = m / cnt;
ll r = m % cnt;
ll chashed = -inf;
if (vec[len(vec) - 1] >= 0) {
chashed = d * vec[len(vec) - 1];
temp_ans = chashed;
fl(0, r, 1) { temp_ans = max(temp_ans, vec[i] + chashed); }
ll tm2 = -inf;
fl(0, cnt, 1) { tm2 = max(tm2, vec[i]); }
ll tm3 = chashed;
fl(0, cnt, 1) { tm3 = max(tm3, vec[i] + chashed - vec[len(vec) - 1]); }
temp_ans = max(max(temp_ans, tm2), tm3);
} else {
fl(0, len(vec), 1) { temp_ans = max(temp_ans, vec[i]); }
}
}
ans = max(ans, temp_ans);
fl(0, len(vec), 1) {
// cout << vec[i] << " ";
}
// cout << endl;
}
co(ans);
// co("Here is the number of elements");
fll {
// co(lt[i]);
}
} | #include <bits/stdc++.h>
using namespace std;
#define INPUT freopen("input.txt", "r", stdin);
#define ll long long
#define ld long double
#define len(s) s.size()
#define fl(st, en, in) for (int i = st; i < en; i += in)
#define rfl(st, en, de) for (int i = st; i < en; i -= de)
#define fll fl(0, n, 1)
#define cy cout << "Yes" << endl;
#define cn cout << "No" << endl;
#define ci(s) cin >> s
#define co(s) cout << s << endl
#define vl vector<ll>
#define pb(x) push_back(x)
#define ms(arr, x) memset(arr, x, sizeof(arr))
#define inf INT_MAX
int main() {
// INPUT;
ll n, m;
ci(n);
ci(m);
ll ar[n];
ll cost[n];
ll lt[n];
ll ans = -inf;
ms(lt, 0);
fll { ci(ar[i]); }
fll { ci(cost[i]); }
fll {
ll st = ar[i];
ll en = ar[i];
ll cnt = 0;
vl vec;
// co("looping...");
ll sum = 0;
while (true) {
sum += cost[st - 1];
vec.pb(sum);
ll tm = ar[st - 1];
cnt++;
if (tm == en) {
break;
}
st = tm;
}
lt[i] = cnt; // f
ll temp_ans = -inf;
// cout << "count = " << cnt << endl;
if (m <= cnt) {
fl(0, m, 1) { temp_ans = max(temp_ans, vec[i]); }
} else {
ll d = m / cnt;
ll r = m % cnt;
ll chashed = -inf;
if (vec[len(vec) - 1] >= 0) {
chashed = d * vec[len(vec) - 1];
temp_ans = chashed;
fl(0, r, 1) { temp_ans = max(temp_ans, vec[i] + chashed); }
ll tm2 = -inf;
fl(0, cnt, 1) { tm2 = max(tm2, vec[i]); }
ll tm3 = chashed;
fl(0, cnt, 1) { tm3 = max(tm3, vec[i] + chashed - vec[len(vec) - 1]); }
temp_ans = max(max(temp_ans, tm2), tm3);
} else {
fl(0, len(vec), 1) { temp_ans = max(temp_ans, vec[i]); }
}
}
ans = max(ans, temp_ans);
fl(0, len(vec), 1) {
// cout << vec[i] << " ";
}
// cout << endl;
}
co(ans);
// co("Here is the number of elements");
fll {
// co(lt[i]);
}
} | replace | 27 | 28 | 27 | 28 | 0 | |
p02585 | C++ | Runtime Error | /*
ID: Loxilante
Time: 2020/08/16
Prog: ABC175D
Lang: cpp
*/
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = l; i < r; i++)
#define hrp(i, l, r) for (register int i = l; i <= r; i++)
#define rev(i, r, l) for (register int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void) {
tn k;
cin >> k;
return k;
}
const int U = 5e3 + 50;
const int INF = 1e18;
int to[U], val[U], ct[U], t;
bool visited[U];
vector<int> loop;
inline void dfs(int e) {
if (visited[e])
return;
visited[e] = 1;
t += val[e];
loop.pb(t);
dfs(to[e]);
}
signed main(void) {
clock_t Begin = clock();
#ifdef JOEON
// freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
// freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, k, ans = -INF;
cin >> n >> k;
rep(i, 0, n) to[i] = next<int>() - 1;
rep(i, 0, n) cin >> val[i];
clock_t InputFinished = clock();
rep(i, 0, n) {
ms(visited, 0);
loop.clear();
t = 0;
dfs(i);
int times = k / loop.size();
if (loop.back() > 0 && k >= loop.size()) {
if (times >= 1) {
int ans1 = -INF;
rep(j, 0, loop.size()) ans1 = max(ans1, loop[j]);
ans = max(loop.back() * (times - 1) + ans1, ans);
}
int rest = k % loop.size(), ans2 = 0;
rep(j, 0, rest) ans2 = max(ans2, loop[j]);
ans = max(loop.back() * times + ans2, ans);
} else
rep(j, 0, k) ans = max(ans, loop[j]);
}
cout << ans << endl;
clock_t End = clock();
D((double)(End - Begin) / CLOCKS_PER_SEC);
D((double)(End - InputFinished) / CLOCKS_PER_SEC);
return 0;
}
/*
*/ | /*
ID: Loxilante
Time: 2020/08/16
Prog: ABC175D
Lang: cpp
*/
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = l; i < r; i++)
#define hrp(i, l, r) for (register int i = l; i <= r; i++)
#define rev(i, r, l) for (register int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void) {
tn k;
cin >> k;
return k;
}
const int U = 5e3 + 50;
const int INF = 1e18;
int to[U], val[U], ct[U], t;
bool visited[U];
vector<int> loop;
inline void dfs(int e) {
if (visited[e])
return;
visited[e] = 1;
t += val[e];
loop.pb(t);
dfs(to[e]);
}
signed main(void) {
clock_t Begin = clock();
#ifdef JOEON
// freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
// freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, k, ans = -INF;
cin >> n >> k;
rep(i, 0, n) to[i] = next<int>() - 1;
rep(i, 0, n) cin >> val[i];
clock_t InputFinished = clock();
rep(i, 0, n) {
ms(visited, 0);
loop.clear();
t = 0;
dfs(i);
int times = k / loop.size();
if (loop.back() > 0 && k >= loop.size()) {
if (times >= 1) {
int ans1 = -INF;
rep(j, 0, loop.size()) ans1 = max(ans1, loop[j]);
ans = max(loop.back() * (times - 1) + ans1, ans);
}
int rest = k % loop.size(), ans2 = 0;
rep(j, 0, rest) ans2 = max(ans2, loop[j]);
ans = max(loop.back() * times + ans2, ans);
} else
rep(j, 0, k) {
if (j >= loop.size())
break;
ans = max(ans, loop[j]);
}
}
cout << ans << endl;
clock_t End = clock();
D((double)(End - Begin) / CLOCKS_PER_SEC);
D((double)(End - InputFinished) / CLOCKS_PER_SEC);
return 0;
}
/*
*/ | replace | 75 | 76 | 75 | 80 | 0 | |
p02585 | C++ | Runtime Error | /*input
5 2
2 4 5 1 3
3 4 -10 -8 8
*/
/*
______________
| )
| ) /
|______/
| | \
| | ) \
|_____|_______)
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long double ld;
#define pb push_back
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
int mod = 1000000007;
const int inf = 1034567891;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#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
#define out(container) \
for (auto it : container) \
cout << it << " "; \
cout << endl;
template <typename T> T GCD(T a, T b) {
ll t;
while (a) {
t = a;
a = b % a;
b = t;
}
return b;
}
template <typename T> string toString(T a) { return to_string(a); }
template <typename T> void toInt(string s, T &x) {
stringstream str(s);
str >> x;
}
inline int add(int x, int y) {
x += y;
if (x >= mod)
x -= mod;
return x;
}
inline int sub(int x, int y) {
x -= y;
if (x < 0)
x += mod;
return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
inline int powr(int a, ll b) {
int x = 1 % mod;
while (b) {
if (b & 1)
x = mul(x, a);
a = mul(a, a);
b >>= 1;
}
return x;
}
inline int inv(int a) { return powr(a, mod - 2); }
const int N = 5e3 + 5;
int p[N], c[N];
bool vis[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
ll ans = -LL_INF;
for (int i = 1; i <= n; i++) {
cin >> c[i];
ans = max(ans, (ll)c[i]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
vis[j] = false;
}
int cc = i;
vector<long long> vec;
vec.push_back(0);
int m = 0;
while (!vis[cc]) {
vis[cc] = true;
long long x = vec.back();
vec.push_back(x + c[p[cc]]);
cc = p[cc];
m++;
}
// out(vec);
ll cur = 0, mx = -LL_INF;
for (int j = 1; j <= k; j++) {
mx = max(mx, vec[j]);
}
ans = max(ans, mx);
ll d = k / m, r = k % m;
cur += d * vec[m];
if (d)
ans = max(ans, cur);
mx = -LL_INF;
for (int j = 1; j <= r; j++) {
mx = max(mx, vec[j]);
}
cur += mx;
ans = max(ans, cur);
if (d > 0) {
d--;
cur = d * vec[m];
mx = *max_element(vec.begin() + 1, vec.end());
cur += mx;
ans = max(ans, cur);
}
}
cout << ans << endl;
return 0;
} | /*input
5 2
2 4 5 1 3
3 4 -10 -8 8
*/
/*
______________
| )
| ) /
|______/
| | \
| | ) \
|_____|_______)
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long double ld;
#define pb push_back
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
int mod = 1000000007;
const int inf = 1034567891;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#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
#define out(container) \
for (auto it : container) \
cout << it << " "; \
cout << endl;
template <typename T> T GCD(T a, T b) {
ll t;
while (a) {
t = a;
a = b % a;
b = t;
}
return b;
}
template <typename T> string toString(T a) { return to_string(a); }
template <typename T> void toInt(string s, T &x) {
stringstream str(s);
str >> x;
}
inline int add(int x, int y) {
x += y;
if (x >= mod)
x -= mod;
return x;
}
inline int sub(int x, int y) {
x -= y;
if (x < 0)
x += mod;
return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
inline int powr(int a, ll b) {
int x = 1 % mod;
while (b) {
if (b & 1)
x = mul(x, a);
a = mul(a, a);
b >>= 1;
}
return x;
}
inline int inv(int a) { return powr(a, mod - 2); }
const int N = 5e3 + 5;
int p[N], c[N];
bool vis[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
ll ans = -LL_INF;
for (int i = 1; i <= n; i++) {
cin >> c[i];
ans = max(ans, (ll)c[i]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
vis[j] = false;
}
int cc = i;
vector<long long> vec;
vec.push_back(0);
int m = 0;
while (!vis[cc]) {
vis[cc] = true;
long long x = vec.back();
vec.push_back(x + c[p[cc]]);
cc = p[cc];
m++;
}
// out(vec);
ll cur = 0, mx = -LL_INF;
for (int j = 1; j <= min(k, m); j++) {
mx = max(mx, vec[j]);
}
ans = max(ans, mx);
ll d = k / m, r = k % m;
cur += d * vec[m];
if (d)
ans = max(ans, cur);
mx = -LL_INF;
for (int j = 1; j <= r; j++) {
mx = max(mx, vec[j]);
}
cur += mx;
ans = max(ans, cur);
if (d > 0) {
d--;
cur = d * vec[m];
mx = *max_element(vec.begin() + 1, vec.end());
cur += mx;
ans = max(ans, cur);
}
}
cout << ans << endl;
return 0;
} | replace | 167 | 168 | 167 | 168 | 0 | |
p02585 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(x) begin(x), end(x)
#define show(obj) \
{ \
for (auto x : obj) \
cout << x << ' '; \
cout << endl; \
}
#define line "----------"
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int inf = 1001001000;
const ll INF = 1LL << 60;
const int MOD = (int)1e9 + 7;
int main() {
ll N, K, t;
cin >> N >> K;
--K;
vector<ll> P(N);
vector<ll> C(N);
rep(i, N) {
cin >> t;
P[i] = --t;
}
rep(i, N) cin >> C[i];
ll ans = -INF;
rep(i, N) {
vector<ll> sum;
vector<bool> visited(N, false);
int nt = P[i];
ll sum_element = 0;
int cnt = 0;
while (!visited[nt]) {
visited[nt] = true;
sum_element += C[nt];
sum.push_back(sum_element);
nt = P[nt];
}
ll role = K / sum.size();
ll tail = K % sum.size();
ll e_ans = -INF;
for (int j = 0; j <= tail; ++j) {
e_ans = max(e_ans, sum[j]);
}
if (sum[sum.size() - 1] > 0) {
e_ans += sum[sum.size() - 1] * role;
}
ll e_ans2 = -INF;
if (role > 0) {
--role;
tail = K - 1;
}
for (int j = 0; j <= tail; ++j) {
e_ans2 = max(e_ans2, sum[j]);
}
if (sum[sum.size() - 1] > 0) {
e_ans2 += sum[sum.size() - 1] * role;
}
//{
// cout << line << endl;
// printf("[start = %d] -> [e_ans = %lld]\n", i, e_ans);
// printf("[role = %lld], [tail = %lld]\n", role, tail);
// show(sum);
// cout << line << endl;
//}
ans = max(ans, e_ans);
ans = max(ans, e_ans2);
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(x) begin(x), end(x)
#define show(obj) \
{ \
for (auto x : obj) \
cout << x << ' '; \
cout << endl; \
}
#define line "----------"
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int inf = 1001001000;
const ll INF = 1LL << 60;
const int MOD = (int)1e9 + 7;
int main() {
ll N, K, t;
cin >> N >> K;
--K;
vector<ll> P(N);
vector<ll> C(N);
rep(i, N) {
cin >> t;
P[i] = --t;
}
rep(i, N) cin >> C[i];
ll ans = -INF;
rep(i, N) {
vector<ll> sum;
vector<bool> visited(N, false);
int nt = P[i];
ll sum_element = 0;
int cnt = 0;
while (!visited[nt]) {
visited[nt] = true;
sum_element += C[nt];
sum.push_back(sum_element);
nt = P[nt];
}
ll role = K / sum.size();
ll tail = K % sum.size();
ll e_ans = -INF;
for (int j = 0; j <= tail; ++j) {
e_ans = max(e_ans, sum[j]);
}
if (sum[sum.size() - 1] > 0) {
e_ans += sum[sum.size() - 1] * role;
}
ll e_ans2 = -INF;
if (role > 0) {
--role;
tail = sum.size() - 1;
}
for (int j = 0; j <= tail; ++j) {
e_ans2 = max(e_ans2, sum[j]);
}
if (sum[sum.size() - 1] > 0) {
e_ans2 += sum[sum.size() - 1] * role;
}
//{
// cout << line << endl;
// printf("[start = %d] -> [e_ans = %lld]\n", i, e_ans);
// printf("[role = %lld], [tail = %lld]\n", role, tail);
// show(sum);
// cout << line << endl;
//}
ans = max(ans, e_ans);
ans = max(ans, e_ans2);
}
cout << ans << endl;
return 0;
}
| replace | 70 | 71 | 70 | 71 | 0 | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\n"
typedef long long ll;
using namespace std;
int static fast = []() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return 0;
}();
// freopen("input.txt", "r", stdin);
ll compute_neg(deque<ll> cost, ll k) {
ll n = cost.size();
ll res = INT_MIN;
ll acc = 0;
for (int i = 0; i < k; i++) {
acc += cost[i % n];
res = max(res, acc);
}
return res;
}
ll compute_pos(deque<ll> cost, ll k, ll sum) {
ll n = cost.size();
ll res = INT_MIN;
ll times = k / n;
ll acc = 0;
if (times > 3) {
ll zip = times - 3;
acc += zip * sum;
k -= zip * n;
}
res = acc;
for (int i = 0; i < k; i++) {
acc += cost[i % n];
res = max(res, acc);
}
return res;
}
int main() {
ll n, k;
cin >> n >> k;
vector<ll> perm(n, 0);
vector<ll> cost(n, 0);
for (auto &val : perm) {
cin >> val;
val -= 1;
}
for (auto &val : cost)
cin >> val;
vector<vector<int>> grp;
vector<int> visit(n, 0);
auto dfs = [&](auto &self, int src, int cur, vector<int> &st) -> void {
if (cur == src) {
grp.push_back(st);
return;
}
st.push_back(cur);
visit[cur] = 1;
self(self, src, perm[cur], st);
};
for (int i = 0; i < n; i++) {
if (visit[i] == 0) {
vector<int> tmp = {i};
visit[i] = 1;
dfs(dfs, i, perm[i], tmp);
}
}
ll ans = INT_MIN;
for (auto vec : grp) {
deque<ll> cost2;
for (int idx : vec) {
cost2.push_back(cost[idx]);
}
ll cycle = vec.size();
ll acc = accumulate(cost2.begin(), cost2.end(), 0LL);
for (int i = 0; i < cycle; i++) {
if (acc < 0)
ans = max(ans, compute_neg(cost2, k));
else
ans = max(ans, compute_pos(cost2, k, acc));
ll head = cost2.front();
cost2.push_back(head);
cost2.pop_front();
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\n"
typedef long long ll;
using namespace std;
int static fast = []() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return 0;
}();
// freopen("input.txt", "r", stdin);
ll compute_neg(deque<ll> cost, ll k) {
ll n = cost.size();
ll res = INT_MIN;
ll acc = 0;
for (int i = 0; i < min(n, k); i++) {
acc += cost[i % n];
res = max(res, acc);
}
return res;
}
ll compute_pos(deque<ll> cost, ll k, ll sum) {
ll n = cost.size();
ll res = INT_MIN;
ll times = k / n;
ll acc = 0;
if (times > 3) {
ll zip = times - 3;
acc += zip * sum;
k -= zip * n;
}
res = acc;
for (int i = 0; i < k; i++) {
acc += cost[i % n];
res = max(res, acc);
}
return res;
}
int main() {
ll n, k;
cin >> n >> k;
vector<ll> perm(n, 0);
vector<ll> cost(n, 0);
for (auto &val : perm) {
cin >> val;
val -= 1;
}
for (auto &val : cost)
cin >> val;
vector<vector<int>> grp;
vector<int> visit(n, 0);
auto dfs = [&](auto &self, int src, int cur, vector<int> &st) -> void {
if (cur == src) {
grp.push_back(st);
return;
}
st.push_back(cur);
visit[cur] = 1;
self(self, src, perm[cur], st);
};
for (int i = 0; i < n; i++) {
if (visit[i] == 0) {
vector<int> tmp = {i};
visit[i] = 1;
dfs(dfs, i, perm[i], tmp);
}
}
ll ans = INT_MIN;
for (auto vec : grp) {
deque<ll> cost2;
for (int idx : vec) {
cost2.push_back(cost[idx]);
}
ll cycle = vec.size();
ll acc = accumulate(cost2.begin(), cost2.end(), 0LL);
for (int i = 0; i < cycle; i++) {
if (acc < 0)
ans = max(ans, compute_neg(cost2, k));
else
ans = max(ans, compute_pos(cost2, k, acc));
ll head = cost2.front();
cost2.push_back(head);
cost2.pop_front();
}
}
cout << ans << endl;
return 0;
}
| replace | 16 | 17 | 16 | 17 | TLE | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
ll n, k, ans = -10000000000;
cin >> n >> k;
vector<ll> p(n), c(n);
rep(i, n) {
cin >> p[i];
--p[i];
}
rep(i, n) cin >> c[i];
if (k) {
vector<ll> dp(n);
for (ll i = 0; i < n; i++) {
ll now = i;
rep(j, k) {
now = p[now];
dp[i] += c[now];
ans = max(ans, dp[i]);
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
ll n, k, ans = -10000000000;
cin >> n >> k;
vector<ll> p(n), c(n);
rep(i, n) {
cin >> p[i];
--p[i];
}
rep(i, n) cin >> c[i];
vector<ll> dp(n);
for (ll i = 0; i < n; i++) {
ll now = i, start = i, count = 0;
rep(j, k) {
now = p[now];
count++;
dp[i] += c[now];
ans = max(ans, dp[i]);
if (now == start) {
j = k;
ll loop = (k - 1) / count;
dp[i] = dp[i] * loop;
ll kp = k - (count * loop);
rep(r, kp) {
now = p[now];
dp[i] += c[now];
ans = max(ans, dp[i]);
}
}
}
}
cout << ans << endl;
return 0;
}
| replace | 16 | 24 | 16 | 34 | TLE | |
p02585 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO {
FIO() {
std::cin.tie(0);
std::cout.tie(0);
std::ios_base::sync_with_stdio(0);
std::cout << std::fixed << std::setprecision(15);
}
} fIO;
/*-*/
struct UnionFind {
int n;
std::vector<int> par, size;
UnionFind(int sz) : n(sz), par(sz, 0), size(sz, 1) {
std::iota(par.begin(), par.end(), 0);
}
int find(int x) {
if (par[x] == x)
return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (size[x] < size[y])
std::swap(x, y);
size[x] += size[y];
par[y] = x;
}
};
int main() {
lnt m, k;
std::cin >> m >> k;
std::vector<lnt> p(m), c(m);
rep(i, m) std::cin >> p[i];
rep(i, m) std::cin >> c[i];
lnt max = -3e18;
std::map<lnt, lnt> map;
UnionFind uf(m);
rep(i, m) { uf.unite(i, p[i] - 1); }
rep(ii, m) {
if (map[uf.find(ii)])
continue;
lnt n = uf.size[uf.find(ii)];
std::vector<lnt> dp(n + 1, 0);
{
int i = ii;
rep(j, n) {
dp[j + 1] = dp[j] + c[i];
i = p[i] - 1;
}
}
rep(i, n) {
for (int j = i; j < n; j++) {
if (j - i + 1 <= k)
max = std::max(max, dp[j + 1] - dp[i]);
if (n - i + j + 1 <= k) {
max = std::max(max, dp[n] - dp[i] + dp[j + 1]);
max = std::max(max, dp[n] - dp[i] + dp[j + 1] +
dp[n] * ((k - (n - i + j + 1)) / n));
}
if (n - j + i + 1 <= k) {
max = std::max(max, dp[n] - dp[j] + dp[i + 1]);
max = std::max(max, dp[n] - dp[j] + dp[i + 1] +
dp[n] * ((k - (n - j + i + 1)) / n));
}
}
}
}
print(max);
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO {
FIO() {
std::cin.tie(0);
std::cout.tie(0);
std::ios_base::sync_with_stdio(0);
std::cout << std::fixed << std::setprecision(15);
}
} fIO;
/*-*/
struct UnionFind {
int n;
std::vector<int> par, size;
UnionFind(int sz) : n(sz), par(sz, 0), size(sz, 1) {
std::iota(par.begin(), par.end(), 0);
}
int find(int x) {
if (par[x] == x)
return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (size[x] < size[y])
std::swap(x, y);
size[x] += size[y];
par[y] = x;
}
};
int main() {
lnt m, k;
std::cin >> m >> k;
std::vector<lnt> p(m), c(m);
rep(i, m) std::cin >> p[i];
rep(i, m) std::cin >> c[i];
lnt max = -3e18;
std::map<lnt, lnt> map;
UnionFind uf(m);
rep(i, m) { uf.unite(i, p[i] - 1); }
rep(ii, m) {
if (map[uf.find(ii)])
continue;
map[uf.find(ii)] = 1;
lnt n = uf.size[uf.find(ii)];
std::vector<lnt> dp(n + 1, 0);
{
int i = ii;
rep(j, n) {
dp[j + 1] = dp[j] + c[i];
i = p[i] - 1;
}
}
rep(i, n) {
for (int j = i; j < n; j++) {
if (j - i + 1 <= k)
max = std::max(max, dp[j + 1] - dp[i]);
if (n - i + j + 1 <= k) {
max = std::max(max, dp[n] - dp[i] + dp[j + 1]);
max = std::max(max, dp[n] - dp[i] + dp[j + 1] +
dp[n] * ((k - (n - i + j + 1)) / n));
}
if (n - j + i + 1 <= k) {
max = std::max(max, dp[n] - dp[j] + dp[i + 1]);
max = std::max(max, dp[n] - dp[j] + dp[i + 1] +
dp[n] * ((k - (n - j + i + 1)) / n));
}
}
}
}
print(max);
}
| insert | 76 | 76 | 76 | 77 | TLE | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, n) for (int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
template <class T> using V = vector<T>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct UnionFind { // UnionFindTree
vector<int> d;
UnionFind(int n) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (-d[x] < -d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
int n, k;
cin >> n >> k;
V<int> p(n), c(n);
rep(i, n) cin >> p[i];
rep(i, n) cin >> c[i];
map<int, ll> mp;
UnionFind uf(n);
rep(i, n) { uf.unite(i, p[i] - 1); }
rep(i, n) { mp[uf.find(i)] += c[i]; }
ll ans = -infll;
rep(i, n) { // スタート位置
int roop = uf.size(i);
int times = (mp[uf.find(i)] > 0 ? (k - 1) / roop : 0);
int over = max(min(k, k - roop * times), 1);
ll tmp = 0;
ll s = -infll;
int idx = i;
rep(j, over) {
tmp += c[idx];
chmax(s, tmp);
idx = p[idx] - 1;
}
s += times * mp[uf.find(i)];
chmax(ans, s);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, n) for (int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
template <class T> using V = vector<T>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct UnionFind { // UnionFindTree
vector<int> d;
UnionFind(int n) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (-d[x] < -d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int main() {
int n, k;
cin >> n >> k;
V<int> p(n), c(n);
rep(i, n) cin >> p[i];
rep(i, n) cin >> c[i];
map<int, ll> mp;
UnionFind uf(n);
rep(i, n) { uf.unite(i, p[i] - 1); }
rep(i, n) { mp[uf.find(i)] += c[i]; }
ll ans = -infll;
rep(i, n) { // スタート位置
int roop = uf.size(i);
int times = (mp[uf.find(i)] > 0 ? (k - 1) / roop : 0);
int over = max(min(roop, k - roop * times), 1);
ll tmp = 0;
ll s = -infll;
int idx = i;
rep(j, over) {
tmp += c[idx];
chmax(s, tmp);
idx = p[idx] - 1;
}
s += times * mp[uf.find(i)];
chmax(ans, s);
}
cout << ans << endl;
} | replace | 73 | 74 | 73 | 74 | TLE | |
p02585 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b) { // a^b
ll res = 1;
a = a % MOD;
while (b > 0) {
if (b & 1) {
res = (res * a) % MOD;
b--;
}
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
bool isPalin(string s) {
if (s == string(s.rbegin(), s.rend()))
return true;
return false;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
ll t = 1;
cin >> t;
while (t--) {
ll n, k;
cin >> n >> k;
int p[n + 1];
int c[n + 1];
for (int i = 1; i <= n; i++)
cin >> p[i];
for (int i = 1; i <= n; i++)
cin >> c[i];
vector<bool> visited(n + 1, false);
ll ans = -1e18;
ans--;
for (int i = 1; i <= n; i++) {
if (visited[i] == false) {
int j = p[i];
vector<ll> v;
ll val = c[i];
while (j != i) {
val += c[j];
visited[j] = true;
v.push_back(c[j]);
j = p[j];
}
visited[i] = true;
v.push_back(c[i]);
int s = v.size();
for (j = 0; j < s; j++)
v.push_back(v[j]);
for (int i = 1; i < 2 * s; i++)
v[i] += v[i - 1];
ll l = 1;
ll r = k;
vector<ll> dp(s + 1, -1e18 + 1);
for (int j = 1; j <= s; j++) {
for (int i = 0; i + j < 2 * s; i++)
dp[j] = max(dp[j], v[i + j] - v[i]);
}
for (int j = 1; j <= s; j++) {
if (j <= k) {
ll v1 = (k - j) / s;
ans = max(ans, dp[j]);
ans = max(ans, dp[j] + v1 * val);
}
}
}
}
cout << ans << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b) { // a^b
ll res = 1;
a = a % MOD;
while (b > 0) {
if (b & 1) {
res = (res * a) % MOD;
b--;
}
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
bool isPalin(string s) {
if (s == string(s.rbegin(), s.rend()))
return true;
return false;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
ll t = 1;
// cin>>t;
while (t--) {
ll n, k;
cin >> n >> k;
int p[n + 1];
int c[n + 1];
for (int i = 1; i <= n; i++)
cin >> p[i];
for (int i = 1; i <= n; i++)
cin >> c[i];
vector<bool> visited(n + 1, false);
ll ans = -1e18;
ans--;
for (int i = 1; i <= n; i++) {
if (visited[i] == false) {
int j = p[i];
vector<ll> v;
ll val = c[i];
while (j != i) {
val += c[j];
visited[j] = true;
v.push_back(c[j]);
j = p[j];
}
visited[i] = true;
v.push_back(c[i]);
int s = v.size();
for (j = 0; j < s; j++)
v.push_back(v[j]);
for (int i = 1; i < 2 * s; i++)
v[i] += v[i - 1];
ll l = 1;
ll r = k;
vector<ll> dp(s + 1, -1e18 + 1);
for (int j = 1; j <= s; j++) {
for (int i = 0; i + j < 2 * s; i++)
dp[j] = max(dp[j], v[i + j] - v[i]);
}
for (int j = 1; j <= s; j++) {
if (j <= k) {
ll v1 = (k - j) / s;
ans = max(ans, dp[j]);
ans = max(ans, dp[j] + v1 * val);
}
}
}
}
cout << ans << "\n";
}
return 0;
}
| replace | 28 | 29 | 28 | 29 | -11 | |
p02585 | C++ | Time Limit Exceeded | //
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)
typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;
int a[N], c[N];
bool vst[N];
vector<int> vec[N];
int main() {
ios::sync_with_stdio(0);
int n, lim;
cin >> n >> lim;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> c[i];
for (int i = 1; i <= n; i++) {
if (vst[i])
continue;
int o = i;
while (!vst[o]) {
vst[o] = 1;
vec[i].push_back(o);
o = a[o];
}
}
ll ans = -INF_LL;
for (int i = 1; i <= n; i++) {
if (vec[i].empty())
continue;
int siz = vec[i].size();
if (siz != 0 && siz <= n)
while (1)
;
static ll s[N];
s[0] = 0;
for (int j = 1; j <= siz; j++)
s[j] = s[j + siz] = c[vec[i][j - 1]];
for (int j = 1; j <= 2 * siz; j++)
s[j] += s[j - 1];
ll sum = s[siz];
for (int j = 1; j <= min(siz, lim); j++) {
ll maxs = -INF_LL;
for (int k = 1; k <= siz; k++)
maxs = max(maxs, s[k + j - 1] - s[k - 1]);
if (sum > 0)
maxs += sum * ((lim - j) / siz);
ans = max(ans, maxs);
}
}
cout << ans << endl;
return 0;
}
| //
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)
typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;
int a[N], c[N];
bool vst[N];
vector<int> vec[N];
int main() {
ios::sync_with_stdio(0);
int n, lim;
cin >> n >> lim;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> c[i];
for (int i = 1; i <= n; i++) {
if (vst[i])
continue;
int o = i;
while (!vst[o]) {
vst[o] = 1;
vec[i].push_back(o);
o = a[o];
}
}
ll ans = -INF_LL;
for (int i = 1; i <= n; i++) {
if (vec[i].empty())
continue;
int siz = vec[i].size();
static ll s[N];
s[0] = 0;
for (int j = 1; j <= siz; j++)
s[j] = s[j + siz] = c[vec[i][j - 1]];
for (int j = 1; j <= 2 * siz; j++)
s[j] += s[j - 1];
ll sum = s[siz];
for (int j = 1; j <= min(siz, lim); j++) {
ll maxs = -INF_LL;
for (int k = 1; k <= siz; k++)
maxs = max(maxs, s[k + j - 1] - s[k - 1]);
if (sum > 0)
maxs += sum * ((lim - j) / siz);
ans = max(ans, maxs);
}
}
cout << ans << endl;
return 0;
}
| delete | 49 | 52 | 49 | 49 | TLE | |
p02585 | C++ | Runtime Error | #pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 205;
const int MAXM = 50005;
const int MOD = 1e9 + 7;
#define MP make_pair
#define INF 0x3f3f3f3f
#define closeSync \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define mst(X, Y) memset(X, Y, sizeof(X))
#define rep(i, a, n) for (int i = (a); i <= (n); i++)
#define per(i, a, n) for (int i = (a); i >= (n); i--)
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
template <typename T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
int vis[MAXN];
int p[MAXN];
ll c[MAXN];
ll sumv[MAXN], preMax[MAXN], val[MAXN];
int n;
ll k;
inline ll go(int st) {
int cnt = 0;
vis[st] = 1;
sumv[0] = 0;
preMax[0] = -1e18;
while (!vis[p[st]]) {
st = p[st];
++cnt;
sumv[cnt] = sumv[cnt - 1] + c[st];
val[cnt] = c[st];
// debug(sumv[cnt]);
preMax[cnt] = max(preMax[cnt - 1], sumv[cnt]);
vis[st] = 1;
}
st = p[st];
++cnt;
sumv[cnt] = sumv[cnt - 1] + c[st];
// debug(sumv[cnt]);
val[cnt] = st;
preMax[cnt] = max(preMax[cnt - 1], sumv[cnt]);
int rd = cnt; // 循环节的长度
// debug(rd);
ll rdval = sumv[cnt]; // 循环节的值
// debug(rdval);
// debug(preMax[cnt]);
preMax[0] = 0;
ll ans = 0;
// debug(cnt);
rep(i, 1, rd) {
sumv[rd + i] = sumv[rd + i - 1] + val[i];
// debug(val[i]);
// debug(sumv[rd+i]);
preMax[rd + i] = max(preMax[rd + i - 1], sumv[rd + i]);
// debug(preMax[rd+i]);
}
if (k > rd) {
if (rdval > 0) {
ll rm = k % rd + rd;
// debug(ans);
ans += rdval * ((k - rd) / rd);
// debug(ans);
ans += preMax[rm];
} else
ans = preMax[cnt];
} else
ans = preMax[k];
return ans;
}
int main() {
closeSync;
#ifdef DEBUG
// freopen("C:/Users/86159/Desktop/a2.in.txt", "r", stdin);
// freopen("C:/Users/86159/Desktop/output.txt", "w", stdout);
#endif
cin >> n >> k;
rep(i, 1, n) cin >> p[i];
rep(i, 1, n) cin >> c[i];
ll mx = -1e18;
rep(i, 1, n) {
rep(i, 1, n) val[i] = sumv[i] = preMax[i] = vis[i] = 0;
ll temp = go(i);
// debug(temp);
mx = max(mx, temp);
}
cout << mx << endl;
return 0;
} | #pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 100005;
const int MAXM = 50005;
const int MOD = 1e9 + 7;
#define MP make_pair
#define INF 0x3f3f3f3f
#define closeSync \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define mst(X, Y) memset(X, Y, sizeof(X))
#define rep(i, a, n) for (int i = (a); i <= (n); i++)
#define per(i, a, n) for (int i = (a); i >= (n); i--)
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
template <typename T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
int vis[MAXN];
int p[MAXN];
ll c[MAXN];
ll sumv[MAXN], preMax[MAXN], val[MAXN];
int n;
ll k;
inline ll go(int st) {
int cnt = 0;
vis[st] = 1;
sumv[0] = 0;
preMax[0] = -1e18;
while (!vis[p[st]]) {
st = p[st];
++cnt;
sumv[cnt] = sumv[cnt - 1] + c[st];
val[cnt] = c[st];
// debug(sumv[cnt]);
preMax[cnt] = max(preMax[cnt - 1], sumv[cnt]);
vis[st] = 1;
}
st = p[st];
++cnt;
sumv[cnt] = sumv[cnt - 1] + c[st];
// debug(sumv[cnt]);
val[cnt] = st;
preMax[cnt] = max(preMax[cnt - 1], sumv[cnt]);
int rd = cnt; // 循环节的长度
// debug(rd);
ll rdval = sumv[cnt]; // 循环节的值
// debug(rdval);
// debug(preMax[cnt]);
preMax[0] = 0;
ll ans = 0;
// debug(cnt);
rep(i, 1, rd) {
sumv[rd + i] = sumv[rd + i - 1] + val[i];
// debug(val[i]);
// debug(sumv[rd+i]);
preMax[rd + i] = max(preMax[rd + i - 1], sumv[rd + i]);
// debug(preMax[rd+i]);
}
if (k > rd) {
if (rdval > 0) {
ll rm = k % rd + rd;
// debug(ans);
ans += rdval * ((k - rd) / rd);
// debug(ans);
ans += preMax[rm];
} else
ans = preMax[cnt];
} else
ans = preMax[k];
return ans;
}
int main() {
closeSync;
#ifdef DEBUG
// freopen("C:/Users/86159/Desktop/a2.in.txt", "r", stdin);
// freopen("C:/Users/86159/Desktop/output.txt", "w", stdout);
#endif
cin >> n >> k;
rep(i, 1, n) cin >> p[i];
rep(i, 1, n) cin >> c[i];
ll mx = -1e18;
rep(i, 1, n) {
rep(i, 1, n) val[i] = sumv[i] = preMax[i] = vis[i] = 0;
ll temp = go(i);
// debug(temp);
mx = max(mx, temp);
}
cout << mx << endl;
return 0;
} | replace | 27 | 28 | 27 | 28 | 0 | |
p02585 | C++ | Time Limit Exceeded | #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int arr[5001];
int points[5001];
pair<int, ll> memo[5001]; // {횟수,총점수}
int main() {
int N, K;
cin >> N >> K;
for (int i = 1; i <= N; i++) {
cin >> arr[i];
}
for (int i = 1; i <= N; i++) {
cin >> points[i];
}
for (int i = 1; i <= N; i++) {
vector<int> nodes;
int initIdx = i;
if (memo[i].first != 0)
continue;
int currIdx = i;
ll point_sum = 0;
int cnt = 0;
do {
cnt++;
currIdx = arr[currIdx];
point_sum += points[currIdx];
nodes.push_back(currIdx);
} while (currIdx != initIdx);
for (int i = 0; i < nodes.size(); i++) {
memo[nodes[i]] = {cnt, point_sum};
}
}
ll maxim = -10e20;
for (int i = 1; i <= N; i++) {
int tempK = K;
ll sum = 0;
int currIdx = i;
for (int j = 0; j < tempK; j++) {
currIdx = arr[currIdx];
sum += points[currIdx];
maxim = max(maxim, sum);
}
}
cout << maxim << endl;
}
| #include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int arr[5001];
int points[5001];
pair<int, ll> memo[5001]; // {횟수,총점수}
int main() {
int N, K;
cin >> N >> K;
for (int i = 1; i <= N; i++) {
cin >> arr[i];
}
for (int i = 1; i <= N; i++) {
cin >> points[i];
}
for (int i = 1; i <= N; i++) {
vector<int> nodes;
int initIdx = i;
if (memo[i].first != 0)
continue;
int currIdx = i;
ll point_sum = 0;
int cnt = 0;
do {
cnt++;
currIdx = arr[currIdx];
point_sum += points[currIdx];
nodes.push_back(currIdx);
} while (currIdx != initIdx);
for (int i = 0; i < nodes.size(); i++) {
memo[nodes[i]] = {cnt, point_sum};
}
}
ll maxim = -10e20;
for (int i = 1; i <= N; i++) {
int tempK = K;
ll sum = 0;
double multiple = (double)tempK / memo[i].first;
if (multiple > 2) {
if (memo[i].second > 0) {
sum += ((int)multiple - 1) * memo[i].second;
}
tempK -= ((int)multiple - 1) * memo[i].first;
}
int currIdx = i;
for (int j = 0; j < tempK; j++) {
currIdx = arr[currIdx];
sum += points[currIdx];
maxim = max(maxim, sum);
}
}
cout << maxim << endl;
}
| insert | 54 | 54 | 54 | 61 | TLE | |
p02585 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;
#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif
struct init {
init() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} init_;
template <class T> inline bool chmax(T &a, const T b) {
return a < b && (a = b, true);
}
int main() {
int N, K;
cin >> N >> K;
vector<int> P(N), C(N);
for (int i = 0; i < N; i++)
cin >> P[i];
for (int i = 0; i < N; i++)
P[i]--;
for (int i = 0; i < N; i++)
cin >> C[i];
lint ans = -LINF;
for (int st = 0; st < N; st++) {
vector<lint> used(N), point(N + 1);
int loop = 0, now = st;
for (int i = 0; i < min(N, K); i++, loop++) {
if (used[now])
break;
point[i + 1] = point[i] + C[now];
used[now] = 1;
now = P[now];
}
DMP(point, loop);
if (point[loop] > 0 && loop < K) {
int r = K / loop;
point[0] = -LINF;
if (chmax(ans,
r * point[loop] +
*max_element(point.begin(), point.begin() + K % loop + 1)))
DMP(1, st, ans, point);
if (chmax(ans, (r - 1) * point[loop] +
*max_element(point.begin(), point.begin() + loop + 1)))
DMP(2, st, ans, point);
} else {
if (chmax(ans, *max_element(point.begin() + 1, point.begin() + K + 1)))
DMP(3, st, ans, point);
}
}
cout << ans << "\n";
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;
#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif
struct init {
init() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} init_;
template <class T> inline bool chmax(T &a, const T b) {
return a < b && (a = b, true);
}
int main() {
int N, K;
cin >> N >> K;
vector<int> P(N), C(N);
for (int i = 0; i < N; i++)
cin >> P[i];
for (int i = 0; i < N; i++)
P[i]--;
for (int i = 0; i < N; i++)
cin >> C[i];
lint ans = -LINF;
for (int st = 0; st < N; st++) {
vector<lint> used(N), point(N + 1);
int loop = 0, now = st;
for (int i = 0; i < min(N, K); i++, loop++) {
if (used[now])
break;
point[i + 1] = point[i] + C[now];
used[now] = 1;
now = P[now];
}
DMP(point, loop);
if (point[loop] > 0 && loop < K) {
int r = K / loop;
point[0] = -LINF;
if (chmax(ans,
r * point[loop] +
*max_element(point.begin(), point.begin() + K % loop + 1)))
DMP(1, st, ans, point);
if (chmax(ans, (r - 1) * point[loop] +
*max_element(point.begin(), point.begin() + loop + 1)))
DMP(2, st, ans, point);
} else {
if (chmax(ans, *max_element(point.begin() + 1,
point.begin() + min(loop, K) + 1)))
DMP(3, st, ans, point);
}
}
cout << ans << "\n";
return 0;
}
| replace | 74 | 75 | 74 | 76 | 0 | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int64_t K;
cin >> N >> K;
vector<int> P(N);
for (int i = 0; i < N; i++)
cin >> P[i], P[i]--;
vector<int64_t> C(N);
for (int i = 0; i < N; i++)
cin >> C[i];
vector<int> visited(N);
vector<vector<int>> VV;
for (int i = 0; i < N; i++)
if (!visited[i]) {
vector<int> v = {i};
int pt = P[i];
while (pt != i) {
v.push_back(pt);
pt = P[pt];
}
VV.push_back(v);
}
int64_t ans = -1e18;
for (auto &v : VV) {
int sz = v.size();
for (int r = 0; r < sz; r++) {
vector<int64_t> S(sz + 1);
for (int i = 0; i < sz; i++)
S[i + 1] = S[i] + C[v[i]];
for (int t = 0; t < sz; t++) {
int64_t lmn = (t ? 0 : 1);
int64_t lmx = (K + sz - t) / sz - 1;
if (lmn > lmx)
continue;
int64_t l = (S[sz] > 0 ? lmx : lmn);
int64_t res = l * S[sz] + S[t];
ans = max(ans, res);
}
rotate(v.begin(), v.begin() + 1, v.end());
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int64_t K;
cin >> N >> K;
vector<int> P(N);
for (int i = 0; i < N; i++)
cin >> P[i], P[i]--;
vector<int64_t> C(N);
for (int i = 0; i < N; i++)
cin >> C[i];
vector<int> visited(N);
vector<vector<int>> VV;
for (int i = 0; i < N; i++)
if (!visited[i]) {
vector<int> v = {i};
int pt = P[i];
while (pt != i) {
v.push_back(pt);
pt = P[pt];
}
for (int a : v)
visited[a] = 1;
VV.push_back(v);
}
int64_t ans = -1e18;
for (auto &v : VV) {
int sz = v.size();
for (int r = 0; r < sz; r++) {
vector<int64_t> S(sz + 1);
for (int i = 0; i < sz; i++)
S[i + 1] = S[i] + C[v[i]];
for (int t = 0; t < sz; t++) {
int64_t lmn = (t ? 0 : 1);
int64_t lmx = (K + sz - t) / sz - 1;
if (lmn > lmx)
continue;
int64_t l = (S[sz] > 0 ? lmx : lmn);
int64_t res = l * S[sz] + S[t];
ans = max(ans, res);
}
rotate(v.begin(), v.begin() + 1, v.end());
}
}
cout << ans << endl;
return 0;
}
| insert | 24 | 24 | 24 | 26 | TLE | |
p02585 | C++ | Runtime Error | #include <climits>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
int main() {
int n, k;
cin >> n >> k;
vector<int> p(n);
vector<ll> c(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
}
for (int i = 0; i < n; i++)
cin >> c[i];
vector<vector<int>> v(n);
for (int i = 0; i < n; i++) {
int j = i;
while (p[j] != i) {
v[i].push_back(p[j]);
j = p[j];
}
v[i].push_back(i);
}
/*
for(int i = 0; i < n; i++) {
for(auto x : v[i]) {
cout << x << " ";
}
cout << endl;
}
*/
ll ans = LONG_LONG_MIN;
for (int i = 0; i < n; i++) {
int s = v[i].size();
ll tmp = 0;
ll wa = 0;
int q = k / s;
int r = k % s;
vector<ll> mx(s, LONG_LONG_MIN);
mx[0] = c[v[i][0]];
wa = c[v[i][0]];
for (int j = 1; j < s; j++) {
wa += c[v[i][j]];
mx[j] = max(mx[j - 1], wa);
}
// for(auto x : v[i]) wa += c[x];
if (wa >= 0 && q >= 1)
tmp = wa * (ll)(q - 1);
tmp += max(mx[s - 1], wa + mx[q]);
ans = max(ans, tmp);
}
cout << ans << endl;
return 0;
} | #include <climits>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
int main() {
int n, k;
cin >> n >> k;
vector<int> p(n);
vector<ll> c(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
}
for (int i = 0; i < n; i++)
cin >> c[i];
vector<vector<int>> v(n);
for (int i = 0; i < n; i++) {
int j = i;
while (p[j] != i) {
v[i].push_back(p[j]);
j = p[j];
}
v[i].push_back(i);
}
/*
for(int i = 0; i < n; i++) {
for(auto x : v[i]) {
cout << x << " ";
}
cout << endl;
}
*/
ll ans = LONG_LONG_MIN;
for (int i = 0; i < n; i++) {
int s = v[i].size();
ll tmp = 0;
ll wa = 0;
int q = k / s;
int r = k % s;
vector<ll> mx(s, LONG_LONG_MIN);
mx[0] = c[v[i][0]];
wa = c[v[i][0]];
for (int j = 1; j < s; j++) {
wa += c[v[i][j]];
mx[j] = max(mx[j - 1], wa);
}
// for(auto x : v[i]) wa += c[x];
if (wa >= 0 && q >= 1)
tmp = wa * (ll)(q - 1);
if (q == 0)
tmp += mx[r - 1];
if (q != 0 && r == 0)
tmp += mx[s - 1];
if (q != 0 && r >= 1)
tmp += max(mx[s - 1], wa + mx[r - 1]);
ans = max(ans, tmp);
}
cout << ans << endl;
return 0;
} | replace | 62 | 63 | 62 | 69 | 0 | |
p02585 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
int p[5001];
int c[5001];
long long ans[5001] = {0};
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> p[i];
for (int i = 0; i < n; i++)
cin >> c[i];
for (int i = 0; i < n; i++) {
// find out length, cycle_length, cycle_sum, whole_sum, max_val, s_pos
int length = 0, cycle_length = 0, s_pos = 0;
long long cycle_sum = 0, whole_sum = 0, max_val = -1e18;
int s = i;
map<int, vector<long long>> dict;
while (dict[s].empty()) {
// cout<< " s is : " << s <<'\n';
dict[s] = {length, whole_sum};
// cout << "dict[s] is : " << length << " " << whole_sum << '\n';
length++;
whole_sum += c[p[s] - 1];
s = p[s] - 1;
if (length <= k)
max_val = max(whole_sum, max_val);
}
cycle_length = length - dict[s][0];
cycle_sum = whole_sum - dict[s][1];
s_pos = s;
// cout << "cycle_length is : " << cycle_length << " cycle_sum is : " <<
// cycle_sum << " s_pos is : " << s_pos <<'\n' << '\n';
if (k <= length) {
ans[i] = max_val;
} else { // k > length
if (cycle_sum <= 0)
ans[i] = max_val;
else {
int pos_cycle = (k - length) / cycle_length;
if (pos_cycle >= 1) {
int left_k = (k - length) - (pos_cycle - 1) * cycle_length;
whole_sum += (pos_cycle - 1) * cycle_sum;
max_val = max(whole_sum, max_val);
for (int j = 0; j < left_k; j++) {
whole_sum += c[p[s_pos] - 1];
s_pos = p[s_pos] - 1;
max_val = max(whole_sum, max_val);
}
ans[i] = max_val;
} else {
int left_k = k - length;
for (int j = 0; j < left_k; j++) {
whole_sum += c[p[s_pos] - 1];
s_pos = p[s_pos] - 1;
max_val = max(whole_sum, max_val);
}
ans[i] = max_val;
}
}
}
}
// for(int i=0; i<n; i++)
// cout << ans[i] <<' ';
// cout << '\n';
cout << *max_element(ans, ans + n);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
int p[5001];
int c[5001];
long long ans[5001] = {0};
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> p[i];
for (int i = 0; i < n; i++)
cin >> c[i];
for (int i = 0; i < n; i++) {
// find out length, cycle_length, cycle_sum, whole_sum, max_val, s_pos
int length = 0, cycle_length = 0, s_pos = 0;
long long cycle_sum = 0, whole_sum = 0, max_val = -1e18;
int s = i;
unordered_map<int, vector<long long>> dict;
while (dict[s].empty()) {
// cout<< " s is : " << s <<'\n';
dict[s] = {length, whole_sum};
// cout << "dict[s] is : " << length << " " << whole_sum << '\n';
length++;
whole_sum += c[p[s] - 1];
s = p[s] - 1;
if (length <= k)
max_val = max(whole_sum, max_val);
}
cycle_length = length - dict[s][0];
cycle_sum = whole_sum - dict[s][1];
s_pos = s;
// cout << "cycle_length is : " << cycle_length << " cycle_sum is : " <<
// cycle_sum << " s_pos is : " << s_pos <<'\n' << '\n';
if (k <= length) {
ans[i] = max_val;
} else { // k > length
if (cycle_sum <= 0)
ans[i] = max_val;
else {
int pos_cycle = (k - length) / cycle_length;
if (pos_cycle >= 1) {
int left_k = (k - length) - (pos_cycle - 1) * cycle_length;
whole_sum += (pos_cycle - 1) * cycle_sum;
max_val = max(whole_sum, max_val);
for (int j = 0; j < left_k; j++) {
whole_sum += c[p[s_pos] - 1];
s_pos = p[s_pos] - 1;
max_val = max(whole_sum, max_val);
}
ans[i] = max_val;
} else {
int left_k = k - length;
for (int j = 0; j < left_k; j++) {
whole_sum += c[p[s_pos] - 1];
s_pos = p[s_pos] - 1;
max_val = max(whole_sum, max_val);
}
ans[i] = max_val;
}
}
}
}
// for(int i=0; i<n; i++)
// cout << ans[i] <<' ';
// cout << '\n';
cout << *max_element(ans, ans + n);
return 0;
} | replace | 24 | 25 | 24 | 25 | TLE | |
p02586 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;
#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) \
ASC((x)); \
REV((x))
#define pb push_back
#define eb emplace_back
int main() {
ll R, C, K;
cin >> R >> C >> K;
map<pair<ll, ll>, ll> S;
REP(i, 0, K) {
ll r, c, v;
cin >> r >> c >> v;
S[{r, c}] = v;
}
vector<vector<vector<ll>>> dp(R + 1,
vector<vector<ll>>(C + 1, vector<ll>(4)));
REP(i, 0, R + 1) REP(j, 0, C + 1) REP(k, 0, 4) dp[i][j][k] = 0;
REP(i, 1, R + 1) {
REP(j, 1, C + 1) {
ll x = S[{i, j}];
dp[i][j][0] = MAX(dp[i][j][0], dp[i][j - 1][0]);
REP(k, 0, 4) dp[i][j][0] = MAX(dp[i][j][0], dp[i - 1][j][k]);
REP(k, 1, 4) dp[i][j][k] = MAX(dp[i][j][0], dp[i][j - 1][k]);
dp[i][j][1] = MAX(dp[i][j][1], dp[i][j - 1][0] + x);
REP(k, 0, 4) dp[i][j][1] = MAX(dp[i][j][1], dp[i - 1][j][k] + x);
REP(k, 2, 4) dp[i][j][k] = MAX(dp[i][j][k], dp[i][j - 1][k - 1] + x);
}
}
ll ans = MAX(dp[R][C][0], dp[R][C][1]);
ans = MAX(ans, dp[R][C][2]);
ans = MAX(ans, dp[R][C][3]);
PR(ans);
return 0;
}
/*
*/ | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;
#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) \
ASC((x)); \
REV((x))
#define pb push_back
#define eb emplace_back
int main() {
ll R, C, K;
cin >> R >> C >> K;
map<pair<ll, ll>, ll> S;
REP(i, 0, K) {
ll r, c, v;
cin >> r >> c >> v;
S[{r, c}] = v;
}
vector<vector<vector<ll>>> dp(R + 1,
vector<vector<ll>>(C + 1, vector<ll>(4)));
REP(i, 0, R + 1) REP(j, 0, C + 1) REP(k, 0, 4) dp[i][j][k] = 0;
REP(i, 1, R + 1) {
REP(j, 1, C + 1) {
ll x = 0;
if (S.count({i, j}) > 0)
x = S[{i, j}];
dp[i][j][0] = MAX(dp[i][j][0], dp[i][j - 1][0]);
REP(k, 0, 4) dp[i][j][0] = MAX(dp[i][j][0], dp[i - 1][j][k]);
REP(k, 1, 4) dp[i][j][k] = MAX(dp[i][j][0], dp[i][j - 1][k]);
dp[i][j][1] = MAX(dp[i][j][1], dp[i][j - 1][0] + x);
REP(k, 0, 4) dp[i][j][1] = MAX(dp[i][j][1], dp[i - 1][j][k] + x);
REP(k, 2, 4) dp[i][j][k] = MAX(dp[i][j][k], dp[i][j - 1][k - 1] + x);
}
}
ll ans = MAX(dp[R][C][0], dp[R][C][1]);
ans = MAX(ans, dp[R][C][2]);
ans = MAX(ans, dp[R][C][3]);
PR(ans);
return 0;
}
/*
*/ | replace | 43 | 44 | 43 | 46 | TLE | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
ll v[3000][3000], dp[3000][3000][4];
int main(void) {
int r, c, t;
cin >> r >> c >> t;
fill(v[0], v[r + 1], 0);
rep(i, t) {
int a, b;
cin >> a >> b;
cin >> v[a - 1][b - 1];
}
fill(dp[0][0], dp[r][0], 0);
if (v[0][0] > 0)
dp[0][0][2] += v[0][0];
rep(i, r) {
rep(j, c) {
ll val = v[i][j];
rep(k, 4) {
if (i > 0) {
dp[i][j][3] = max(dp[i][j][3], dp[i - 1][j][k]);
dp[i][j][2] = max(dp[i][j][2], dp[i - 1][j][k] + val);
}
if (j > 0) {
dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
if (k > 0) {
dp[i][j][k - 1] = max(dp[i][j][k - 1], dp[i][j - 1][k] + val);
}
}
}
}
}
ll ans = 0;
rep(k, 4) ans = max(ans, dp[r - 1][c - 1][k]);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
ll v[3000][3000], dp[3000][3000][4];
int main(void) {
int r, c, t;
cin >> r >> c >> t;
fill(v[0], v[r], 0);
rep(i, t) {
int a, b;
cin >> a >> b;
cin >> v[a - 1][b - 1];
}
fill(dp[0][0], dp[r][0], 0);
if (v[0][0] > 0)
dp[0][0][2] += v[0][0];
rep(i, r) {
rep(j, c) {
ll val = v[i][j];
rep(k, 4) {
if (i > 0) {
dp[i][j][3] = max(dp[i][j][3], dp[i - 1][j][k]);
dp[i][j][2] = max(dp[i][j][2], dp[i - 1][j][k] + val);
}
if (j > 0) {
dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
if (k > 0) {
dp[i][j][k - 1] = max(dp[i][j][k - 1], dp[i][j - 1][k] + val);
}
}
}
}
}
ll ans = 0;
rep(k, 4) ans = max(ans, dp[r - 1][c - 1][k]);
cout << ans << endl;
return 0;
} | replace | 11 | 12 | 11 | 12 | -11 | |
p02586 | C++ | Runtime Error | // #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/
using namespace std;
#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007
#define MAX 3000
signed main() {
int rcv[MAX + 1][MAX + 1] = {{0}};
int ans[4][MAX + 1][MAX + 1];
int R, C, K;
cin >> R >> C >> K;
for (int i = 0; i < K; i++) {
int r, c, v;
cin >> r >> c >> v;
rcv[r][c] = v;
}
queue<int> qx, qy;
qx.push(1);
qy.push(1);
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
for (int k = 0; k < 4; k++) {
ans[k][i][j] = -1;
}
}
}
ans[0][1][1] = 0;
while (!qx.empty()) {
int nx = qx.front();
qx.pop();
int ny = qy.front();
qy.pop();
for (int i = 0; i < 4; i++) {
if (ans[i][ny][nx] == -1)
continue;
if (i <= 2) {
if (nx != C &&
(ans[i + 1][ny][nx + 1] < ans[i][ny][nx] + rcv[ny][nx])) {
ans[i + 1][ny][nx + 1] = ans[i][ny][nx] + rcv[ny][nx];
qx.push(nx + 1);
qy.push(ny);
}
if (ny != R && (ans[0][ny + 1][nx] < ans[i][ny][nx] + rcv[ny][nx])) {
ans[0][ny + 1][nx] = ans[i][ny][nx] + rcv[ny][nx];
qx.push(nx);
qy.push(ny + 1);
}
}
if (nx != C && (ans[i][ny][nx + 1] < ans[i][ny][nx])) {
ans[i][ny][nx + 1] = ans[i][ny][nx];
qx.push(nx + 1);
qy.push(ny);
}
if (ny != R && (ans[0][ny + 1][nx] < ans[i][ny][nx])) {
ans[0][ny + 1][nx] = ans[i][ny][nx];
qx.push(nx);
qy.push(ny + 1);
}
}
}
cout << max(ans[0][R][C] + rcv[R][C],
max(ans[1][R][C] + rcv[R][C],
max(ans[2][R][C] + rcv[R][C], ans[3][R][C])))
<< endl;
}
| // #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/
using namespace std;
#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007
#define MAX 3000
signed main() {
static int rcv[MAX + 1][MAX + 1] = {{0}};
static int ans[4][MAX + 1][MAX + 1];
int R, C, K;
cin >> R >> C >> K;
for (int i = 0; i < K; i++) {
int r, c, v;
cin >> r >> c >> v;
rcv[r][c] = v;
}
queue<int> qx, qy;
qx.push(1);
qy.push(1);
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
for (int k = 0; k < 4; k++) {
ans[k][i][j] = -1;
}
}
}
ans[0][1][1] = 0;
while (!qx.empty()) {
int nx = qx.front();
qx.pop();
int ny = qy.front();
qy.pop();
for (int i = 0; i < 4; i++) {
if (ans[i][ny][nx] == -1)
continue;
if (i <= 2) {
if (nx != C &&
(ans[i + 1][ny][nx + 1] < ans[i][ny][nx] + rcv[ny][nx])) {
ans[i + 1][ny][nx + 1] = ans[i][ny][nx] + rcv[ny][nx];
qx.push(nx + 1);
qy.push(ny);
}
if (ny != R && (ans[0][ny + 1][nx] < ans[i][ny][nx] + rcv[ny][nx])) {
ans[0][ny + 1][nx] = ans[i][ny][nx] + rcv[ny][nx];
qx.push(nx);
qy.push(ny + 1);
}
}
if (nx != C && (ans[i][ny][nx + 1] < ans[i][ny][nx])) {
ans[i][ny][nx + 1] = ans[i][ny][nx];
qx.push(nx + 1);
qy.push(ny);
}
if (ny != R && (ans[0][ny + 1][nx] < ans[i][ny][nx])) {
ans[0][ny + 1][nx] = ans[i][ny][nx];
qx.push(nx);
qy.push(ny + 1);
}
}
}
cout << max(ans[0][R][C] + rcv[R][C],
max(ans[1][R][C] + rcv[R][C],
max(ans[2][R][C] + rcv[R][C], ans[3][R][C])))
<< endl;
}
| replace | 28 | 30 | 28 | 30 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define show(x) cout << #x << " = " << (x) << endl
#define show2(x, y) \
cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define show3(x, y, z) \
cout << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z \
<< " = " << (z) << endl
#define showv(v) \
rep(i, v.size()) printf("%d%c", v[i], i == v.size() - 1 ? '\n' : ' ')
#define showv2(v) rep(j, v.size()) showv(v[j])
#define showt(t, n) rep(i, n) printf("%d%c", t[i], i == n - 1 ? '\n' : ' ')
#define showt2(t, r, c) rep(j, r) showt(t[j], c)
#define showvp(p) rep(i, p.size()) printf("%d %d\n", p[i].first, p[i].second)
#define printv(v) rep(i, v.size()) printf("%d\n", v[i])
#define printt(t, n) rep(i, n) printf("%d\n", t[i])
#define incl(v, x) (find(all(v), x) != v.end())
#define incls(s, c) (s.find(c) != string::npos)
#define lb(a, x) distance((a).begin(), lower_bound(all(a), (x)))
#define ub(a, x) distance((a).begin(), upper_bound(all(a), (x)))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define bit(n, k) ((n >> k) & 1) // nのk bit目
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define uni(x) x.erase(unique(all(x)), x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using vp = vector<P>;
using vt = vector<T>;
const int mod = 1000000007;
const double EPS = 1e-9;
// const long double EPS = 1e-14;
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 62) - 1;
#define dame \
{ \
puts("No"); \
return 0; \
}
#define yn \
{ puts("Yes"); } \
else { \
puts("No"); \
}
#define YN \
{ puts("YES"); } \
else { \
puts("NO"); \
}
inline int in() {
int x;
cin >> x;
return x;
}
inline ll lin() {
ll x;
cin >> x;
return x;
}
inline char chin() {
char x;
cin >> x;
return x;
}
inline string stin() {
string x;
cin >> x;
return x;
}
inline double din() {
double x;
cin >> x;
return x;
}
// template<class T = int> inline T in() { T x; cin >> x; return (x);}
template <typename T> inline ll suma(const vector<T> &a) {
ll res(0);
for (auto &&x : a)
res += x;
return res;
}
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;
}
char itoa(int n) { return n + '0'; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const int dy[2] = {1, 0};
const int dx[2] = {0, 1};
ll dp[1005][1005][4];
int main() {
int R, C, K;
cin >> R >> C >> K;
vi r(K), c(K), v(K);
map<P, int> id;
rep(i, K) {
cin >> r[i] >> c[i] >> v[i];
r[i]--, c[i]--;
id[P(r[i], c[i])] = i;
}
rep(k, 4) dp[0][0][k] = 0;
if (id.count(P(0, 0))) {
int vi = id[P(0, 0)];
dp[0][0][0] = 0;
dp[0][0][1] = v[vi];
}
rep(i, R) rep(j, C) rep(k, 4) {
rep(di, 2) {
int ni = i + dy[di], nj = j + dx[di], nk;
if (!isin(ni, 0, R) || !isin(nj, 0, C))
continue;
if (di == 0)
nk = 0;
else
nk = k;
chmax(dp[ni][nj][nk], dp[i][j][k]);
if (id.count(P(ni, nj))) {
int vi = id[P(ni, nj)];
nk++;
if (nk == 4)
continue;
chmax(dp[ni][nj][nk], dp[i][j][k] + v[vi]);
}
}
}
ll ans = 0;
rep(k, 4) chmax(ans, dp[R - 1][C - 1][k]);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define show(x) cout << #x << " = " << (x) << endl
#define show2(x, y) \
cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define show3(x, y, z) \
cout << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z \
<< " = " << (z) << endl
#define showv(v) \
rep(i, v.size()) printf("%d%c", v[i], i == v.size() - 1 ? '\n' : ' ')
#define showv2(v) rep(j, v.size()) showv(v[j])
#define showt(t, n) rep(i, n) printf("%d%c", t[i], i == n - 1 ? '\n' : ' ')
#define showt2(t, r, c) rep(j, r) showt(t[j], c)
#define showvp(p) rep(i, p.size()) printf("%d %d\n", p[i].first, p[i].second)
#define printv(v) rep(i, v.size()) printf("%d\n", v[i])
#define printt(t, n) rep(i, n) printf("%d\n", t[i])
#define incl(v, x) (find(all(v), x) != v.end())
#define incls(s, c) (s.find(c) != string::npos)
#define lb(a, x) distance((a).begin(), lower_bound(all(a), (x)))
#define ub(a, x) distance((a).begin(), upper_bound(all(a), (x)))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define bit(n, k) ((n >> k) & 1) // nのk bit目
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define uni(x) x.erase(unique(all(x)), x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using vp = vector<P>;
using vt = vector<T>;
const int mod = 1000000007;
const double EPS = 1e-9;
// const long double EPS = 1e-14;
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 62) - 1;
#define dame \
{ \
puts("No"); \
return 0; \
}
#define yn \
{ puts("Yes"); } \
else { \
puts("No"); \
}
#define YN \
{ puts("YES"); } \
else { \
puts("NO"); \
}
inline int in() {
int x;
cin >> x;
return x;
}
inline ll lin() {
ll x;
cin >> x;
return x;
}
inline char chin() {
char x;
cin >> x;
return x;
}
inline string stin() {
string x;
cin >> x;
return x;
}
inline double din() {
double x;
cin >> x;
return x;
}
// template<class T = int> inline T in() { T x; cin >> x; return (x);}
template <typename T> inline ll suma(const vector<T> &a) {
ll res(0);
for (auto &&x : a)
res += x;
return res;
}
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;
}
char itoa(int n) { return n + '0'; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const int dy[2] = {1, 0};
const int dx[2] = {0, 1};
ll dp[3005][3005][4];
int main() {
int R, C, K;
cin >> R >> C >> K;
vi r(K), c(K), v(K);
map<P, int> id;
rep(i, K) {
cin >> r[i] >> c[i] >> v[i];
r[i]--, c[i]--;
id[P(r[i], c[i])] = i;
}
rep(k, 4) dp[0][0][k] = 0;
if (id.count(P(0, 0))) {
int vi = id[P(0, 0)];
dp[0][0][0] = 0;
dp[0][0][1] = v[vi];
}
rep(i, R) rep(j, C) rep(k, 4) {
rep(di, 2) {
int ni = i + dy[di], nj = j + dx[di], nk;
if (!isin(ni, 0, R) || !isin(nj, 0, C))
continue;
if (di == 0)
nk = 0;
else
nk = k;
chmax(dp[ni][nj][nk], dp[i][j][k]);
if (id.count(P(ni, nj))) {
int vi = id[P(ni, nj)];
nk++;
if (nk == 4)
continue;
chmax(dp[ni][nj][nk], dp[i][j][k] + v[vi]);
}
}
}
ll ans = 0;
rep(k, 4) chmax(ans, dp[R - 1][C - 1][k]);
cout << ans << endl;
return 0;
} | replace | 129 | 130 | 129 | 130 | 0 | |
p02586 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
// #include<unordered_map>
int main() {
int R, C, K;
std::cin >> R >> C >> K;
std::vector<std::vector<unsigned long long>> mat(
R, std::vector<unsigned long long>(C, 0));
std::vector<std::vector<bool>> flags(R, std::vector<bool>(C, 1));
std::vector<std::vector<std::vector<unsigned long long>>> dp(
R, std::vector<std::vector<unsigned long long>>(
C, std::vector<unsigned long long>(4, 0)));
for (int i = 0; i < K; ++i) {
int r, c, v;
std::cin >> r >> c >> v;
mat[r - 1][c - 1] = v;
}
std::queue<std::pair<int, int>> searchQ;
searchQ.push(std::pair<int, int>(0, 0));
flags[0][0] = 0;
// dp[0][0][1]=mat[0][0];
while (!searchQ.empty()) {
std::pair<int, int> index = searchQ.front();
int y = index.first, x = index.second;
searchQ.pop();
if (index.second + 1 < C) {
for (int i = 0; i < 4; ++i) {
if (dp[y][x + 1][i] < dp[y][x][i]) {
dp[y][x + 1][i] = dp[y][x][i];
}
}
if (mat[y][x] > 0) {
for (int i = 0; i < 3; ++i) {
unsigned long long buf = (dp[y][x][i] + mat[y][x]);
if (dp[y][x + 1][i + 1] < buf) {
dp[y][x + 1][i + 1] = buf;
}
}
}
if (flags[y][x + 1] == 1) {
searchQ.push(std::pair<int, int>(y, x + 1));
flags[y][x + 1] = 0;
}
}
if (index.first + 1 < R) {
unsigned long long max = 0;
for (int i = 0; i < 4; ++i) {
unsigned long long buf = dp[y][x][i];
if (mat[y][x] > 0 && i < 3) {
buf += mat[y][x];
}
if (max < buf) {
max = buf;
}
}
dp[y + 1][x][0] = max;
if (flags[y + 1][x] == 1) {
searchQ.push(std::pair<int, int>(y, x + 1));
flags[y + 1][x] = 0;
}
}
}
unsigned long long ans = 0;
for (int i = 0; i < 4; ++i) {
unsigned long long buf = dp[R - 1][C - 1][i];
if (mat[R - 1][C - 1] && i < 3) {
buf += mat[R - 1][C - 1];
}
if (ans < buf) {
ans = buf;
}
}
std::cout << ans << std::endl;
} | #include <iostream>
#include <queue>
#include <vector>
// #include<unordered_map>
int main() {
int R, C, K;
std::cin >> R >> C >> K;
std::vector<std::vector<unsigned long long>> mat(
R, std::vector<unsigned long long>(C, 0));
std::vector<std::vector<bool>> flags(R, std::vector<bool>(C, 1));
std::vector<std::vector<std::vector<unsigned long long>>> dp(
R, std::vector<std::vector<unsigned long long>>(
C, std::vector<unsigned long long>(4, 0)));
for (int i = 0; i < K; ++i) {
int r, c, v;
std::cin >> r >> c >> v;
mat[r - 1][c - 1] = v;
}
std::queue<std::pair<int, int>> searchQ;
searchQ.push(std::pair<int, int>(0, 0));
flags[0][0] = 0;
// dp[0][0][1]=mat[0][0];
while (!searchQ.empty()) {
std::pair<int, int> index = searchQ.front();
int y = index.first, x = index.second;
searchQ.pop();
if (index.second + 1 < C) {
for (int i = 0; i < 4; ++i) {
if (dp[y][x + 1][i] < dp[y][x][i]) {
dp[y][x + 1][i] = dp[y][x][i];
}
}
if (mat[y][x] > 0) {
for (int i = 0; i < 3; ++i) {
unsigned long long buf = (dp[y][x][i] + mat[y][x]);
if (dp[y][x + 1][i + 1] < buf) {
dp[y][x + 1][i + 1] = buf;
}
}
}
if (flags[y][x + 1] == 1) {
searchQ.push(std::pair<int, int>(y, x + 1));
flags[y][x + 1] = 0;
}
}
if (index.first + 1 < R) {
unsigned long long max = 0;
for (int i = 0; i < 4; ++i) {
unsigned long long buf = dp[y][x][i];
if (mat[y][x] > 0 && i < 3) {
buf += mat[y][x];
}
if (max < buf) {
max = buf;
}
}
dp[y + 1][x][0] = max;
if (flags[y + 1][x] == 1) {
searchQ.push(std::pair<int, int>(y + 1, x));
flags[y + 1][x] = 0;
}
}
}
unsigned long long ans = 0;
for (int i = 0; i < 4; ++i) {
unsigned long long buf = dp[R - 1][C - 1][i];
if (mat[R - 1][C - 1] && i < 3) {
buf += mat[R - 1][C - 1];
}
if (ans < buf) {
ans = buf;
}
}
std::cout << ans << std::endl;
} | replace | 58 | 59 | 58 | 59 | -11 | |
p02586 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
int field[3000][3000];
ll dp[4000][4000][4];
int main() {
int R, C, K;
cin >> R >> C >> K;
for (int i = 0; i < R + 1; i++) {
for (int j = 0; j < C + 1; j++) {
field[i][j] = 0;
}
}
for (int i = 0; i < K; i++) {
int r, c, v;
cin >> r >> c >> v;
r--;
c--;
field[r][c] = v;
}
for (int i = 0; i < R + 1; i++) {
for (int j = 0; j < C + 1; j++) {
for (int k = 0; k < 4; k++)
dp[i][j][k] = 0;
}
}
ll ans = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
for (int k = 0; k < 4; k++) {
if (k + 1 < 4) {
dp[i][j + 1][k + 1] =
max(dp[i][j + 1][k + 1], dp[i][j][k] + field[i][j]);
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + field[i][j]);
}
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
}
}
}
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
for (int k = 0; k < 4; k++) {
// cout << i << " " << j << " " << k << " " << dp[i][j][k] << endl;
ans = max(ans, dp[i][j][k]);
}
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
int field[3010][3010];
ll dp[4000][4000][4];
int main() {
int R, C, K;
cin >> R >> C >> K;
for (int i = 0; i < R + 1; i++) {
for (int j = 0; j < C + 1; j++) {
field[i][j] = 0;
}
}
for (int i = 0; i < K; i++) {
int r, c, v;
cin >> r >> c >> v;
r--;
c--;
field[r][c] = v;
}
for (int i = 0; i < R + 1; i++) {
for (int j = 0; j < C + 1; j++) {
for (int k = 0; k < 4; k++)
dp[i][j][k] = 0;
}
}
ll ans = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
for (int k = 0; k < 4; k++) {
if (k + 1 < 4) {
dp[i][j + 1][k + 1] =
max(dp[i][j + 1][k + 1], dp[i][j][k] + field[i][j]);
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + field[i][j]);
}
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
}
}
}
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
for (int k = 0; k < 4; k++) {
// cout << i << " " << j << " " << k << " " << dp[i][j][k] << endl;
ans = max(ans, dp[i][j][k]);
}
}
}
cout << ans << endl;
return 0;
}
| replace | 24 | 25 | 24 | 25 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;
#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) \
ASC((x)); \
REV((x))
#define pb push_back
#define eb emplace_back
int main() {
ll R, C, K;
cin >> R >> C >> K;
map<pair<ll, ll>, ll> S;
REP(i, 0, K) {
ll r, c, v;
cin >> r >> c >> v;
S[{r, c}] = v;
}
// vector<vector<vector<ll>>> dp(R+1, vector<vector<ll>>(C+1, vector<ll>(4)));
ll dp[R + 1][C + 1][4];
REP(i, 0, R + 1) REP(j, 0, C + 1) REP(k, 0, 4) dp[i][j][k] = 0;
REP(i, 1, R + 1) {
REP(j, 1, C + 1) {
ll x = 0;
if (S.count({i, j}) > 0)
x = S[{i, j}];
dp[i][j][0] = MAX(dp[i][j][0], dp[i][j - 1][0]);
REP(k, 0, 4) dp[i][j][0] = MAX(dp[i][j][0], dp[i - 1][j][k]);
REP(k, 1, 4) dp[i][j][k] = MAX(dp[i][j][0], dp[i][j - 1][k]);
dp[i][j][1] = MAX(dp[i][j][1], dp[i][j - 1][0] + x);
REP(k, 0, 4) dp[i][j][1] = MAX(dp[i][j][1], dp[i - 1][j][k] + x);
REP(k, 2, 4) dp[i][j][k] = MAX(dp[i][j][k], dp[i][j - 1][k - 1] + x);
}
}
ll ans = MAX(dp[R][C][0], dp[R][C][1]);
ans = MAX(ans, dp[R][C][2]);
ans = MAX(ans, dp[R][C][3]);
PR(ans);
return 0;
}
/*
*/ | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;
#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) \
ASC((x)); \
REV((x))
#define pb push_back
#define eb emplace_back
int main() {
ll R, C, K;
cin >> R >> C >> K;
map<pair<ll, ll>, ll> S;
REP(i, 0, K) {
ll r, c, v;
cin >> r >> c >> v;
S[{r, c}] = v;
}
vector<vector<vector<ll>>> dp(R + 1,
vector<vector<ll>>(C + 1, vector<ll>(4, 0)));
REP(i, 1, R + 1) {
REP(j, 1, C + 1) {
ll x = 0;
if (S.count({i, j}) > 0)
x = S[{i, j}];
dp[i][j][0] = MAX(dp[i][j][0], dp[i][j - 1][0]);
REP(k, 0, 4) dp[i][j][0] = MAX(dp[i][j][0], dp[i - 1][j][k]);
REP(k, 1, 4) dp[i][j][k] = MAX(dp[i][j][0], dp[i][j - 1][k]);
dp[i][j][1] = MAX(dp[i][j][1], dp[i][j - 1][0] + x);
REP(k, 0, 4) dp[i][j][1] = MAX(dp[i][j][1], dp[i - 1][j][k] + x);
REP(k, 2, 4) dp[i][j][k] = MAX(dp[i][j][k], dp[i][j - 1][k - 1] + x);
}
}
ll ans = MAX(dp[R][C][0], dp[R][C][1]);
ans = MAX(ans, dp[R][C][2]);
ans = MAX(ans, dp[R][C][3]);
PR(ans);
return 0;
}
/*
*/ | replace | 38 | 41 | 38 | 40 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int M = 1e3;
int n, m, mat[M][M];
long long memo[M][M][4];
long long solve(int i, int j, int cnt) {
if (i == n || j == m)
return 0;
long long &r = memo[i][j][cnt];
if (r == -1) {
r = max(solve(i + 1, j, 0), solve(i, j + 1, cnt));
if (cnt < 3 && mat[i][j])
r = max(r, mat[i][j] + max(solve(i + 1, j, 0), solve(i, j + 1, cnt + 1)));
}
return r;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(memo, -1, sizeof memo);
int k;
cin >> n >> m >> k;
while (k--) {
int i, j, x;
cin >> i >> j >> x;
mat[i - 1][j - 1] = x;
}
cout << solve(0, 0, 0) << '\n';
} | #include <bits/stdc++.h>
using namespace std;
const int M = 3e3;
int n, m, mat[M][M];
long long memo[M][M][4];
long long solve(int i, int j, int cnt) {
if (i == n || j == m)
return 0;
long long &r = memo[i][j][cnt];
if (r == -1) {
r = max(solve(i + 1, j, 0), solve(i, j + 1, cnt));
if (cnt < 3 && mat[i][j])
r = max(r, mat[i][j] + max(solve(i + 1, j, 0), solve(i, j + 1, cnt + 1)));
}
return r;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(memo, -1, sizeof memo);
int k;
cin >> n >> m >> k;
while (k--) {
int i, j, x;
cin >> i >> j >> x;
mat[i - 1][j - 1] = x;
}
cout << solve(0, 0, 0) << '\n';
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02586 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(i, a) for (ll i = ll(a - 1); i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
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;
}
inline void Yes(bool condition) {
if (condition)
PRINT("Yes");
else
PRINT("No");
}
template <class itr> void cins(itr first, itr last) {
for (auto i = first; i != last; i++) {
cin >> (*i);
}
}
template <class itr> void array_output(itr start, itr goal) {
string ans = "", k = " ";
for (auto i = start; i != goal; i++)
ans += to_string(*i) + k;
if (!ans.empty())
ans.pop_back();
PRINT(ans);
}
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
const ll INF = 1e15;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll EPS = 1e-10;
int sgn(const double a) { return (a < -EPS ? -1 : (a > EPS ? +1 : 0)); }
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> tri;
typedef pair<double, double> point;
typedef complex<double> Point;
const ll MAX = 30000;
constexpr ll nx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr ll ny[8] = {0, 1, 0, -1, 1, 1, -1, -1};
ll dp[3005][3005][4] = {0};
ll it[3005][3005] = {0};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll r, c, k;
cin >> r >> c >> k;
rep(i, k) {
ll rr, cc, v;
cin >> rr >> cc >> v;
it[rr][cc] = v;
}
rep(i, r + 1) {
rep(j, c + 1) {
rep(s, 4) {
if (i < r && it[i + 1][j]) {
chmax(dp[i + 1][j][1], dp[i][j][s] + it[i + 1][j]);
}
chmax(dp[i + 1][j][0], dp[i][j][k]);
if (j < c && s < 3 && it[i][j + 1]) {
chmax(dp[i][j + 1][s + 1], dp[i][j][s] + it[i][j + 1]);
}
chmax(dp[i][j + 1][s], dp[i][j][s]);
}
}
}
ll ans = 0;
rep(i, 4) {
chmax(ans, dp[r + 1][c][i]);
chmax(ans, dp[r][c + 1][i]);
}
PRINT(ans);
}
| #include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(i, a) for (ll i = ll(a - 1); i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
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;
}
inline void Yes(bool condition) {
if (condition)
PRINT("Yes");
else
PRINT("No");
}
template <class itr> void cins(itr first, itr last) {
for (auto i = first; i != last; i++) {
cin >> (*i);
}
}
template <class itr> void array_output(itr start, itr goal) {
string ans = "", k = " ";
for (auto i = start; i != goal; i++)
ans += to_string(*i) + k;
if (!ans.empty())
ans.pop_back();
PRINT(ans);
}
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
const ll INF = 1e15;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll EPS = 1e-10;
int sgn(const double a) { return (a < -EPS ? -1 : (a > EPS ? +1 : 0)); }
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> tri;
typedef pair<double, double> point;
typedef complex<double> Point;
const ll MAX = 30000;
constexpr ll nx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr ll ny[8] = {0, 1, 0, -1, 1, 1, -1, -1};
ll dp[3005][3005][4] = {0};
ll it[3005][3005] = {0};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll r, c, k;
cin >> r >> c >> k;
rep(i, k) {
ll rr, cc, v;
cin >> rr >> cc >> v;
it[rr][cc] = v;
}
rep(i, r + 1) {
rep(j, c + 1) {
rep(s, 4) {
if (i < r && it[i + 1][j]) {
chmax(dp[i + 1][j][1], dp[i][j][s] + it[i + 1][j]);
}
chmax(dp[i + 1][j][0], dp[i][j][s]);
if (j < c && s < 3 && it[i][j + 1]) {
chmax(dp[i][j + 1][s + 1], dp[i][j][s] + it[i][j + 1]);
}
chmax(dp[i][j + 1][s], dp[i][j][s]);
}
}
}
ll ans = 0;
rep(i, 4) {
chmax(ans, dp[r + 1][c][i]);
chmax(ans, dp[r][c + 1][i]);
}
PRINT(ans);
}
| replace | 97 | 98 | 97 | 98 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
int main() {
ll R, C, K;
cin >> R >> C >> K;
vector<ll> r(K), c(K), v(K);
rep(i, K) cin >> r[i] >> c[i] >> v[i];
vector<vector<ll>> point(R + 2, vector<ll>(C + 2));
rep(i, R + 2) {
rep(j, C + 2) { point[i][j] = 0; }
}
rep(i, K) { point[r[i]][c[i]] = v[i]; }
ll dp[R + 2][C + 2][5];
for (ll i = 0; i <= R + 1; i++) {
for (ll j = 0; j <= C + 1; j++) {
for (ll k = 0; k < 5; k++) {
dp[i][j][k] = 0;
}
}
}
dp[1][1][1] = point[1][1];
for (ll i = 1; i <= R; i++) {
for (ll j = 1; j <= C; j++) {
for (ll k = 0; k <= 3; k++) {
dp[i][j + 1][k + 1] = max(dp[i][j][k] + point[i][j + 1],
max(dp[i][j + 1][k + 1], dp[i][j][k + 1]));
dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k]);
}
}
for (ll j = 1; j <= C; j++) {
for (ll k = 0; k <= 3; k++) {
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + point[i + 1][j]);
}
}
}
ll ans = max(dp[R][C][0], max(dp[R][C][1], max(dp[R][C][2], dp[R][C][3])));
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
int main() {
ll R, C, K;
cin >> R >> C >> K;
vector<ll> r(K), c(K), v(K);
rep(i, K) cin >> r[i] >> c[i] >> v[i];
vector<vector<ll>> point(R + 2, vector<ll>(C + 2));
rep(i, R + 2) {
rep(j, C + 2) { point[i][j] = 0; }
}
rep(i, K) { point[r[i]][c[i]] = v[i]; }
vector<vector<vector<ll>>> dp(R + 2,
vector<vector<ll>>(C + 2, vector<ll>(5)));
for (ll i = 0; i <= R + 1; i++) {
for (ll j = 0; j <= C + 1; j++) {
for (ll k = 0; k < 5; k++) {
dp[i][j][k] = 0;
}
}
}
dp[1][1][1] = point[1][1];
for (ll i = 1; i <= R; i++) {
for (ll j = 1; j <= C; j++) {
for (ll k = 0; k <= 3; k++) {
dp[i][j + 1][k + 1] = max(dp[i][j][k] + point[i][j + 1],
max(dp[i][j + 1][k + 1], dp[i][j][k + 1]));
dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k]);
}
}
for (ll j = 1; j <= C; j++) {
for (ll k = 0; k <= 3; k++) {
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + point[i + 1][j]);
}
}
}
ll ans = max(dp[R][C][0], max(dp[R][C][1], max(dp[R][C][2], dp[R][C][3])));
cout << ans << endl;
}
| replace | 17 | 18 | 17 | 19 | 0 | |
p02586 | C++ | Runtime Error | #ifndef INCLUDEHEADER_MY_TEMPLATE
#define INCLUDEHEADER_MY_TEMPLATE
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
#define y0 qvya13579
#define y1 qvyb24680
#define j0 qvja13579
#define j1 qvjb24680
#define next qvne13579xt
#define prev qvpr13579ev
#define INF 1000000007
#define MOD 1000000007
#define endl "\n"
#define PI acos(-1.0)
#if __cplusplus < 201103L
#define stoi(argument_string) atoi((argument_string).c_str())
#define stoll(argument_string) atoll((argument_string).c_str())
#endif
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP_R(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define FOR(i, m, n) for (int i = ((int)(m)); i < (int)(n); ++i)
#define FOR_R(i, m, n) for (int i = ((int)(m)-1); i >= (int)(n); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define SIZ(x) ((int)(x).size())
#define CIN(x) cin >> (x)
#define CIN2(x, y) cin >> (x) >> (y)
#define CIN3(x, y, z) cin >> (x) >> (y) >> (z)
#define CIN4(x, y, z, w) cin >> (x) >> (y) >> (z) >> (w)
#define CIN5(x, y, z, w, u) cin >> (x) >> (y) >> (z) >> (w) >> (u)
#define SCAND(x) scanf("%d", &(x))
#define SCAND2(x, y) scanf("%d%d", &(x), &(y))
#define SCAND3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z))
#define SCAND4(x, y, z, w) scanf("%d%d%d%d", &(x), &(y), &(z), &(w))
#define SCAND5(x, y, z, w, u) scanf("%d%d%d%d%d", &(x), &(y), &(z), &(w), &(u))
#define SCANLLD(x) scanf("%lld", &(x))
#define SCANLLD2(x, y) scanf("%lld%lld", &(x), &(y))
#define SCANLLD3(x, y, z) scanf("%lld%lld%lld", &(x), &(y), &(z))
#define SCANLLD4(x, y, z, w) scanf("%lld%lld%lld%lld", &(x), &(y), &(z), &(w))
#define SCANLLD5(x, y, z, w, u) \
scanf("%lld%lld%lld%lld%lld", &(x), &(y), &(z), &(w), &(u))
#define PRINTD(x) printf("%d\n", (x))
#define PRINTLLD(x) printf("%lld\n", (x))
#define DEBUG(argument) cerr << (#argument) << " : " << (argument) << "\n"
typedef long long lli;
typedef unsigned long long ulli;
using namespace std;
#endif // header
int ctoi(char c) {
if (c >= '0' and c <= '9') {
return (int)(c - '0');
}
return -1;
}
int alphabet_pos(char c) {
if (c >= 'a' and c <= 'z') {
return (int)(c - 'a');
}
return -1;
}
int alphabet_pos_capital(char c) {
if (c >= 'A' and c <= 'Z') {
return (int)(c - 'A');
}
return -1;
}
vector<string> split(string str, char ch) {
int first = 0;
int last = str.find_first_of(ch);
if (last == ((int)string::npos)) {
last = SIZ(str);
}
vector<string> result;
while (first < SIZ(str)) {
string Ssubstr(str, first, last - first);
result.push_back(Ssubstr);
first = last + 1;
last = str.find_first_of(ch, first);
if (last == ((int)string::npos)) {
last = SIZ(str);
}
}
return result;
}
int gcd(int a, int b) // assuming a,b >= 1
{
if (a < b) {
swap(a, b);
}
if (b == 0) {
return a;
}
if (a % b == 0) {
return b;
}
return gcd(b, a % b);
}
long long gcd(long long a, long long b) // assuming a,b >= 1
{
if (a < b) {
swap(a, b);
}
if (b == 0LL) {
return a;
}
if (a % b == 0) {
return b;
}
return gcd(b, a % b);
}
int lcm(int a, int b) // assuming a,b >= 1
{
return a * b / gcd(a, b);
}
long long lcm(long long a, long long b) // assuming a,b >= 1
{
return a * b / gcd(a, b);
}
long long pow_fast(long long x, long long n_power, long long modulus) {
if (n_power == 0) {
return 1;
}
if (n_power % 2 == 0) {
return pow_fast(x * x % modulus, n_power / 2, modulus);
}
return x * pow_fast(x, n_power - 1, modulus) % modulus;
}
struct CombinationTable {
vector<vector<long long>> val;
CombinationTable(int size)
: val(size + 1, vector<long long>(size + 1)) // constructor
{
for (int i = 0; i <= size; ++i) // note that 0 <= i <= size
{
for (int j = 0; j <= i; ++j) {
if (j == 0 or j == i) {
val[i][j] = 1LL;
} else {
val[i][j] = val[i - 1][j - 1] + val[i - 1][j];
}
}
}
}
};
void print_vector(vector<int> h, bool verbose = true) {
int L = h.size();
for (int i = 0; i < L; ++i) {
if (verbose) {
printf("%d", h[i]);
} else {
cerr << h[i];
}
if (i != L - 1) {
if (verbose) {
printf(" ");
} else {
cerr << " ";
}
} else {
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
}
}
void print_vector(vector<long long> h, bool verbose = true) {
int L = h.size();
for (int i = 0; i < L; ++i) {
if (verbose) {
printf("%lld", h[i]);
} else {
cerr << h[i];
}
if (i != L - 1) {
if (verbose) {
printf(" ");
} else {
cerr << " ";
}
} else {
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
}
}
void print_matrix2D(vector<vector<int>> h, bool verbose = true) {
int Ly = h.size();
for (int i = 0; i < Ly; ++i) {
int Lx = h[i].size();
for (int j = 0; j < Lx; ++j) {
if (verbose) {
printf("%d", h[i][j]);
} else {
cerr << h[i][j];
}
if (j != Lx - 1) {
if (verbose) {
printf(" ");
} else {
cerr << " ";
}
} else {
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
}
}
}
void print_matrix2D(vector<vector<long long>> h, bool verbose = true) {
int Ly = h.size();
for (int i = 0; i < Ly; ++i) {
int Lx = h[i].size();
for (int j = 0; j < Lx; ++j) {
if (verbose) {
printf("%lld", h[i][j]);
} else {
cerr << h[i][j];
}
if (j != Lx - 1) {
if (verbose) {
printf(" ");
} else {
cerr << " ";
}
} else {
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
}
}
}
void print_matrix2D(vector<string> h, bool verbose = true) {
int Ly = h.size();
for (int i = 0; i < Ly; ++i) {
int Lx = h[i].size();
for (int j = 0; j < Lx; ++j) {
if (verbose) {
printf("%c", h[i][j]);
} else {
cerr << h[i][j];
}
}
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
}
void print_binary(int val, int num_digit = 31, bool verbose = false) {
for (int k = num_digit - 1; k >= 0; --k) {
if (verbose) {
printf("%d", (val >> k) & 1);
} else {
cerr << ((val >> k) & 1);
}
}
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
void print_binary(long long val, int num_digit = 63, bool verbose = false) {
for (int k = num_digit - 1; k >= 0; --k) {
if (verbose) {
printf("%lld", ((val >> k) & 1));
} else {
cerr << ((val >> k) & 1);
}
}
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
void print_all() { cout << endl; }
void print_all_nobreak() { return; }
template <class Head, class... Tail>
void print_all_nobreak(Head head, Tail... tail) {
cout << head;
if (sizeof...(tail) != 0) {
cout << " ";
}
print_all_nobreak(forward<Tail>(tail)...);
}
template <class Head, class... Tail> void print_all(Head head, Tail... tail) {
cout << head;
if (sizeof...(tail) != 0) {
cout << " ";
}
print_all(forward<Tail>(tail)...);
}
template <class T> void print_all_nobreak(vector<T> vec) {
cout << "[";
for (int i = 0; i < vec.size(); i++) {
cout << vec[i];
if (i != ((int)vec.size() - 1)) {
cout << ", ";
}
}
cout << "]";
}
template <class T> void print_all(vector<T> vec) {
cout << "[";
for (int i = 0; i < vec.size(); i++) {
cout << vec[i];
if (i != ((int)vec.size() - 1)) {
cout << ", ";
}
}
cout << "]";
cout << endl;
}
template <class T> void print_all(vector<vector<T>> df) {
cout << "[";
for (int i = 0; i < df.size(); i++) {
print_all_nobreak(df[i]);
if (i != ((int)df.size() - 1)) {
cout << ", ";
}
}
cout << "]";
cout << endl;
}
template <class T> void print_all_nobreak(vector<vector<T>> df) {
cout << "[";
for (int i = 0; i < df.size(); i++) {
print_all_nobreak(df[i]);
if (i != ((int)df.size() - 1)) {
cout << ", ";
}
}
cout << "]";
}
template <class T1, class T2> void print_all_nobreak(pair<T1, T2> p) {
cout << "(";
print_all_nobreak(p.first);
cout << ", ";
print_all_nobreak(p.second);
cout << ")";
}
template <class T1, class T2> void print_all(pair<T1, T2> p) {
cout << "(";
print_all_nobreak(p.first);
cout << ", ";
print_all_nobreak(p.second);
cout << ")";
cout << endl;
}
struct UnionFind // size-based
{
vector<int> parent, treesize;
UnionFind(int size)
: parent(size), treesize(size, 1) // constructor
{
for (int i = 0; i < size; ++i) {
parent[i] = i;
}
}
int root(int x) {
if (parent[x] == x) {
return x;
}
return parent[x] = root(parent[x]);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return;
}
if (treesize[x] < treesize[y]) {
parent[x] = y;
treesize[y] += treesize[x];
} else {
parent[y] = x;
treesize[x] += treesize[y];
}
}
bool sametree(int x, int y) { return root(x) == root(y); }
int gettreesize(int x) { return treesize[root(x)]; }
};
template <typename Type_value>
struct SegmentTree // Range Minimum Query (RMQ)
{
private:
int n;
vector<Type_value> node;
Type_value identity_element_segmenttree;
public:
SegmentTree(vector<Type_value> v,
Type_value identity_element_st) // constructor
{
int sz = v.size();
identity_element_segmenttree = identity_element_st;
n = 1;
while (n < sz) {
n <<= 1;
}
node.resize(2 * n - 1, identity_element_segmenttree);
for (int i = 0; i < sz; ++i) {
node[i + n - 1] = v[i];
}
for (int i = n - 2; i >= 0; --i) {
node[i] = min(node[2 * i + 1], node[2 * i + 2]);
}
}
void update(int x, Type_value val) {
x += (n - 1);
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = min(node[2 * x + 1], node[2 * x + 2]);
}
}
Type_value getmin(int a, int b, int k = 0, int l = 0,
int r = -1) // getting minimum value in [a,b)
{
// k : index of the referred node
// [l,r) : range covered by the k-th node
if (r < 0) {
r = n;
}
if (r <= a or b <= l) {
return identity_element_segmenttree;
}
if (a <= l and r <= b) {
return node[k];
}
Type_value vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
Type_value vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
return min(vl, vr);
}
};
template <typename Type_value>
struct BinaryIndexedTree // Range Sum Query (RSQ), 0-indexed
{
private:
int size_;
vector<Type_value> data;
public:
BinaryIndexedTree(int sz, Type_value identity_element_binaryindexedtree =
0.0) // constructor
{
size_ = sz;
data.resize(sz + 1, identity_element_binaryindexedtree);
}
Type_value sum(int i) // sum within [0,i)
{
if (i <= 0) {
return (Type_value)0.0;
}
if (i > size_) {
i = size_;
}
Type_value sm = 0.0;
while (i > 0) {
sm += data[i];
i -= i & -i;
}
return sm;
}
void add(int i, Type_value x) {
if (i < 0 or i >= size_) {
return;
}
++i;
while (i <= size_) {
data[i] += x;
i += i & -i;
}
}
};
struct RollingHash {
/*
10 primes near 1e9(for other mod(s)):
{
1000000007,
1000000009,
1000000021,
1000000033,
1000000087,
1000000093,
1000000097,
1000000103,
1000000123,
1000000181
}
10 primes near 1e5(for other base(s)):
{100003,
100019,
100043,
100049,
100057,
100069,
100103,
100109,
100129,
100151}
*/
private:
vector<unsigned long long> hash;
vector<unsigned long long> base_pow;
unsigned long long base, modulus;
const unsigned long long MODULUS_DEFAULT = (1ULL << 61) - 1;
const unsigned long long MASK30 = (1ULL << 30) - 1;
const unsigned long long MASK31 = (1ULL << 31) - 1;
const unsigned long long BASE_MIN = 1e7;
unsigned long long Modulus_2pow61m1(unsigned long long val) {
val = (val & MODULUS_DEFAULT) + (val >> 61);
if (MODULUS_DEFAULT < val) {
val -= MODULUS_DEFAULT;
}
return val;
}
unsigned long long Multiple_2pow61m1(unsigned long long a,
unsigned long long b) {
unsigned long long au = a >> 31;
unsigned long long ad = a & MASK31;
unsigned long long bu = b >> 31;
unsigned long long bd = b & MASK31;
unsigned long long mid = ad * bu + au * bd;
unsigned long long midu = mid >> 30;
unsigned long long midd = mid & MASK30;
return Modulus_2pow61m1(((au * bu) << 1) + midu + (midd << 31) + ad * bd);
}
void initialize(string &S) {
int N = S.size();
vector<int> s(N);
for (int i = 0; i < N; ++i) {
s[i] = S[i];
}
initialize(s);
}
void initialize(vector<int> &S) {
hash.resize(S.size() + 1);
base_pow.resize(S.size() + 1);
hash[0] = 0;
base_pow[0] = 1;
if (modulus == MODULUS_DEFAULT) {
for (int i = 1; i <= ((int)S.size()); ++i) {
hash[i] =
Modulus_2pow61m1(Multiple_2pow61m1(hash[i - 1], base) + S[i - 1]);
base_pow[i] = Multiple_2pow61m1(base_pow[i - 1], base);
}
} else {
for (int i = 1; i <= ((int)S.size()); ++i) {
hash[i] = (hash[i - 1] * base + S[i - 1]) % modulus;
base_pow[i] = (base_pow[i - 1] * base) % modulus;
}
}
}
public:
RollingHash(string S = "", unsigned long long base_ = 0,
unsigned long long modulus_ = 0) {
if (0 < modulus_) {
modulus = modulus_;
} else {
modulus = MODULUS_DEFAULT;
}
if (0 < base_) {
base = base_;
} else {
mt19937_64 mt64(static_cast<unsigned int>(time(nullptr)));
uniform_int_distribution<unsigned long long> rand_uniform(
BASE_MIN, modulus - BASE_MIN);
base = rand_uniform(mt64);
}
if (S.size() > 0) {
initialize(S);
}
}
// 0-indexed, [a, b)
unsigned long long between(int a, int b) {
if (modulus == MODULUS_DEFAULT) {
return Modulus_2pow61m1(modulus + hash[b] -
Multiple_2pow61m1(hash[a], base_pow[b - a]));
}
return (modulus + hash[b] - (hash[a] * base_pow[b - a]) % modulus) %
modulus;
}
};
struct TrieTree {
private:
char chara_origin;
int numtypes_ch;
struct TrieNode {
public:
TrieNode **children;
bool isLeaf;
};
TrieNode *newNode() {
TrieNode *pNode = new TrieNode;
pNode->children = new TrieNode *[numtypes_ch];
pNode->isLeaf = false;
for (int i = 0; i < numtypes_ch; i++) {
pNode->children[i] = nullptr;
}
return pNode;
}
TrieNode *treeroot;
public:
TrieTree(int number_of_types_of_character = 26, char character_origin = 'a') {
numtypes_ch = number_of_types_of_character;
chara_origin = character_origin;
treeroot = newNode();
}
void insert(string key) {
TrieNode *pCrawl = treeroot;
for (int i = 0; i < ((int)key.size()); i++) {
int index = key[i] - chara_origin;
if (pCrawl->children[index] == nullptr) {
pCrawl->children[index] = newNode();
}
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
pCrawl->isLeaf = true;
}
bool search(string key) {
TrieNode *pCrawl = treeroot;
for (int i = 0; i < ((int)key.size()); i++) {
int index = key[i] - chara_origin;
if (pCrawl->children[index] == nullptr) {
return false;
}
pCrawl = pCrawl->children[index];
}
return (pCrawl != nullptr and pCrawl->isLeaf);
}
};
/*------------------ the end of the template -----------------------*/
// global variables
lli dp[3000][3000][4];
int vals[3000][3000];
int H;
int W;
int K;
vector<int> r, c, v;
lli expected;
void give_inputs(int state_debug) {
if (state_debug == 0) {
// standard I/O
cin.tie(0);
ios::sync_with_stdio(false); /* making cin faster */
SCAND3(H, W, K);
REP(i, K) {
SCAND3(r[i], c[i], v[i]);
r[i]--;
c[i]--;
}
} else if (state_debug == 1) {
// testcase 1
H = 2;
W = 2;
K = 3;
r = {0, 1, 0};
c = {0, 0, 1};
v = {3, 4, 5};
expected = 8;
} else if (state_debug == 2) {
// testcase 2
H = 2;
W = 5;
K = 5;
r = {0, 1, 0, 0, 0};
c = {0, 3, 1, 2, 3};
v = {3, 20, 1, 4, 2};
expected = 29;
} else if (state_debug == 3) {
// testcase 3
H = 4;
W = 5;
K = 10;
r = {1, 0, 1, 0, 0, 1, 2, 3, 2, 0};
c = {4, 4, 2, 1, 0, 3, 1, 4, 4, 2};
v = {12, 12, 15, 20, 28, 26, 27, 21, 10, 10};
expected = 142;
}
}
lli solve() // ここに処理を書く。型はansに合わせる。
{
REP(i, 3000) {
REP(j, 3000) {
REP(k, 4) { dp[i][j][k] = 0; }
vals[i][j] = 0;
}
}
REP(i, K) { vals[r[i]][c[i]] = v[i]; }
REP(s, H + W - 1) {
REP(i, H) {
int j = s - i;
if (not(0 <= j and j < W)) {
continue;
}
REP(k, 4) {
if (0 < j) {
dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
}
if (0 < i) {
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
}
}
REP_R(k, 4) {
if (0 < k) {
dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + vals[i][j]);
}
}
}
}
lli ans = 0;
REP(k, 4) { ans = max(ans, dp[H - 1][W - 1][k]); }
return ans;
}
signed main(signed argc, char *argv[]) {
if (argc == 1) {
give_inputs(0);
} else {
give_inputs(stoi(string(argv[1])));
}
auto ans = solve();
/*
print here!!!
*/
PRINTLLD(ans);
// confirm expected == ans
if (argc != 1 and 0 < stoi(string(argv[1])) and expected != ans) {
puts("[WA]");
puts("expected: ");
print_all(expected);
puts("found: ");
print_all(ans);
}
}
| #ifndef INCLUDEHEADER_MY_TEMPLATE
#define INCLUDEHEADER_MY_TEMPLATE
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
#define y0 qvya13579
#define y1 qvyb24680
#define j0 qvja13579
#define j1 qvjb24680
#define next qvne13579xt
#define prev qvpr13579ev
#define INF 1000000007
#define MOD 1000000007
#define endl "\n"
#define PI acos(-1.0)
#if __cplusplus < 201103L
#define stoi(argument_string) atoi((argument_string).c_str())
#define stoll(argument_string) atoll((argument_string).c_str())
#endif
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP_R(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define FOR(i, m, n) for (int i = ((int)(m)); i < (int)(n); ++i)
#define FOR_R(i, m, n) for (int i = ((int)(m)-1); i >= (int)(n); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define SIZ(x) ((int)(x).size())
#define CIN(x) cin >> (x)
#define CIN2(x, y) cin >> (x) >> (y)
#define CIN3(x, y, z) cin >> (x) >> (y) >> (z)
#define CIN4(x, y, z, w) cin >> (x) >> (y) >> (z) >> (w)
#define CIN5(x, y, z, w, u) cin >> (x) >> (y) >> (z) >> (w) >> (u)
#define SCAND(x) scanf("%d", &(x))
#define SCAND2(x, y) scanf("%d%d", &(x), &(y))
#define SCAND3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z))
#define SCAND4(x, y, z, w) scanf("%d%d%d%d", &(x), &(y), &(z), &(w))
#define SCAND5(x, y, z, w, u) scanf("%d%d%d%d%d", &(x), &(y), &(z), &(w), &(u))
#define SCANLLD(x) scanf("%lld", &(x))
#define SCANLLD2(x, y) scanf("%lld%lld", &(x), &(y))
#define SCANLLD3(x, y, z) scanf("%lld%lld%lld", &(x), &(y), &(z))
#define SCANLLD4(x, y, z, w) scanf("%lld%lld%lld%lld", &(x), &(y), &(z), &(w))
#define SCANLLD5(x, y, z, w, u) \
scanf("%lld%lld%lld%lld%lld", &(x), &(y), &(z), &(w), &(u))
#define PRINTD(x) printf("%d\n", (x))
#define PRINTLLD(x) printf("%lld\n", (x))
#define DEBUG(argument) cerr << (#argument) << " : " << (argument) << "\n"
typedef long long lli;
typedef unsigned long long ulli;
using namespace std;
#endif // header
int ctoi(char c) {
if (c >= '0' and c <= '9') {
return (int)(c - '0');
}
return -1;
}
int alphabet_pos(char c) {
if (c >= 'a' and c <= 'z') {
return (int)(c - 'a');
}
return -1;
}
int alphabet_pos_capital(char c) {
if (c >= 'A' and c <= 'Z') {
return (int)(c - 'A');
}
return -1;
}
vector<string> split(string str, char ch) {
int first = 0;
int last = str.find_first_of(ch);
if (last == ((int)string::npos)) {
last = SIZ(str);
}
vector<string> result;
while (first < SIZ(str)) {
string Ssubstr(str, first, last - first);
result.push_back(Ssubstr);
first = last + 1;
last = str.find_first_of(ch, first);
if (last == ((int)string::npos)) {
last = SIZ(str);
}
}
return result;
}
int gcd(int a, int b) // assuming a,b >= 1
{
if (a < b) {
swap(a, b);
}
if (b == 0) {
return a;
}
if (a % b == 0) {
return b;
}
return gcd(b, a % b);
}
long long gcd(long long a, long long b) // assuming a,b >= 1
{
if (a < b) {
swap(a, b);
}
if (b == 0LL) {
return a;
}
if (a % b == 0) {
return b;
}
return gcd(b, a % b);
}
int lcm(int a, int b) // assuming a,b >= 1
{
return a * b / gcd(a, b);
}
long long lcm(long long a, long long b) // assuming a,b >= 1
{
return a * b / gcd(a, b);
}
long long pow_fast(long long x, long long n_power, long long modulus) {
if (n_power == 0) {
return 1;
}
if (n_power % 2 == 0) {
return pow_fast(x * x % modulus, n_power / 2, modulus);
}
return x * pow_fast(x, n_power - 1, modulus) % modulus;
}
struct CombinationTable {
vector<vector<long long>> val;
CombinationTable(int size)
: val(size + 1, vector<long long>(size + 1)) // constructor
{
for (int i = 0; i <= size; ++i) // note that 0 <= i <= size
{
for (int j = 0; j <= i; ++j) {
if (j == 0 or j == i) {
val[i][j] = 1LL;
} else {
val[i][j] = val[i - 1][j - 1] + val[i - 1][j];
}
}
}
}
};
void print_vector(vector<int> h, bool verbose = true) {
int L = h.size();
for (int i = 0; i < L; ++i) {
if (verbose) {
printf("%d", h[i]);
} else {
cerr << h[i];
}
if (i != L - 1) {
if (verbose) {
printf(" ");
} else {
cerr << " ";
}
} else {
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
}
}
void print_vector(vector<long long> h, bool verbose = true) {
int L = h.size();
for (int i = 0; i < L; ++i) {
if (verbose) {
printf("%lld", h[i]);
} else {
cerr << h[i];
}
if (i != L - 1) {
if (verbose) {
printf(" ");
} else {
cerr << " ";
}
} else {
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
}
}
void print_matrix2D(vector<vector<int>> h, bool verbose = true) {
int Ly = h.size();
for (int i = 0; i < Ly; ++i) {
int Lx = h[i].size();
for (int j = 0; j < Lx; ++j) {
if (verbose) {
printf("%d", h[i][j]);
} else {
cerr << h[i][j];
}
if (j != Lx - 1) {
if (verbose) {
printf(" ");
} else {
cerr << " ";
}
} else {
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
}
}
}
void print_matrix2D(vector<vector<long long>> h, bool verbose = true) {
int Ly = h.size();
for (int i = 0; i < Ly; ++i) {
int Lx = h[i].size();
for (int j = 0; j < Lx; ++j) {
if (verbose) {
printf("%lld", h[i][j]);
} else {
cerr << h[i][j];
}
if (j != Lx - 1) {
if (verbose) {
printf(" ");
} else {
cerr << " ";
}
} else {
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
}
}
}
void print_matrix2D(vector<string> h, bool verbose = true) {
int Ly = h.size();
for (int i = 0; i < Ly; ++i) {
int Lx = h[i].size();
for (int j = 0; j < Lx; ++j) {
if (verbose) {
printf("%c", h[i][j]);
} else {
cerr << h[i][j];
}
}
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
}
void print_binary(int val, int num_digit = 31, bool verbose = false) {
for (int k = num_digit - 1; k >= 0; --k) {
if (verbose) {
printf("%d", (val >> k) & 1);
} else {
cerr << ((val >> k) & 1);
}
}
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
void print_binary(long long val, int num_digit = 63, bool verbose = false) {
for (int k = num_digit - 1; k >= 0; --k) {
if (verbose) {
printf("%lld", ((val >> k) & 1));
} else {
cerr << ((val >> k) & 1);
}
}
if (verbose) {
printf("\n");
} else {
cerr << "\n";
}
}
void print_all() { cout << endl; }
void print_all_nobreak() { return; }
template <class Head, class... Tail>
void print_all_nobreak(Head head, Tail... tail) {
cout << head;
if (sizeof...(tail) != 0) {
cout << " ";
}
print_all_nobreak(forward<Tail>(tail)...);
}
template <class Head, class... Tail> void print_all(Head head, Tail... tail) {
cout << head;
if (sizeof...(tail) != 0) {
cout << " ";
}
print_all(forward<Tail>(tail)...);
}
template <class T> void print_all_nobreak(vector<T> vec) {
cout << "[";
for (int i = 0; i < vec.size(); i++) {
cout << vec[i];
if (i != ((int)vec.size() - 1)) {
cout << ", ";
}
}
cout << "]";
}
template <class T> void print_all(vector<T> vec) {
cout << "[";
for (int i = 0; i < vec.size(); i++) {
cout << vec[i];
if (i != ((int)vec.size() - 1)) {
cout << ", ";
}
}
cout << "]";
cout << endl;
}
template <class T> void print_all(vector<vector<T>> df) {
cout << "[";
for (int i = 0; i < df.size(); i++) {
print_all_nobreak(df[i]);
if (i != ((int)df.size() - 1)) {
cout << ", ";
}
}
cout << "]";
cout << endl;
}
template <class T> void print_all_nobreak(vector<vector<T>> df) {
cout << "[";
for (int i = 0; i < df.size(); i++) {
print_all_nobreak(df[i]);
if (i != ((int)df.size() - 1)) {
cout << ", ";
}
}
cout << "]";
}
template <class T1, class T2> void print_all_nobreak(pair<T1, T2> p) {
cout << "(";
print_all_nobreak(p.first);
cout << ", ";
print_all_nobreak(p.second);
cout << ")";
}
template <class T1, class T2> void print_all(pair<T1, T2> p) {
cout << "(";
print_all_nobreak(p.first);
cout << ", ";
print_all_nobreak(p.second);
cout << ")";
cout << endl;
}
struct UnionFind // size-based
{
vector<int> parent, treesize;
UnionFind(int size)
: parent(size), treesize(size, 1) // constructor
{
for (int i = 0; i < size; ++i) {
parent[i] = i;
}
}
int root(int x) {
if (parent[x] == x) {
return x;
}
return parent[x] = root(parent[x]);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return;
}
if (treesize[x] < treesize[y]) {
parent[x] = y;
treesize[y] += treesize[x];
} else {
parent[y] = x;
treesize[x] += treesize[y];
}
}
bool sametree(int x, int y) { return root(x) == root(y); }
int gettreesize(int x) { return treesize[root(x)]; }
};
template <typename Type_value>
struct SegmentTree // Range Minimum Query (RMQ)
{
private:
int n;
vector<Type_value> node;
Type_value identity_element_segmenttree;
public:
SegmentTree(vector<Type_value> v,
Type_value identity_element_st) // constructor
{
int sz = v.size();
identity_element_segmenttree = identity_element_st;
n = 1;
while (n < sz) {
n <<= 1;
}
node.resize(2 * n - 1, identity_element_segmenttree);
for (int i = 0; i < sz; ++i) {
node[i + n - 1] = v[i];
}
for (int i = n - 2; i >= 0; --i) {
node[i] = min(node[2 * i + 1], node[2 * i + 2]);
}
}
void update(int x, Type_value val) {
x += (n - 1);
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = min(node[2 * x + 1], node[2 * x + 2]);
}
}
Type_value getmin(int a, int b, int k = 0, int l = 0,
int r = -1) // getting minimum value in [a,b)
{
// k : index of the referred node
// [l,r) : range covered by the k-th node
if (r < 0) {
r = n;
}
if (r <= a or b <= l) {
return identity_element_segmenttree;
}
if (a <= l and r <= b) {
return node[k];
}
Type_value vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
Type_value vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
return min(vl, vr);
}
};
template <typename Type_value>
struct BinaryIndexedTree // Range Sum Query (RSQ), 0-indexed
{
private:
int size_;
vector<Type_value> data;
public:
BinaryIndexedTree(int sz, Type_value identity_element_binaryindexedtree =
0.0) // constructor
{
size_ = sz;
data.resize(sz + 1, identity_element_binaryindexedtree);
}
Type_value sum(int i) // sum within [0,i)
{
if (i <= 0) {
return (Type_value)0.0;
}
if (i > size_) {
i = size_;
}
Type_value sm = 0.0;
while (i > 0) {
sm += data[i];
i -= i & -i;
}
return sm;
}
void add(int i, Type_value x) {
if (i < 0 or i >= size_) {
return;
}
++i;
while (i <= size_) {
data[i] += x;
i += i & -i;
}
}
};
struct RollingHash {
/*
10 primes near 1e9(for other mod(s)):
{
1000000007,
1000000009,
1000000021,
1000000033,
1000000087,
1000000093,
1000000097,
1000000103,
1000000123,
1000000181
}
10 primes near 1e5(for other base(s)):
{100003,
100019,
100043,
100049,
100057,
100069,
100103,
100109,
100129,
100151}
*/
private:
vector<unsigned long long> hash;
vector<unsigned long long> base_pow;
unsigned long long base, modulus;
const unsigned long long MODULUS_DEFAULT = (1ULL << 61) - 1;
const unsigned long long MASK30 = (1ULL << 30) - 1;
const unsigned long long MASK31 = (1ULL << 31) - 1;
const unsigned long long BASE_MIN = 1e7;
unsigned long long Modulus_2pow61m1(unsigned long long val) {
val = (val & MODULUS_DEFAULT) + (val >> 61);
if (MODULUS_DEFAULT < val) {
val -= MODULUS_DEFAULT;
}
return val;
}
unsigned long long Multiple_2pow61m1(unsigned long long a,
unsigned long long b) {
unsigned long long au = a >> 31;
unsigned long long ad = a & MASK31;
unsigned long long bu = b >> 31;
unsigned long long bd = b & MASK31;
unsigned long long mid = ad * bu + au * bd;
unsigned long long midu = mid >> 30;
unsigned long long midd = mid & MASK30;
return Modulus_2pow61m1(((au * bu) << 1) + midu + (midd << 31) + ad * bd);
}
void initialize(string &S) {
int N = S.size();
vector<int> s(N);
for (int i = 0; i < N; ++i) {
s[i] = S[i];
}
initialize(s);
}
void initialize(vector<int> &S) {
hash.resize(S.size() + 1);
base_pow.resize(S.size() + 1);
hash[0] = 0;
base_pow[0] = 1;
if (modulus == MODULUS_DEFAULT) {
for (int i = 1; i <= ((int)S.size()); ++i) {
hash[i] =
Modulus_2pow61m1(Multiple_2pow61m1(hash[i - 1], base) + S[i - 1]);
base_pow[i] = Multiple_2pow61m1(base_pow[i - 1], base);
}
} else {
for (int i = 1; i <= ((int)S.size()); ++i) {
hash[i] = (hash[i - 1] * base + S[i - 1]) % modulus;
base_pow[i] = (base_pow[i - 1] * base) % modulus;
}
}
}
public:
RollingHash(string S = "", unsigned long long base_ = 0,
unsigned long long modulus_ = 0) {
if (0 < modulus_) {
modulus = modulus_;
} else {
modulus = MODULUS_DEFAULT;
}
if (0 < base_) {
base = base_;
} else {
mt19937_64 mt64(static_cast<unsigned int>(time(nullptr)));
uniform_int_distribution<unsigned long long> rand_uniform(
BASE_MIN, modulus - BASE_MIN);
base = rand_uniform(mt64);
}
if (S.size() > 0) {
initialize(S);
}
}
// 0-indexed, [a, b)
unsigned long long between(int a, int b) {
if (modulus == MODULUS_DEFAULT) {
return Modulus_2pow61m1(modulus + hash[b] -
Multiple_2pow61m1(hash[a], base_pow[b - a]));
}
return (modulus + hash[b] - (hash[a] * base_pow[b - a]) % modulus) %
modulus;
}
};
struct TrieTree {
private:
char chara_origin;
int numtypes_ch;
struct TrieNode {
public:
TrieNode **children;
bool isLeaf;
};
TrieNode *newNode() {
TrieNode *pNode = new TrieNode;
pNode->children = new TrieNode *[numtypes_ch];
pNode->isLeaf = false;
for (int i = 0; i < numtypes_ch; i++) {
pNode->children[i] = nullptr;
}
return pNode;
}
TrieNode *treeroot;
public:
TrieTree(int number_of_types_of_character = 26, char character_origin = 'a') {
numtypes_ch = number_of_types_of_character;
chara_origin = character_origin;
treeroot = newNode();
}
void insert(string key) {
TrieNode *pCrawl = treeroot;
for (int i = 0; i < ((int)key.size()); i++) {
int index = key[i] - chara_origin;
if (pCrawl->children[index] == nullptr) {
pCrawl->children[index] = newNode();
}
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
pCrawl->isLeaf = true;
}
bool search(string key) {
TrieNode *pCrawl = treeroot;
for (int i = 0; i < ((int)key.size()); i++) {
int index = key[i] - chara_origin;
if (pCrawl->children[index] == nullptr) {
return false;
}
pCrawl = pCrawl->children[index];
}
return (pCrawl != nullptr and pCrawl->isLeaf);
}
};
/*------------------ the end of the template -----------------------*/
// global variables
lli dp[3000][3000][4];
int vals[3000][3000];
int H;
int W;
int K;
vector<int> r, c, v;
lli expected;
void give_inputs(int state_debug) {
if (state_debug == 0) {
// standard I/O
cin.tie(0);
ios::sync_with_stdio(false); /* making cin faster */
SCAND3(H, W, K);
r.resize(K);
c.resize(K);
v.resize(K);
REP(i, K) {
SCAND3(r[i], c[i], v[i]);
r[i]--;
c[i]--;
}
} else if (state_debug == 1) {
// testcase 1
H = 2;
W = 2;
K = 3;
r = {0, 1, 0};
c = {0, 0, 1};
v = {3, 4, 5};
expected = 8;
} else if (state_debug == 2) {
// testcase 2
H = 2;
W = 5;
K = 5;
r = {0, 1, 0, 0, 0};
c = {0, 3, 1, 2, 3};
v = {3, 20, 1, 4, 2};
expected = 29;
} else if (state_debug == 3) {
// testcase 3
H = 4;
W = 5;
K = 10;
r = {1, 0, 1, 0, 0, 1, 2, 3, 2, 0};
c = {4, 4, 2, 1, 0, 3, 1, 4, 4, 2};
v = {12, 12, 15, 20, 28, 26, 27, 21, 10, 10};
expected = 142;
}
}
lli solve() // ここに処理を書く。型はansに合わせる。
{
REP(i, 3000) {
REP(j, 3000) {
REP(k, 4) { dp[i][j][k] = 0; }
vals[i][j] = 0;
}
}
REP(i, K) { vals[r[i]][c[i]] = v[i]; }
REP(s, H + W - 1) {
REP(i, H) {
int j = s - i;
if (not(0 <= j and j < W)) {
continue;
}
REP(k, 4) {
if (0 < j) {
dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
}
if (0 < i) {
dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
}
}
REP_R(k, 4) {
if (0 < k) {
dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + vals[i][j]);
}
}
}
}
lli ans = 0;
REP(k, 4) { ans = max(ans, dp[H - 1][W - 1][k]); }
return ans;
}
signed main(signed argc, char *argv[]) {
if (argc == 1) {
give_inputs(0);
} else {
give_inputs(stoi(string(argv[1])));
}
auto ans = solve();
/*
print here!!!
*/
PRINTLLD(ans);
// confirm expected == ans
if (argc != 1 and 0 < stoi(string(argv[1])) and expected != ans) {
puts("[WA]");
puts("expected: ");
print_all(expected);
puts("found: ");
print_all(ans);
}
}
| insert | 870 | 870 | 870 | 873 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define ll long long
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;
}
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
ll t1, t2, t3;
const ll mod = 998244353;
const int INF = 1e9;
const ll INFLONG = 1e18;
int main() {
ll r, c, k;
cin >> r >> c >> k;
vector<vector<ll>> arr(r, vector<ll>(c));
vector<vector<vector<ll>>> dp(r + 1,
vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
rep(i, k) {
cin >> t1 >> t2 >> t3;
t1--, t2--;
arr[t1][t2] = t3;
}
rep(i, r) {
rep(j, c) {
rep(p, 4) {
chmax(dp[i + 1][j + 1][0], dp[i][j + 1][p]);
chmax(dp[i + 1][j + 1][p], dp[i + 1][j][p]);
if (p < 3) {
chmax(dp[i + 1][j + 1][p + 1], dp[i + 1][j][p] + arr[i][j]);
}
chmax(dp[i + 1][j + 1][1], dp[i][j + 1][p] + arr[i][j]);
}
}
}
ll ans = 0;
rep(p, 4) { chmax(ans, dp[r][c][p]); }
rep(i, 4) { cout << dp[1][4][i] << endl; }
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define ll long long
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;
}
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
ll t1, t2, t3;
const ll mod = 998244353;
const int INF = 1e9;
const ll INFLONG = 1e18;
int main() {
ll r, c, k;
cin >> r >> c >> k;
vector<vector<ll>> arr(r, vector<ll>(c));
vector<vector<vector<ll>>> dp(r + 1,
vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
rep(i, k) {
cin >> t1 >> t2 >> t3;
t1--, t2--;
arr[t1][t2] = t3;
}
rep(i, r) {
rep(j, c) {
rep(p, 4) {
chmax(dp[i + 1][j + 1][0], dp[i][j + 1][p]);
chmax(dp[i + 1][j + 1][p], dp[i + 1][j][p]);
if (p < 3) {
chmax(dp[i + 1][j + 1][p + 1], dp[i + 1][j][p] + arr[i][j]);
}
chmax(dp[i + 1][j + 1][1], dp[i][j + 1][p] + arr[i][j]);
}
}
}
ll ans = 0;
rep(p, 4) { chmax(ans, dp[r][c][p]); }
cout << ans << endl;
} | delete | 55 | 56 | 55 | 55 | -11 | |
p02586 | C++ | Time Limit Exceeded | #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
constexpr long long INF = 1LL << 60;
constexpr long long MOD = 1000000007;
double PI = acos(-1.0);
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> tpl;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template <class S, class T> inline bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T> inline bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3>
istream &operator>>(istream &is, tuple<T1, T2, T3> &t) {
is >> get<0>(t) >> get<1>(t) >> get<2>(t);
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
int ct = 0;
for (auto &s : st)
cout << s << (++ct != st.size() ? " " : "");
return os;
}
template <typename T>
constexpr set<T> &operator|=(set<T> &st1, const set<T> &st2) {
for (auto &s : st2)
st1.insert(s);
return st1;
}
template <typename T>
constexpr set<T> &operator-=(set<T> &st1, const set<T> &st2) {
for (auto &s : st2)
if (st1.count(s))
st1.erase(s);
return st1;
}
template <typename T>
constexpr set<T> &operator&=(set<T> &st1, const set<T> &st2) {
auto itr = st1.begin();
while (itr != st1.end()) {
if (!st2.count(*itr))
itr = st1.erase(itr);
else
++itr;
}
return st1;
}
template <typename T>
constexpr set<T> operator|(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res |= st2;
return res;
}
template <typename T>
constexpr set<T> operator-(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res -= st2;
return res;
}
template <typename T>
constexpr set<T> operator&(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res &= st2;
return res;
}
/*--------------------------------- Tools
* ------------------------------------------*/
template <typename T> vector<T> cumsum(const vector<T> &X) {
vector<T> res(X.size() + 1, 0);
for (int i = 0; i < X.size(); ++i)
res[i + 1] += res[i] + X[i];
return res;
}
template <typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
while (abs(right - left) > 1) {
T mid = (right + left) / 2;
if (f(mid))
right = mid;
else
left = mid;
}
return {left, right};
}
template <typename S, typename T, typename F>
double trisearch(S left, T right, F f, int maxLoop = 90) {
double low = left, high = right;
while (maxLoop--) {
double mid_left = high / 3 + low * 2 / 3;
double mid_right = high * 2 / 3 + low / 3;
if (f(mid_left) >= f(mid_right))
low = mid_left;
else
high = mid_right;
}
return (low + high) * 0.5;
}
template <typename F> ll ternarySearch(ll L, ll R, F f) { //[L, R)
ll lo = L - 1, hi = R - 1;
while (lo + 1 != hi) {
ll mi = (lo + hi) / 2;
if (f(mi) <= f(mi + 1))
hi = mi;
else
lo = mi;
}
return hi;
}
/*--------------------------------- Graph
* ------------------------------------------*/
struct Graph {
struct Edge {
ll from, to, weight;
Edge() : from(0), to(0), weight(0) {}
Edge(ll f, ll t, ll w) : from(f), to(t), weight(w) {}
};
using Edges = vector<Edge>;
vector<Edges> G;
Graph() : G(){};
Graph(int N) : G(N) {}
Edges operator[](int k) const { return G[k]; }
ll size() const { return G.size(); }
void resize(int N) { G.resize(N); }
void add_edge(int a, int b, ll w = 1) {
G[a].emplace_back(a, b, w);
G[b].emplace_back(b, a, w);
}
void add_arrow(int a, int b, ll w = 1) { G[a].emplace_back(a, b, w); }
// Topological_sort
//!!return empty, if not DAG
vector<ll> topological_sort() const;
// Dijkstra and related
vector<ll> dijkstra(ll s, bool restore = false) const;
vector<ll> shortest_path(ll start, ll goal) const;
// Bellman-Ford
//!!return empty, if negative loop exists
vector<ll> bellman_ford(ll s) const;
// Warshall-Floyd
vector<vector<ll>> Warshall_Floyd() const;
// Kruskal
//!!Required UnionFind
Graph Kruskal() const;
};
vector<ll> Graph::dijkstra(ll s, bool restore) const {
vector<ll> dist(G.size(), INF);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> que;
dist[s] = 0;
que.emplace(dist[s], s);
vector<ll> prev(G.size(), -1);
while (!que.empty()) {
ll cost, idx;
tie(cost, idx) = que.top();
que.pop();
if (dist[idx] < cost)
continue;
for (auto &e : G[idx]) {
auto next_cost = cost + e.weight;
if (dist[e.to] <= next_cost)
continue;
dist[e.to] = next_cost;
if (restore)
prev[e.to] = e.from;
que.emplace(dist[e.to], e.to);
}
}
if (restore)
return prev;
return dist;
}
vector<ll> Graph::shortest_path(ll start, ll goal) const {
vector<ll> prev = dijkstra(start, true);
vector<ll> path;
for (int cur = goal; cur != -1; cur = prev[cur])
path.push_back(cur);
reverse(path.begin(), path.end());
if (path.front() != start)
return {};
return path;
}
vector<ll> Graph::bellman_ford(ll s) const {
vector<ll> dist(G.size(), INF);
dist[s] = 0;
for (ll i = 0; i < G.size(); ++i) {
for (ll j = 0; j < G.size(); ++j) {
for (auto &e : G[j]) {
if (dist[e.from] == INF)
continue;
bool res = chmin(dist[e.to], dist[e.from] + e.weight);
if (i == G.size() - 1 and res)
return {};
}
}
}
return dist;
}
vector<vector<ll>> Graph::Warshall_Floyd() const {
int N = G.size();
vector<vector<ll>> d(N, vector<ll>(N));
rep(i, N) rep(j, N) {
if (i == j)
d[i][j] = 0;
else
d[i][j] = INF;
}
rep(i, N) for (auto &e : G[i]) d[i][e.to] = e.weight;
rep(k, N) rep(i, N) rep(j, N) {
if (d[i][k] == INF or d[k][j] == INF)
continue;
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
return d;
}
vector<ll> Graph::topological_sort() const {
vector<ll> ans;
int N = G.size();
vector<int> ind(N);
rep(i, N) for (auto &e : G[i]) ind[e.to]++;
queue<int> que;
rep(i, N) if (!ind[i]) que.push(i);
while (!que.empty()) {
int now = que.front();
ans.pb(now);
que.pop();
for (auto &e : G[now]) {
ind[e.to]--;
if (!ind[e.to])
que.push(e.to);
}
}
if (ans.size() != N)
return {};
return ans;
}
/*------------------------------- Main Code Here
* -----------------------------------------*/
int main() {
ll R, C, K;
cin >> R >> C >> K;
vec2 mx(R + 1, vec(C + 1));
rep(i, K) {
ll r, c, v;
cin >> r >> c >> v;
mx[r][c] = v;
}
vec2 res(R + 1, vec(C + 1, -1));
auto f = [&](auto &self, ll x, ll y, vec tp) -> ll {
if (x > R or y > C)
return 0;
// if(res[x][y] != -1) return res[x][y];
sort(ALL(tp));
if (mx[x][y] > tp[0])
tp[0] = mx[x][y];
ll sum = accumulate(ALL(tp), 0LL);
return res[x][y] = max(self(self, x + 1, y, {0, 0, 0}) + sum,
self(self, x, y + 1, tp));
};
print(f(f, 0, 0, {0, 0, 0}));
return 0;
} | #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
constexpr long long INF = 1LL << 60;
constexpr long long MOD = 1000000007;
double PI = acos(-1.0);
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> tpl;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template <class S, class T> inline bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T> inline bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3>
istream &operator>>(istream &is, tuple<T1, T2, T3> &t) {
is >> get<0>(t) >> get<1>(t) >> get<2>(t);
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
int ct = 0;
for (auto &s : st)
cout << s << (++ct != st.size() ? " " : "");
return os;
}
template <typename T>
constexpr set<T> &operator|=(set<T> &st1, const set<T> &st2) {
for (auto &s : st2)
st1.insert(s);
return st1;
}
template <typename T>
constexpr set<T> &operator-=(set<T> &st1, const set<T> &st2) {
for (auto &s : st2)
if (st1.count(s))
st1.erase(s);
return st1;
}
template <typename T>
constexpr set<T> &operator&=(set<T> &st1, const set<T> &st2) {
auto itr = st1.begin();
while (itr != st1.end()) {
if (!st2.count(*itr))
itr = st1.erase(itr);
else
++itr;
}
return st1;
}
template <typename T>
constexpr set<T> operator|(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res |= st2;
return res;
}
template <typename T>
constexpr set<T> operator-(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res -= st2;
return res;
}
template <typename T>
constexpr set<T> operator&(const set<T> &st1, const set<T> &st2) {
set<T> res = st1;
res &= st2;
return res;
}
/*--------------------------------- Tools
* ------------------------------------------*/
template <typename T> vector<T> cumsum(const vector<T> &X) {
vector<T> res(X.size() + 1, 0);
for (int i = 0; i < X.size(); ++i)
res[i + 1] += res[i] + X[i];
return res;
}
template <typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
while (abs(right - left) > 1) {
T mid = (right + left) / 2;
if (f(mid))
right = mid;
else
left = mid;
}
return {left, right};
}
template <typename S, typename T, typename F>
double trisearch(S left, T right, F f, int maxLoop = 90) {
double low = left, high = right;
while (maxLoop--) {
double mid_left = high / 3 + low * 2 / 3;
double mid_right = high * 2 / 3 + low / 3;
if (f(mid_left) >= f(mid_right))
low = mid_left;
else
high = mid_right;
}
return (low + high) * 0.5;
}
template <typename F> ll ternarySearch(ll L, ll R, F f) { //[L, R)
ll lo = L - 1, hi = R - 1;
while (lo + 1 != hi) {
ll mi = (lo + hi) / 2;
if (f(mi) <= f(mi + 1))
hi = mi;
else
lo = mi;
}
return hi;
}
/*--------------------------------- Graph
* ------------------------------------------*/
struct Graph {
struct Edge {
ll from, to, weight;
Edge() : from(0), to(0), weight(0) {}
Edge(ll f, ll t, ll w) : from(f), to(t), weight(w) {}
};
using Edges = vector<Edge>;
vector<Edges> G;
Graph() : G(){};
Graph(int N) : G(N) {}
Edges operator[](int k) const { return G[k]; }
ll size() const { return G.size(); }
void resize(int N) { G.resize(N); }
void add_edge(int a, int b, ll w = 1) {
G[a].emplace_back(a, b, w);
G[b].emplace_back(b, a, w);
}
void add_arrow(int a, int b, ll w = 1) { G[a].emplace_back(a, b, w); }
// Topological_sort
//!!return empty, if not DAG
vector<ll> topological_sort() const;
// Dijkstra and related
vector<ll> dijkstra(ll s, bool restore = false) const;
vector<ll> shortest_path(ll start, ll goal) const;
// Bellman-Ford
//!!return empty, if negative loop exists
vector<ll> bellman_ford(ll s) const;
// Warshall-Floyd
vector<vector<ll>> Warshall_Floyd() const;
// Kruskal
//!!Required UnionFind
Graph Kruskal() const;
};
vector<ll> Graph::dijkstra(ll s, bool restore) const {
vector<ll> dist(G.size(), INF);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> que;
dist[s] = 0;
que.emplace(dist[s], s);
vector<ll> prev(G.size(), -1);
while (!que.empty()) {
ll cost, idx;
tie(cost, idx) = que.top();
que.pop();
if (dist[idx] < cost)
continue;
for (auto &e : G[idx]) {
auto next_cost = cost + e.weight;
if (dist[e.to] <= next_cost)
continue;
dist[e.to] = next_cost;
if (restore)
prev[e.to] = e.from;
que.emplace(dist[e.to], e.to);
}
}
if (restore)
return prev;
return dist;
}
vector<ll> Graph::shortest_path(ll start, ll goal) const {
vector<ll> prev = dijkstra(start, true);
vector<ll> path;
for (int cur = goal; cur != -1; cur = prev[cur])
path.push_back(cur);
reverse(path.begin(), path.end());
if (path.front() != start)
return {};
return path;
}
vector<ll> Graph::bellman_ford(ll s) const {
vector<ll> dist(G.size(), INF);
dist[s] = 0;
for (ll i = 0; i < G.size(); ++i) {
for (ll j = 0; j < G.size(); ++j) {
for (auto &e : G[j]) {
if (dist[e.from] == INF)
continue;
bool res = chmin(dist[e.to], dist[e.from] + e.weight);
if (i == G.size() - 1 and res)
return {};
}
}
}
return dist;
}
vector<vector<ll>> Graph::Warshall_Floyd() const {
int N = G.size();
vector<vector<ll>> d(N, vector<ll>(N));
rep(i, N) rep(j, N) {
if (i == j)
d[i][j] = 0;
else
d[i][j] = INF;
}
rep(i, N) for (auto &e : G[i]) d[i][e.to] = e.weight;
rep(k, N) rep(i, N) rep(j, N) {
if (d[i][k] == INF or d[k][j] == INF)
continue;
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
return d;
}
vector<ll> Graph::topological_sort() const {
vector<ll> ans;
int N = G.size();
vector<int> ind(N);
rep(i, N) for (auto &e : G[i]) ind[e.to]++;
queue<int> que;
rep(i, N) if (!ind[i]) que.push(i);
while (!que.empty()) {
int now = que.front();
ans.pb(now);
que.pop();
for (auto &e : G[now]) {
ind[e.to]--;
if (!ind[e.to])
que.push(e.to);
}
}
if (ans.size() != N)
return {};
return ans;
}
/*------------------------------- Main Code Here
* -----------------------------------------*/
int main() {
ll R, C, K;
cin >> R >> C >> K;
vec2 mx(R + 1, vec(C + 1));
rep(i, K) {
ll r, c, v;
cin >> r >> c >> v;
mx[r][c] = v;
}
vec3 res(4, vec2(R + 1, vec(C + 1, 0)));
rep1(j, R) rep1(k, C) {
// 変化なし
rep(i, 4) chmax(res[0][j][k], res[i][j - 1][k]);
rep(i, 4) chmax(res[i][j][k], res[i][j][k - 1]);
// 取る場合
rrep(i, 4) if (i) chmax(res[i][j][k], res[i - 1][j][k] + mx[j][k]);
}
// for(auto r : res[3]) print(r);
ll ans = 0;
rep(i, 4) chmax(ans, res[i][R][C]);
print(ans);
return 0;
}
| replace | 361 | 377 | 361 | 377 | TLE | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define INF (int)1e9 + 4
#define INFL (ll)1e18 + 4
#define md (int)1e9 + 7
#define mx 10000000
#define mk make_pair
#define pi pair<int, int>
#define ss second
#define ff first
#define pb emplace_back
#define onell(x) __builtin_popcountll(x)
#define one(x) __builtin_popcount(x)
#define all(s) s.begin(), s.end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(x, a) for (auto &x : a)
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i > b; i--)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define sz(x) ((long long)x.size())
#define mez(s) memset(s, 0, sizeof s)
#define mex(s) memset(s, -1, sizeof s)
#define metr(s) memset(s, true, sizeof s)
typedef vector<int> vi;
typedef vector<ll> vl;
typedef map<int, int> mp;
typedef queue<int> que;
typedef vector<pi> vp;
int n, m, k;
const int mxn = 350;
int gh[mxn][mxn];
ll dp[mxn][mxn][4];
ll dfs(int i, int j, int k) {
if (i > n)
return 0;
if (j > m)
return 0;
ll ans = 0;
if (dp[i][j][k] != -1)
return dp[i][j][k];
if (k > 0) {
ans = max(ans, gh[i][j] + dfs(i, j + 1, k - 1));
ans = max(ans, gh[i][j] + dfs(i + 1, j, 3));
}
ans = max(ans, dfs(i + 1, j, 3));
ans = max(ans, dfs(i, j + 1, k));
return dp[i][j][k] = ans;
}
void solve() {
int x = 0, y = 0, z = 0;
cin >> n >> m >> k;
mex(dp), mez(gh);
while (k--) {
cin >> x >> y >> z;
gh[x][y] = z;
}
ll ans = dfs(1, 1, 3);
cout << ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define INF (int)1e9 + 4
#define INFL (ll)1e18 + 4
#define md (int)1e9 + 7
#define mx 10000000
#define mk make_pair
#define pi pair<int, int>
#define ss second
#define ff first
#define pb emplace_back
#define onell(x) __builtin_popcountll(x)
#define one(x) __builtin_popcount(x)
#define all(s) s.begin(), s.end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(x, a) for (auto &x : a)
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i > b; i--)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define sz(x) ((long long)x.size())
#define mez(s) memset(s, 0, sizeof s)
#define mex(s) memset(s, -1, sizeof s)
#define metr(s) memset(s, true, sizeof s)
typedef vector<int> vi;
typedef vector<ll> vl;
typedef map<int, int> mp;
typedef queue<int> que;
typedef vector<pi> vp;
int n, m, k;
const int mxn = 3010;
int gh[mxn][mxn];
ll dp[mxn][mxn][4];
ll dfs(int i, int j, int k) {
if (i > n)
return 0;
if (j > m)
return 0;
ll ans = 0;
if (dp[i][j][k] != -1)
return dp[i][j][k];
if (k > 0) {
ans = max(ans, gh[i][j] + dfs(i, j + 1, k - 1));
ans = max(ans, gh[i][j] + dfs(i + 1, j, 3));
}
ans = max(ans, dfs(i + 1, j, 3));
ans = max(ans, dfs(i, j + 1, k));
return dp[i][j][k] = ans;
}
void solve() {
int x = 0, y = 0, z = 0;
cin >> n >> m >> k;
mex(dp), mez(gh);
while (k--) {
cin >> x >> y >> z;
gh[x][y] = z;
}
ll ans = dfs(1, 1, 3);
cout << ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | replace | 32 | 33 | 32 | 33 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FO(x) \
{ \
freopen(#x ".in", "r", stdin); \
freopen(#x ".out", "w", stdout); \
}
#define pa pair<int, int>
#define mod 998244353
#define ll long long
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define cl(x) memset(x, 0, sizeof x)
#ifdef Devil_Gary
#define bug(x) cout << (#x) << " " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bug(x)
#define debug(...)
#endif
const int INF = 0x7fffffff;
const int N = 5e3 + 5;
/*
char *TT,*mo,but[(1<<15)+2];
#define getchar()
((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/
inline int read() {
int x = 0, rev = 0, ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
rev = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return rev ? -x : x;
} /*
struct Edge{
int nxt,v;
// int w;
// int id;
}e[N<<1];
void add(int u,int v){
e[++tot].v=v,e[tot].nxt=head[u],head[u]=tot;
e[++tot].v=u,e[tot].nxt=head[v],head[v]=tot;
}*/
ll f[N][N][3];
int n, m, k;
ll c[N][N];
int main() {
#ifdef Devil_Gary
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
n = read(), m = read(), k = read();
for (int i = 1; i <= k; i++) {
ll x, y, z;
cin >> x >> y >> z;
c[x][y] = z;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 0; k <= 3; k++) {
if (k != 0)
f[i][j][k] = c[i][j] + max(f[i][j - 1][k - 1],
max(max(f[i - 1][j][0], f[i - 1][j][1]),
max(f[i - 1][j][2], f[i - 1][j][3])));
f[i][j][k] =
max(f[i][j][k],
max(f[i][j - 1][k], max(max(f[i - 1][j][0], f[i - 1][j][1]),
max(f[i - 1][j][2], f[i - 1][j][3]))));
}
}
}
cout << max(max(f[n][m][0], f[n][m][1]), max(f[n][m][2], f[n][m][3]));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define FO(x) \
{ \
freopen(#x ".in", "r", stdin); \
freopen(#x ".out", "w", stdout); \
}
#define pa pair<int, int>
#define mod 998244353
#define ll long long
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define cl(x) memset(x, 0, sizeof x)
#ifdef Devil_Gary
#define bug(x) cout << (#x) << " " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bug(x)
#define debug(...)
#endif
const int INF = 0x7fffffff;
const int N = 5e3 + 5;
/*
char *TT,*mo,but[(1<<15)+2];
#define getchar()
((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/
inline int read() {
int x = 0, rev = 0, ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
rev = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return rev ? -x : x;
} /*
struct Edge{
int nxt,v;
// int w;
// int id;
}e[N<<1];
void add(int u,int v){
e[++tot].v=v,e[tot].nxt=head[u],head[u]=tot;
e[++tot].v=u,e[tot].nxt=head[v],head[v]=tot;
}*/
ll f[N][N][4];
int n, m, k;
ll c[N][N];
int main() {
#ifdef Devil_Gary
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
n = read(), m = read(), k = read();
for (int i = 1; i <= k; i++) {
ll x, y, z;
cin >> x >> y >> z;
c[x][y] = z;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 0; k <= 3; k++) {
if (k != 0)
f[i][j][k] = c[i][j] + max(f[i][j - 1][k - 1],
max(max(f[i - 1][j][0], f[i - 1][j][1]),
max(f[i - 1][j][2], f[i - 1][j][3])));
f[i][j][k] =
max(f[i][j][k],
max(f[i][j - 1][k], max(max(f[i - 1][j][0], f[i - 1][j][1]),
max(f[i - 1][j][2], f[i - 1][j][3]))));
}
}
}
cout << max(max(f[n][m][0], f[n][m][1]), max(f[n][m][2], f[n][m][3]));
return 0;
}
| replace | 51 | 52 | 51 | 52 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[ ";
for (auto v : V)
os << v << " ";
return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
os << "{ ";
for (auto s : S)
os << s << " ";
return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
os << "{ ";
for (auto m : M)
os << "(" << m.first << ":" << m.second << ")";
return os << "}";
}
#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update> find_by_order(k) returns
// iterator to kth element starting from 0; order_of_key(k) returns count of
// elements strictly smaller than k;
// For multiset use less_equal operator but it does support erase operations for
// multiset
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define all(v) (v).begin(), (v).end()
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpii;
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define mp make_pair
const int mod = 1e9 + 7;
inline int add(int a, int b) {
a += b;
if (a >= mod)
a -= mod;
return a;
}
inline int sub(int a, int b) {
a -= b;
if (a < 0)
a += mod;
return a;
}
inline int mul(int a, int b) { return a * 1ll * b % mod; }
inline int power(int x, ll y) {
int res = 1;
while (y) {
if (y & 1)
res = mul(res, x);
x = mul(x, x);
y >>= 1;
}
return res;
}
int r, c, k;
const int N = 1e3 + 100;
ll val[N][N];
ll dp[4][N][N];
int main() {
fio;
cout << fixed << setprecision(25);
cin >> r >> c >> k;
int r1, c1, v1;
for (int i = 1; i <= k; ++i) {
cin >> r1 >> c1 >> v1;
val[r1][c1] = v1;
}
for (int i = 1; i <= r; ++i) {
for (int j = 1; j <= c; ++j) {
for (int k = 0; k < 4; ++k) {
dp[0][i][j] = max(dp[0][i][j], dp[k][i - 1][j]);
dp[1][i][j] = max(dp[1][i][j], val[i][j] + dp[k][i - 1][j]);
if (k) {
dp[k][i][j] = max(dp[k][i][j], val[i][j] + dp[k - 1][i][j - 1]);
dp[k][i][j] = max(dp[k][i][j], dp[k][i][j - 1]);
}
}
}
}
ll mx = 0;
for (int i = 0; i < 4; ++i)
mx = max(mx, dp[i][r][c]);
cout << mx << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[ ";
for (auto v : V)
os << v << " ";
return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
os << "{ ";
for (auto s : S)
os << s << " ";
return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
os << "{ ";
for (auto m : M)
os << "(" << m.first << ":" << m.second << ")";
return os << "}";
}
#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update> find_by_order(k) returns
// iterator to kth element starting from 0; order_of_key(k) returns count of
// elements strictly smaller than k;
// For multiset use less_equal operator but it does support erase operations for
// multiset
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define all(v) (v).begin(), (v).end()
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpii;
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define mp make_pair
const int mod = 1e9 + 7;
inline int add(int a, int b) {
a += b;
if (a >= mod)
a -= mod;
return a;
}
inline int sub(int a, int b) {
a -= b;
if (a < 0)
a += mod;
return a;
}
inline int mul(int a, int b) { return a * 1ll * b % mod; }
inline int power(int x, ll y) {
int res = 1;
while (y) {
if (y & 1)
res = mul(res, x);
x = mul(x, x);
y >>= 1;
}
return res;
}
int r, c, k;
const int N = 3e3 + 100;
ll val[N][N];
ll dp[4][N][N];
int main() {
fio;
cout << fixed << setprecision(25);
cin >> r >> c >> k;
int r1, c1, v1;
for (int i = 1; i <= k; ++i) {
cin >> r1 >> c1 >> v1;
val[r1][c1] = v1;
}
for (int i = 1; i <= r; ++i) {
for (int j = 1; j <= c; ++j) {
for (int k = 0; k < 4; ++k) {
dp[0][i][j] = max(dp[0][i][j], dp[k][i - 1][j]);
dp[1][i][j] = max(dp[1][i][j], val[i][j] + dp[k][i - 1][j]);
if (k) {
dp[k][i][j] = max(dp[k][i][j], val[i][j] + dp[k - 1][i][j - 1]);
dp[k][i][j] = max(dp[k][i][j], dp[k][i][j - 1]);
}
}
}
}
ll mx = 0;
for (int i = 0; i < 4; ++i)
mx = max(mx, dp[i][r][c]);
cout << mx << endl;
return 0;
}
| replace | 95 | 96 | 95 | 96 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int R, C, K;
long long memo[3005][3005][10];
int grid[3005][3005];
bool exists(int r, int c) { return (1 <= r && r <= R) && (1 <= c && c <= C); }
long long dp(int r, int c, int rem) {
if (memo[r][c][rem] != -1)
return memo[r][c][rem];
memo[r][c][rem] = 0;
if (r == R && c == C) {
if (rem > 0 && grid[r][c] != 0)
memo[r][c][rem] = grid[r][c];
else
memo[r][c][rem] = 0;
return memo[r][c][rem];
}
else {
if (exists(r + 1, c)) {
if (rem > 0 && grid[r][c] != 0)
memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3) + grid[r][c]);
else
memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3));
}
if (exists(r, c + 1)) {
memo[r][c][rem] = max(memo[r][c][rem], dp(r, c + 1, rem));
if (rem > 0 && grid[r][c] != 0)
memo[r][c][rem] =
max(memo[r][c][rem], dp(r, c + 1, rem - 1) + grid[r][c]);
}
return memo[r][c][rem];
}
}
int main() {
scanf("%d%d%d", &R, &C, &K);
for (int i = 1; i <= K; i++) {
int ri, ci, vi;
scanf("%d%d%d", &ri, &ci, &vi);
grid[ri][ci] = vi;
}
for (int i = 1; i <= K; i++) {
for (int j = 1; j <= C; j++) {
for (int k = 0; k <= 3; k++)
memo[i][j][k] = -1;
}
}
printf("%lld", dp(1, 1, 3));
} | #include <bits/stdc++.h>
using namespace std;
int R, C, K;
long long memo[3005][3005][10];
int grid[3005][3005];
bool exists(int r, int c) { return (1 <= r && r <= R) && (1 <= c && c <= C); }
long long dp(int r, int c, int rem) {
if (memo[r][c][rem] != -1)
return memo[r][c][rem];
memo[r][c][rem] = 0;
if (r == R && c == C) {
if (rem > 0 && grid[r][c] != 0)
memo[r][c][rem] = grid[r][c];
else
memo[r][c][rem] = 0;
return memo[r][c][rem];
}
else {
if (exists(r + 1, c)) {
if (rem > 0 && grid[r][c] != 0)
memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3) + grid[r][c]);
else
memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3));
}
if (exists(r, c + 1)) {
memo[r][c][rem] = max(memo[r][c][rem], dp(r, c + 1, rem));
if (rem > 0 && grid[r][c] != 0)
memo[r][c][rem] =
max(memo[r][c][rem], dp(r, c + 1, rem - 1) + grid[r][c]);
}
return memo[r][c][rem];
}
}
int main() {
scanf("%d%d%d", &R, &C, &K);
for (int i = 1; i <= K; i++) {
int ri, ci, vi;
scanf("%d%d%d", &ri, &ci, &vi);
grid[ri][ci] = vi;
}
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
for (int k = 0; k <= 3; k++)
memo[i][j][k] = -1;
}
}
printf("%lld", dp(1, 1, 3));
}
| replace | 53 | 54 | 53 | 54 | -11 | |
p02586 | C++ | Runtime Error |
// #pragma GCC optimize ("-O3")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <type_traits>
#include <typeinfo>
#include <unordered_map>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define popcnt __popcnt64
// # define __builtin_popcount __popcnt
#else
#define popcnt __builtin_popcountll
#endif
// #include "boost/variant.hpp"
using namespace std;
typedef long long ll;
constexpr ll MOD = 1000000007ll;
constexpr ll INF = 1LL << 60;
#define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i)
#define rep_skip(i, N, M, ...) \
for (ll i = N, i##_len = (M); i < i##_len; i += (skip))
#define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i)
#define repbit(bit, N, DIG) rep(bit, (N), (1LL << (DIG)))
#define pb push_back
#define fir first
#define sec second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define perm(c) \
sort(all(c)); \
for (bool c##perm = 1; c##perm; \
c##perm = next_permutation( \
all(c))) // perm(c){write(c)} writes all permutation of c
constexpr ll dceil(ll x, ll y) {
if (y < 0) {
x *= -1;
y *= -1;
};
return x > 0 ? (x + y - 1) / y : x / y;
} // ceil for x/y
constexpr ll dfloor(ll x, ll y) {
if (y < 0) {
x *= -1;
y *= -1;
};
return x > 0 ? x / y : -dceil((-x), y);
} // floor for x/y
typedef pair<double, double> pd;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
template <typename T>
using pq_greater = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using vpt = vector<complex<T>>;
template <int n> struct tll_impl {
using type = decltype(tuple_cat(tuple<ll>(),
declval<typename tll_impl<n - 1>::type>()));
};
template <> struct tll_impl<1> {
using type = tuple<ll>;
};
template <int n> using tll = typename tll_impl<n>::type;
template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); };
template <int n, typename T> struct vec_t_impl {
using type = vector<typename vec_t_impl<n - 1, T>::type>;
};
template <typename T> struct vec_t_impl<1, T> {
using type = vector<T>;
};
template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type;
// check
static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, "");
// decompose vector into basetype and dimension.
template <typename T> struct vec_dec {
static constexpr int dim = 0;
using type = T;
};
template <typename T> struct vec_dec<vector<T>> {
static constexpr int dim = vec_dec<T>::dim + 1;
using type = typename vec_dec<T>::type;
};
static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, "");
static_assert(vec_dec<vec_t<3, ll>>::dim == 3, "");
template <typename T = ll> vector<T> makev(size_t a) { return vector<T>(a); }
template <typename T = ll, typename... Ts> auto makev(size_t a, Ts... ts) {
return vector<decltype(makev<T>(ts...))>(a, makev<T>(ts...));
} // ex: auto dp = makev<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5));
template <typename T>
struct is_vector : std::false_type {}; // check if T is vector
template <typename T> struct is_vector<vector<T>> : std::true_type {};
static_assert(is_vector<vector<ll>>::value == true &&
is_vector<ll>::value == false,
"");
// check if T is vector
template <typename T> struct is_pair : std::false_type {};
template <typename T, typename S>
struct is_pair<pair<T, S>> : std::true_type {};
static_assert(is_pair<pll>::value == true && is_pair<ll>::value == false, "");
template <typename T, typename V,
typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr>
void fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V,
typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr>
void fill_v(T &t, const V &v) {
for (auto &&x : t)
fill_v(x, v);
} // ex: fill_v(dp, INF);
namespace std {
template <class T> bool operator<(const complex<T> &a, const complex<T> &b) {
return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real();
}
}; // namespace std
template <typename T, typename S>
istream &operator>>(istream &istr, pair<T, S> &x) {
return istr >> x.first >> x.second;
}
template <typename T> istream &operator>>(istream &istr, vector<T> &x) {
rep(i, 0, x.size()) istr >> x[i];
return istr;
}
template <typename T> istream &operator>>(istream &istr, complex<T> &x) {
T r, i;
istr >> r >> i;
x.real(r);
x.imag(i);
return istr;
}
template <typename T, typename Delim_t = string,
typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr>
void write(T &x, Delim_t delim = " ") {
cout << x << delim;
}
template <typename T, typename Delim_t = string,
typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr>
void write(T &x, Delim_t delim = " ") {
rep(i, 0, x.size()) write(x[i], (i == (x.size() - 1) ? "" : delim));
cout << '\n';
}
template <typename T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <typename T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
vll seq(ll i, ll j) {
vll res(j - i);
rep(k, i, j) res[k] = i + k;
return res;
}
constexpr ll POW_0(ll x, ll y) {
if (y == 0)
return 1;
if (y == 1)
return x;
if (y == 2)
return x * x;
if (y % 2 == 0)
return POW_0(POW_0(x, y / 2), 2LL);
return ((POW_0(POW_0(x, y / 2), 2LL)) * (x));
}
constexpr ll POW(ll x, ll y, ll mod = 0) {
if (mod == 0)
return POW_0(x, y);
if (y == 0)
return 1;
if (y == 1)
return x % mod;
if (y == 2)
return x * x % mod;
if (y % 2 == 0)
return POW(POW(x, y / 2, mod), 2LL, mod) % mod;
return ((POW(POW(x, y / 2, mod), 2LL, mod)) * (x % mod)) % mod;
}
template <typename Inputs, typename Functor,
typename T = typename Inputs::value_type>
void sort_by(Inputs &inputs, Functor f) {
std::sort(std::begin(inputs), std::end(inputs),
[&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); });
}
template <int pos, typename Inputs, typename T = typename Inputs::value_type>
void sort_by(Inputs &inputs) {
std::sort(
std::begin(inputs), std::end(inputs),
[](const T &lhs, const T &rhs) { return get<pos>(lhs) < get<pos>(rhs); });
}
template <typename Inputs, typename Functor,
typename T = typename Inputs::value_type>
void stable_sort_by(Inputs &inputs, Functor f) {
std::stable_sort(
std::begin(inputs), std::end(inputs),
[&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); });
}
template <typename Inputs> void sort_uniq(Inputs &inputs) {
sort(all(inputs));
inputs.erase(unique(all(inputs)), inputs.end());
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
stringstream ss(s);
string item;
if (s.size() > 0 && s.front() == delim) {
elems.push_back("");
}
while (getline(ss, item, delim)) {
if (!item.empty()) {
elems.push_back(item);
}
}
if (s.size() > 0 && s.back() == delim) {
elems.push_back("");
}
return elems;
}
template <class T> map<T, ll> inv_map(vector<T> &x) {
map<T, ll> res;
rep(i, 0, x.size()) { res[x[i]] = i; }
return res;
}
template <class K, class V> map<V, K> inv_map(map<K, V> &m) {
map<V, K> res;
for (const auto &x : m) {
res[x.second] = x.first;
}
return res;
}
template <class T, class val_t = typename T::value_type,
enable_if_t<!is_same<T, set<val_t>>::value> * = nullptr>
constexpr bool exist(const T &container, val_t val) {
return find(all(container), val) != container.end();
}
template <class T, class val_t = typename T::value_type,
enable_if_t<is_same<T, set<val_t>>::value> * = nullptr>
constexpr bool exist(const T &container, val_t val) {
return container.find(val) != container.end();
}
// inner prod: |a||b|cos(theta)
template <class T> T dot(complex<T> a, complex<T> b) {
return a.real() * b.real() + a.imag() * b.imag();
}
// outer prod |a||b|sin(theta)
template <class T> T cross(complex<T> a, complex<T> b) {
return a.real() * b.imag() - a.imag() * b.real();
}
struct D2 {
enum Dir { U, D, L, R };
static inline vector<Dir> Dirs = {U, D, L, R};
D2(ll h, ll w) : h(h), w(w){};
bool in(ll n, D2::Dir d, ll k = 1) {
if ((W(n) <= k - 1) && d == L)
return false;
if ((W(n) >= w - 1 - (k - 1)) && d == R)
return false;
if ((H(n) <= k - 1) && d == D)
return false;
if ((H(n) >= h - 1 - (k - 1)) && d == U)
return false;
return true;
};
ll next(Dir d, ll k = 1) {
switch (d) {
case U:
return w * k;
case D:
return -w * k;
case L:
return -k;
case R:
return k;
default:
throw invalid_argument("not direction");
}
}
ll H(ll n) { return n / w; }
ll W(ll n) { return n % w; }
ll h, w;
};
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
ll w, h, k;
cin >> w >> h >> k;
map<pll, ll> item;
rep(i, 0, k) {
ll r, c, v;
cin >> c >> r >> v;
item[{r - 1, c - 1}] = v;
}
auto dp = makev(h, w, 4);
auto in = [&](ll i, ll j) { return item.find({i, j}) != item.end(); };
if (in(h - 1, w - 1)) {
dp[h - 1][w - 1][1] = item[{h - 1, w - 1}];
}
rrep(i, 0, h) rrep(j, 0, w) {
if (i == h - 1 && j == w - 1)
continue;
if (in(i, j)) {
if (j < w - 1)
chmax(dp[i][j][1], item[{i, j}] + max({dp[i][j + 1][1], dp[i][j + 1][2],
dp[i][j + 1][3]}));
if (i < h - 1) {
rep(k, 0, 3) chmax(dp[i][j][k + 1], item[{i, j}] + dp[i + 1][j][k]);
}
chmax(dp[i][j][k], item[{i, j}]);
}
if (j < w - 1)
rep(k, 1, 4) chmax(dp[i][j][1], dp[i][j + 1][k]);
if (i < h - 1)
rep(k, 1, 4) chmax(dp[i][j][k], dp[i + 1][j][k]);
}
cout << *max_element(all(dp[0][0])) << endl;
return 0;
}
|
// #pragma GCC optimize ("-O3")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <type_traits>
#include <typeinfo>
#include <unordered_map>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define popcnt __popcnt64
// # define __builtin_popcount __popcnt
#else
#define popcnt __builtin_popcountll
#endif
// #include "boost/variant.hpp"
using namespace std;
typedef long long ll;
constexpr ll MOD = 1000000007ll;
constexpr ll INF = 1LL << 60;
#define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i)
#define rep_skip(i, N, M, ...) \
for (ll i = N, i##_len = (M); i < i##_len; i += (skip))
#define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i)
#define repbit(bit, N, DIG) rep(bit, (N), (1LL << (DIG)))
#define pb push_back
#define fir first
#define sec second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define perm(c) \
sort(all(c)); \
for (bool c##perm = 1; c##perm; \
c##perm = next_permutation( \
all(c))) // perm(c){write(c)} writes all permutation of c
constexpr ll dceil(ll x, ll y) {
if (y < 0) {
x *= -1;
y *= -1;
};
return x > 0 ? (x + y - 1) / y : x / y;
} // ceil for x/y
constexpr ll dfloor(ll x, ll y) {
if (y < 0) {
x *= -1;
y *= -1;
};
return x > 0 ? x / y : -dceil((-x), y);
} // floor for x/y
typedef pair<double, double> pd;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
template <typename T>
using pq_greater = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using vpt = vector<complex<T>>;
template <int n> struct tll_impl {
using type = decltype(tuple_cat(tuple<ll>(),
declval<typename tll_impl<n - 1>::type>()));
};
template <> struct tll_impl<1> {
using type = tuple<ll>;
};
template <int n> using tll = typename tll_impl<n>::type;
template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); };
template <int n, typename T> struct vec_t_impl {
using type = vector<typename vec_t_impl<n - 1, T>::type>;
};
template <typename T> struct vec_t_impl<1, T> {
using type = vector<T>;
};
template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type;
// check
static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, "");
// decompose vector into basetype and dimension.
template <typename T> struct vec_dec {
static constexpr int dim = 0;
using type = T;
};
template <typename T> struct vec_dec<vector<T>> {
static constexpr int dim = vec_dec<T>::dim + 1;
using type = typename vec_dec<T>::type;
};
static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, "");
static_assert(vec_dec<vec_t<3, ll>>::dim == 3, "");
template <typename T = ll> vector<T> makev(size_t a) { return vector<T>(a); }
template <typename T = ll, typename... Ts> auto makev(size_t a, Ts... ts) {
return vector<decltype(makev<T>(ts...))>(a, makev<T>(ts...));
} // ex: auto dp = makev<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5));
template <typename T>
struct is_vector : std::false_type {}; // check if T is vector
template <typename T> struct is_vector<vector<T>> : std::true_type {};
static_assert(is_vector<vector<ll>>::value == true &&
is_vector<ll>::value == false,
"");
// check if T is vector
template <typename T> struct is_pair : std::false_type {};
template <typename T, typename S>
struct is_pair<pair<T, S>> : std::true_type {};
static_assert(is_pair<pll>::value == true && is_pair<ll>::value == false, "");
template <typename T, typename V,
typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr>
void fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V,
typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr>
void fill_v(T &t, const V &v) {
for (auto &&x : t)
fill_v(x, v);
} // ex: fill_v(dp, INF);
namespace std {
template <class T> bool operator<(const complex<T> &a, const complex<T> &b) {
return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real();
}
}; // namespace std
template <typename T, typename S>
istream &operator>>(istream &istr, pair<T, S> &x) {
return istr >> x.first >> x.second;
}
template <typename T> istream &operator>>(istream &istr, vector<T> &x) {
rep(i, 0, x.size()) istr >> x[i];
return istr;
}
template <typename T> istream &operator>>(istream &istr, complex<T> &x) {
T r, i;
istr >> r >> i;
x.real(r);
x.imag(i);
return istr;
}
template <typename T, typename Delim_t = string,
typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr>
void write(T &x, Delim_t delim = " ") {
cout << x << delim;
}
template <typename T, typename Delim_t = string,
typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr>
void write(T &x, Delim_t delim = " ") {
rep(i, 0, x.size()) write(x[i], (i == (x.size() - 1) ? "" : delim));
cout << '\n';
}
template <typename T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <typename T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
vll seq(ll i, ll j) {
vll res(j - i);
rep(k, i, j) res[k] = i + k;
return res;
}
constexpr ll POW_0(ll x, ll y) {
if (y == 0)
return 1;
if (y == 1)
return x;
if (y == 2)
return x * x;
if (y % 2 == 0)
return POW_0(POW_0(x, y / 2), 2LL);
return ((POW_0(POW_0(x, y / 2), 2LL)) * (x));
}
constexpr ll POW(ll x, ll y, ll mod = 0) {
if (mod == 0)
return POW_0(x, y);
if (y == 0)
return 1;
if (y == 1)
return x % mod;
if (y == 2)
return x * x % mod;
if (y % 2 == 0)
return POW(POW(x, y / 2, mod), 2LL, mod) % mod;
return ((POW(POW(x, y / 2, mod), 2LL, mod)) * (x % mod)) % mod;
}
template <typename Inputs, typename Functor,
typename T = typename Inputs::value_type>
void sort_by(Inputs &inputs, Functor f) {
std::sort(std::begin(inputs), std::end(inputs),
[&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); });
}
template <int pos, typename Inputs, typename T = typename Inputs::value_type>
void sort_by(Inputs &inputs) {
std::sort(
std::begin(inputs), std::end(inputs),
[](const T &lhs, const T &rhs) { return get<pos>(lhs) < get<pos>(rhs); });
}
template <typename Inputs, typename Functor,
typename T = typename Inputs::value_type>
void stable_sort_by(Inputs &inputs, Functor f) {
std::stable_sort(
std::begin(inputs), std::end(inputs),
[&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); });
}
template <typename Inputs> void sort_uniq(Inputs &inputs) {
sort(all(inputs));
inputs.erase(unique(all(inputs)), inputs.end());
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
stringstream ss(s);
string item;
if (s.size() > 0 && s.front() == delim) {
elems.push_back("");
}
while (getline(ss, item, delim)) {
if (!item.empty()) {
elems.push_back(item);
}
}
if (s.size() > 0 && s.back() == delim) {
elems.push_back("");
}
return elems;
}
template <class T> map<T, ll> inv_map(vector<T> &x) {
map<T, ll> res;
rep(i, 0, x.size()) { res[x[i]] = i; }
return res;
}
template <class K, class V> map<V, K> inv_map(map<K, V> &m) {
map<V, K> res;
for (const auto &x : m) {
res[x.second] = x.first;
}
return res;
}
template <class T, class val_t = typename T::value_type,
enable_if_t<!is_same<T, set<val_t>>::value> * = nullptr>
constexpr bool exist(const T &container, val_t val) {
return find(all(container), val) != container.end();
}
template <class T, class val_t = typename T::value_type,
enable_if_t<is_same<T, set<val_t>>::value> * = nullptr>
constexpr bool exist(const T &container, val_t val) {
return container.find(val) != container.end();
}
// inner prod: |a||b|cos(theta)
template <class T> T dot(complex<T> a, complex<T> b) {
return a.real() * b.real() + a.imag() * b.imag();
}
// outer prod |a||b|sin(theta)
template <class T> T cross(complex<T> a, complex<T> b) {
return a.real() * b.imag() - a.imag() * b.real();
}
struct D2 {
enum Dir { U, D, L, R };
static inline vector<Dir> Dirs = {U, D, L, R};
D2(ll h, ll w) : h(h), w(w){};
bool in(ll n, D2::Dir d, ll k = 1) {
if ((W(n) <= k - 1) && d == L)
return false;
if ((W(n) >= w - 1 - (k - 1)) && d == R)
return false;
if ((H(n) <= k - 1) && d == D)
return false;
if ((H(n) >= h - 1 - (k - 1)) && d == U)
return false;
return true;
};
ll next(Dir d, ll k = 1) {
switch (d) {
case U:
return w * k;
case D:
return -w * k;
case L:
return -k;
case R:
return k;
default:
throw invalid_argument("not direction");
}
}
ll H(ll n) { return n / w; }
ll W(ll n) { return n % w; }
ll h, w;
};
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
ll w, h, k;
cin >> w >> h >> k;
map<pll, ll> item;
rep(i, 0, k) {
ll r, c, v;
cin >> c >> r >> v;
item[{r - 1, c - 1}] = v;
}
auto dp = makev(h, w, 4);
auto in = [&](ll i, ll j) { return item.find({i, j}) != item.end(); };
if (in(h - 1, w - 1)) {
dp[h - 1][w - 1][1] = item[{h - 1, w - 1}];
}
rrep(i, 0, h) rrep(j, 0, w) {
if (i == h - 1 && j == w - 1)
continue;
if (in(i, j)) {
if (j < w - 1)
chmax(dp[i][j][1], item[{i, j}] + max({dp[i][j + 1][1], dp[i][j + 1][2],
dp[i][j + 1][3]}));
if (i < h - 1) {
rep(k, 0, 3) chmax(dp[i][j][k + 1], item[{i, j}] + dp[i + 1][j][k]);
}
chmax(dp[i][j][1], item[{i, j}]);
}
if (j < w - 1)
rep(k, 1, 4) chmax(dp[i][j][1], dp[i][j + 1][k]);
if (i < h - 1)
rep(k, 1, 4) chmax(dp[i][j][k], dp[i + 1][j][k]);
}
cout << *max_element(all(dp[0][0])) << endl;
return 0;
}
| replace | 369 | 370 | 369 | 370 | 0 | |
p02586 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
typedef long long int64;
const int MAXN = 3000;
int n, m, q;
int a[MAXN][MAXN];
int64 f[MAXN][MAXN][4];
#define chkmax(x, y) x = std::max(x, y)
int main() {
// #ifndef ONLINE_JUDGE
// freopen("e.in", "r", stdin);
// freopen("e.out", "w", stdout);
// #endif
int i, j, k;
int x, y;
scanf("%d %d %d", &n, &m, &q);
for (i = 1; i <= q; i++) {
scanf("%d %d", &x, &y);
scanf("%d", &a[x][y]);
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
for (k = 1; k < 4; k++) {
chkmax(f[i][j][k], f[i - 1][j][3] + a[i][j]);
chkmax(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
}
for (k = 1; k < 4; k++)
chkmax(f[i][j][k], f[i][j][k - 1]);
for (k = 0; k < 4; k++)
chkmax(f[i][j][k], f[i][j - 1][k]);
}
for (j = 1; j <= m; j++)
for (k = 0; k < 4; k++)
f[i][j][k] = std::max(f[i][j][k], f[i][j - 1][k]);
}
printf("%lld\n", f[n][m][3]);
return 0;
} | #include <algorithm>
#include <cstdio>
typedef long long int64;
const int MAXN = 3000 + 10;
int n, m, q;
int a[MAXN][MAXN];
int64 f[MAXN][MAXN][4];
#define chkmax(x, y) x = std::max(x, y)
int main() {
// #ifndef ONLINE_JUDGE
// freopen("e.in", "r", stdin);
// freopen("e.out", "w", stdout);
// #endif
int i, j, k;
int x, y;
scanf("%d %d %d", &n, &m, &q);
for (i = 1; i <= q; i++) {
scanf("%d %d", &x, &y);
scanf("%d", &a[x][y]);
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
for (k = 1; k < 4; k++) {
chkmax(f[i][j][k], f[i - 1][j][3] + a[i][j]);
chkmax(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
}
for (k = 1; k < 4; k++)
chkmax(f[i][j][k], f[i][j][k - 1]);
for (k = 0; k < 4; k++)
chkmax(f[i][j][k], f[i][j - 1][k]);
}
for (j = 1; j <= m; j++)
for (k = 0; k < 4; k++)
f[i][j][k] = std::max(f[i][j][k], f[i][j - 1][k]);
}
printf("%lld\n", f[n][m][3]);
return 0;
} | replace | 5 | 6 | 5 | 6 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
int r, c, k;
cin >> r >> c >> k;
ll vec[3100][3100][4] = {};
ll p[3100][3100] = {};
for (int i = 0; i < k; i++) {
int a, b;
ll point;
cin >> a >> b >> point;
p[a][b] = point;
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
vec[i][j][0] = max(vec[i][j][0], vec[i][j - 1][0]);
vec[i][j][0] = max({vec[i][j][0], vec[i - 1][j][0], vec[i - 1][j][1],
vec[i - 1][j][2], vec[i - 1][j][3]});
vec[i][j][1] =
max({vec[i][j][1], vec[i][j - 1][0] + p[i][j], vec[i][j - 1][1]});
vec[i][j][1] =
max({vec[i][j][1], max({vec[i - 1][j][0], vec[i - 1][j][1],
vec[i - 1][j][2], vec[i - 1][j][3]}) +
p[i][j]});
vec[i][j][2] =
max({vec[i][j][2], vec[i][j - 1][1] + p[i][j], vec[i][j - 1][2]});
// vec[i][j][2]=max({vec[i][j][2],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
vec[i][j][3] =
max({vec[i][j][3], vec[i][j - 1][2] + p[i][j], vec[i][j - 1][3]});
// vec[i][j][3]=max({vec[i][j][1],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
}
}
cout << max(vec[r][c][0], max(vec[r][c][1], max(vec[r][c][2], vec[r][c][3])))
<< endl;
} | #include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
int r, c, k;
cin >> r >> c >> k;
vector<vector<vector<ll>>> vec(3100, vector<vector<ll>>(3100, vector<ll>(4)));
vector<vector<ll>> p(3100, vector<ll>(3100));
for (int i = 0; i < 3100; i++) {
for (int j = 0; j < 3100; j++) {
vec[i][j][0] = 0;
vec[i][j][1] = 0;
vec[i][j][2] = 0;
vec[i][j][3] = 0;
p[i][j] = 0;
}
}
for (int i = 0; i < k; i++) {
int a, b;
ll point;
cin >> a >> b >> point;
p[a][b] = point;
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
vec[i][j][0] = max(vec[i][j][0], vec[i][j - 1][0]);
vec[i][j][0] = max({vec[i][j][0], vec[i - 1][j][0], vec[i - 1][j][1],
vec[i - 1][j][2], vec[i - 1][j][3]});
vec[i][j][1] =
max({vec[i][j][1], vec[i][j - 1][0] + p[i][j], vec[i][j - 1][1]});
vec[i][j][1] =
max({vec[i][j][1], max({vec[i - 1][j][0], vec[i - 1][j][1],
vec[i - 1][j][2], vec[i - 1][j][3]}) +
p[i][j]});
vec[i][j][2] =
max({vec[i][j][2], vec[i][j - 1][1] + p[i][j], vec[i][j - 1][2]});
// vec[i][j][2]=max({vec[i][j][2],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
vec[i][j][3] =
max({vec[i][j][3], vec[i][j - 1][2] + p[i][j], vec[i][j - 1][3]});
// vec[i][j][3]=max({vec[i][j][1],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
}
}
cout << max(vec[r][c][0], max(vec[r][c][1], max(vec[r][c][2], vec[r][c][3])))
<< endl;
} | replace | 7 | 9 | 7 | 18 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, n) for (int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
template <class T> using V = vector<T>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
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;
}
// dp[i][j][cnt] = 座標(i,j), i行目でcnt個拾うときの価値の最大値
ll dp[3010][3010][4] = {};
int main() {
int h, w, k;
cin >> h >> w >> k;
V<V<int>> v(h + 1, V<int>(w + 1));
rep(i, k) {
int y, x;
cin >> y >> x;
int c;
cin >> c;
v[y][x] = c;
}
rep(i, h + 1) rep(j, w + 1) rep(l, 4) {
{ // ↑
int ny = i + dy[0];
int nx = j + dx[0];
chmax(dp[ny][nx][0], dp[i][j][l]);
chmax(dp[ny][nx][1], dp[i][j][l] + v[ny][nx]);
}
{ // →
int ny = i + dy[1];
int nx = j + dx[1];
chmax(dp[ny][nx][l], dp[i][j][l]);
if (l < 3)
chmax(dp[ny][nx][l + 1], dp[i][j][l] + v[ny][nx]);
}
}
ll ans = 0;
rep(l, 4) { chmax(ans, dp[h][w][l]); }
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, n) for (int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
template <class T> using V = vector<T>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
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;
}
// dp[i][j][cnt] = 座標(i,j), i行目でcnt個拾うときの価値の最大値
ll dp[3010][3010][4] = {};
int main() {
int h, w, k;
cin >> h >> w >> k;
V<V<int>> v(h + 2, V<int>(w + 2));
rep(i, k) {
int y, x;
cin >> y >> x;
int c;
cin >> c;
v[y][x] = c;
}
rep(i, h + 1) rep(j, w + 1) rep(l, 4) {
{ // ↑
int ny = i + dy[0];
int nx = j + dx[0];
chmax(dp[ny][nx][0], dp[i][j][l]);
chmax(dp[ny][nx][1], dp[i][j][l] + v[ny][nx]);
}
{ // →
int ny = i + dy[1];
int nx = j + dx[1];
chmax(dp[ny][nx][l], dp[i][j][l]);
if (l < 3)
chmax(dp[ny][nx][l + 1], dp[i][j][l] + v[ny][nx]);
}
}
ll ans = 0;
rep(l, 4) { chmax(ans, dp[h][w][l]); }
cout << ans << endl;
} | replace | 42 | 43 | 42 | 43 | -11 | |
p02586 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long int;
using ii = pair<int, int>;
using vi = vector<ll>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
#define mp(a, b) make_pair(a, b)
const int MOD = 1000 * 1000 * 1000 + 7;
ll dp(int x, int y, int it, vvi const &mat, vvvi &marcaje) {
if (y >= mat.size())
return 0;
else if (x >= mat[0].size())
return 0;
else if (marcaje[y][x][it] != -1)
return marcaje[y][x][it];
else {
// No hay item, vamos para la derecha o para abajo
if (mat[y][x] == 0) {
ll s1 = dp(x + 1, y, it, mat, marcaje);
ll s2 = dp(x, y + 1, 0, mat, marcaje);
marcaje[y][x][it] = max(s1, s2);
return marcaje[y][x][it];
} else {
// Podemos o coger el ítem o no cogerlo
// Cogemos el item
// Vamos hacia la derecha si podemos
ll s1 = 0;
if (it < 2)
s1 = dp(x + 1, y, it + 1, mat, marcaje) + mat[y][x];
// Vamos hacia abajo
ll s2 = dp(x, y + 1, 0, mat, marcaje) + mat[y][x];
// No lo cogemos
ll s3 = dp(x + 1, y, it, mat, marcaje);
ll s4 = dp(x, y + 1, 0, mat, marcaje);
marcaje[y][x][it] = max({s1, s2, s3, s4});
return marcaje[y][x][it];
}
}
}
int main() {
int r, c, k;
cin >> r >> c >> k;
vvi mat(r, vi(c));
for (int i = 0; i < k; ++i) {
int fil, col, val;
cin >> fil >> col >> val;
mat[fil - 1][col - 1] = val;
}
vvvi marcaje(r, vvi(c, vi(4, -1)));
cout << dp(0, 0, 0, mat, marcaje) << '\n';
return 0;
} | #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long int;
using ii = pair<int, int>;
using vi = vector<ll>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
#define mp(a, b) make_pair(a, b)
const int MOD = 1000 * 1000 * 1000 + 7;
ll dp(int x, int y, int it, vvi const &mat, vvvi &marcaje) {
if (y >= mat.size())
return 0;
else if (x >= mat[0].size())
return 0;
else if (marcaje[y][x][it] != -1)
return marcaje[y][x][it];
else {
// No hay item, vamos para la derecha o para abajo
if (mat[y][x] == 0) {
ll s1 = dp(x + 1, y, it, mat, marcaje);
ll s2 = dp(x, y + 1, 0, mat, marcaje);
marcaje[y][x][it] = max(s1, s2);
return marcaje[y][x][it];
} else {
// Podemos o coger el ítem o no cogerlo
// Cogemos el item
// Vamos hacia la derecha si podemos
ll s1 = 0;
if (it < 2)
s1 = dp(x + 1, y, it + 1, mat, marcaje) + mat[y][x];
// Vamos hacia abajo
ll s2 = dp(x, y + 1, 0, mat, marcaje) + mat[y][x];
// No lo cogemos
ll s3 = dp(x + 1, y, it, mat, marcaje);
ll s4 = dp(x, y + 1, 0, mat, marcaje);
marcaje[y][x][it] = max({s1, s2, s3, s4});
return marcaje[y][x][it];
}
}
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int r, c, k;
cin >> r >> c >> k;
vvi mat(r, vi(c));
for (int i = 0; i < k; ++i) {
int fil, col, val;
cin >> fil >> col >> val;
mat[fil - 1][col - 1] = val;
}
vvvi marcaje(r, vvi(c, vi(4, -1)));
cout << dp(0, 0, 0, mat, marcaje) << '\n';
return 0;
} | insert | 66 | 66 | 66 | 68 | TLE | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
const ll N = 2e3 + 30, Mod = 1e9 + 7;
const ll SQ = 330;
ll dp[N][N][4], n, m, k, a[N][N];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= k; i++) {
ll x, y, v;
cin >> x >> y >> v;
a[x][y] += v;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int x = 0; x <= 3; x++) {
ll res = 0;
for (int y = 0; y <= 3; y++) {
res = max(res, dp[i - 1][j][y]);
}
res = max(res, dp[i][j - 1][x]);
dp[i][j][x] = res;
if (!x)
continue;
res = 0;
for (int y = 0; y <= 3; y++)
res = max(res, dp[i - 1][j][y]);
res = max(res, dp[i][j - 1][x - 1]);
dp[i][j][x] = max(res + a[i][j], dp[i][j][x]);
}
}
}
cout << max({dp[n][m][1], dp[n][m][3], dp[n][m][2], dp[n][m][0]});
} | #include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
const ll N = 3e3 + 30, Mod = 1e9 + 7;
const ll SQ = 330;
ll dp[N][N][4], n, m, k, a[N][N];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= k; i++) {
ll x, y, v;
cin >> x >> y >> v;
a[x][y] += v;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int x = 0; x <= 3; x++) {
ll res = 0;
for (int y = 0; y <= 3; y++) {
res = max(res, dp[i - 1][j][y]);
}
res = max(res, dp[i][j - 1][x]);
dp[i][j][x] = res;
if (!x)
continue;
res = 0;
for (int y = 0; y <= 3; y++)
res = max(res, dp[i - 1][j][y]);
res = max(res, dp[i][j - 1][x - 1]);
dp[i][j][x] = max(res + a[i][j], dp[i][j][x]);
}
}
}
cout << max({dp[n][m][1], dp[n][m][3], dp[n][m][2], dp[n][m][0]});
} | replace | 10 | 11 | 10 | 11 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MOD = 1e9 + 7;
ll R, C;
ll dp[3000][3000][4];
// dp[i][j][picked] : denotes the max value picked from (i,j) to (R,C) with the
// constraints given
ll rec(int i, int j, int picked, vector<vector<ll>> &grid) {
if (i > R || i < 1 || j > C || j < 1)
return 0;
if (dp[i][j][picked] != -1)
return dp[i][j][picked];
dp[i][j][picked] = max(dp[i][j][picked], rec(i + 1, j, 0, grid));
if (picked < 3)
dp[i][j][picked] =
max(dp[i][j][picked], grid[i][j] + rec(i + 1, j, 0, grid));
dp[i][j][picked] = max(dp[i][j][picked], rec(i, j + 1, picked, grid));
if (picked < 3)
dp[i][j][picked] =
max(dp[i][j][picked], rec(i, j + 1, picked + 1, grid) + grid[i][j]);
return dp[i][j][picked];
}
void solve() {
ll K;
cin >> R >> C >> K;
vector<vector<ll>> grid(R + 1, vector<ll>(C + 1, 0));
memset(dp, -1, sizeof(dp));
for (ll i = 0; i < K; i++) {
ll r, c, v;
cin >> r >> c >> v;
grid[r][c] = v;
}
cout << rec(1, 1, 0, grid) << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MOD = 1e9 + 7;
ll R, C;
ll dp[3001][3001][4];
// dp[i][j][picked] : denotes the max value picked from (i,j) to (R,C) with the
// constraints given
ll rec(int i, int j, int picked, vector<vector<ll>> &grid) {
if (i > R || i < 1 || j > C || j < 1)
return 0;
if (dp[i][j][picked] != -1)
return dp[i][j][picked];
dp[i][j][picked] = max(dp[i][j][picked], rec(i + 1, j, 0, grid));
if (picked < 3)
dp[i][j][picked] =
max(dp[i][j][picked], grid[i][j] + rec(i + 1, j, 0, grid));
dp[i][j][picked] = max(dp[i][j][picked], rec(i, j + 1, picked, grid));
if (picked < 3)
dp[i][j][picked] =
max(dp[i][j][picked], rec(i, j + 1, picked + 1, grid) + grid[i][j]);
return dp[i][j][picked];
}
void solve() {
ll K;
cin >> R >> C >> K;
vector<vector<ll>> grid(R + 1, vector<ll>(C + 1, 0));
memset(dp, -1, sizeof(dp));
for (ll i = 0; i < K; i++) {
ll r, c, v;
cin >> r >> c >> v;
grid[r][c] = v;
}
cout << rec(1, 1, 0, grid) << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
} | replace | 5 | 6 | 5 | 6 | -11 | |
p02586 | C++ | Runtime Error | // #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
//========================TypeDefs===================================
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ldb;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
//=========================MACROS====================================
#define pb push_back
#define popb pop_back()
#define pf push_front
#define popf pop_front()
#define si size()
#define be begin()
#define en end()
#define all(v) v.be, v.en
#define le length()
#define mp make_pair
#define mt make_tuple
#define acc(v) accumulate(all(v), 0)
#define fi first
#define se second
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define vvi vector<vector<int>>
#define vvll vector<vll>
#define mli map<ll, int>
#define mll map<ll, ll>
#define forz(i, n) for (int i = 0; i < n; i++)
#define fora(i, m, n) for (int i = m; i < n; i++)
#define rforz(i, n) for (int i = n - 1; i >= 0; i--)
#define rfora(i, n, m) for (int i = n; i >= m; i--)
#define deci(n) fixed << setprecision(n)
#define high(n) __builtin_popcount(n)
#define highll(n) __builtin_popcountll(n)
#define parity(n) __builtin_parity(n)
#define clz(n) __builtin_clz(n)
#define clzll(n) __builtin_clzll(n)
#define ctz(n) __builtin_ctz(n)
#define bset(a, p) ((a) | (1ll << (p)))
#define bchk(a, p) ((a) & (1ll << (p)))
#define bxor(a, p) ((a) ^ (1ll << (p)));
#define brem(a, p) (bchk(a, p) ? (bxor(a, p)) : (a))
#define lb lower_bound
#define ub upper_bound
#define er equal_range
#define findnot find_first_not_of
#define maxe *max_element
#define mine *min_element
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.1415926535897932384
#define INF LLONG_MAX
#define gcd __gcd
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define P0(a) cout << a << " "
#define P1(a) cout << a << endl
#define P2(a, b) cout << a << " " << b << endl
#define P3(a, b, c) cout << a << " " << b << " " << c << endl
#define P4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define oset \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define osetlli \
tree<lli, null_type, less<lli>, rb_tree_tag, \
tree_order_statistics_node_update>
#define ofk order_of_key
#define fbo find_by_order
// member functions :
// 1. order_of_key(k) : number of elements sbtriectly lesser than k
// 2. find_by_order(k) : k-th element in the set
//==============================FUNCTIONS===========================================
auto clk = clock();
mt19937_64
rang(chrono::high_resolution_clock::now().time_since_epoch().count());
void run_time() {
#ifdef local
cout << endl;
cout << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;
#endif
return;
}
inline ll power(ll x, ll y, ll p = MOD) {
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void SieveOfEratosthenes(ll n) {
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (ll p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p] == true) {
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (ll i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (ll p = 2; p <= n; p++)
if (prime[p])
cout << p << " ";
}
inline ll modadd(ll a, ll b, ll m = MOD) {
a += b;
if (a >= m)
a -= m;
return a;
}
inline ll modmul(ll a, ll b, ll m = MOD) { return ((a % m) * (b % m)) % m; }
inline ll modi(ll a, ll m = MOD) { return power(a, m - 2, m); }
long long int inverse(long long int i, ll m = MOD) {
if (i == 1)
return 1;
return (m - ((m / i) * inverse(m % i)) % m + m) % m;
}
//================================CODE=============================================
int main() {
fastio;
ll r, c, k, a, b, m;
cin >> r >> c >> k;
ll dp[3001][4][3001] = {0};
vvll v(r, vll(c, 0));
fora(i, 0, k) {
cin >> a >> b >> m;
v[a - 1][b - 1] = m;
}
fora(i, 1, r + 1) {
fora(cn, 0, 4) {
fora(j, 1, c + 1) {
if (cn != 0) {
if (cn != 1)
dp[i][cn][j] = max({dp[i][cn][j], dp[i][cn][j - 1],
dp[i][cn - 1][j - 1] + v[i - 1][j - 1]});
else {
dp[i][cn][j] = max({dp[i][cn][j], dp[i][cn][j - 1],
dp[i][cn - 1][j - 1] + v[i - 1][j - 1],
max({dp[i - 1][0][j], dp[i - 1][1][j],
dp[i - 1][2][j], dp[i - 1][3][j]}) +
v[i - 1][j - 1]});
}
} else {
dp[i][cn][j] = max({dp[i][cn][j],
max({dp[i - 1][0][j], dp[i - 1][1][j],
dp[i - 1][2][j], dp[i - 1][3][j]}),
dp[i][cn][j - 1]});
}
}
}
}
cout << max({dp[r][0][c], dp[r][1][c], dp[r][2][c], dp[r][3][c]}) << endl;
run_time();
return 0;
} | // #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
//========================TypeDefs===================================
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ldb;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
//=========================MACROS====================================
#define pb push_back
#define popb pop_back()
#define pf push_front
#define popf pop_front()
#define si size()
#define be begin()
#define en end()
#define all(v) v.be, v.en
#define le length()
#define mp make_pair
#define mt make_tuple
#define acc(v) accumulate(all(v), 0)
#define fi first
#define se second
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define vvi vector<vector<int>>
#define vvll vector<vll>
#define mli map<ll, int>
#define mll map<ll, ll>
#define forz(i, n) for (int i = 0; i < n; i++)
#define fora(i, m, n) for (int i = m; i < n; i++)
#define rforz(i, n) for (int i = n - 1; i >= 0; i--)
#define rfora(i, n, m) for (int i = n; i >= m; i--)
#define deci(n) fixed << setprecision(n)
#define high(n) __builtin_popcount(n)
#define highll(n) __builtin_popcountll(n)
#define parity(n) __builtin_parity(n)
#define clz(n) __builtin_clz(n)
#define clzll(n) __builtin_clzll(n)
#define ctz(n) __builtin_ctz(n)
#define bset(a, p) ((a) | (1ll << (p)))
#define bchk(a, p) ((a) & (1ll << (p)))
#define bxor(a, p) ((a) ^ (1ll << (p)));
#define brem(a, p) (bchk(a, p) ? (bxor(a, p)) : (a))
#define lb lower_bound
#define ub upper_bound
#define er equal_range
#define findnot find_first_not_of
#define maxe *max_element
#define mine *min_element
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.1415926535897932384
#define INF LLONG_MAX
#define gcd __gcd
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define P0(a) cout << a << " "
#define P1(a) cout << a << endl
#define P2(a, b) cout << a << " " << b << endl
#define P3(a, b, c) cout << a << " " << b << " " << c << endl
#define P4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define oset \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
#define osetlli \
tree<lli, null_type, less<lli>, rb_tree_tag, \
tree_order_statistics_node_update>
#define ofk order_of_key
#define fbo find_by_order
// member functions :
// 1. order_of_key(k) : number of elements sbtriectly lesser than k
// 2. find_by_order(k) : k-th element in the set
//==============================FUNCTIONS===========================================
auto clk = clock();
mt19937_64
rang(chrono::high_resolution_clock::now().time_since_epoch().count());
void run_time() {
#ifdef local
cout << endl;
cout << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;
#endif
return;
}
inline ll power(ll x, ll y, ll p = MOD) {
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void SieveOfEratosthenes(ll n) {
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (ll p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p] == true) {
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (ll i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (ll p = 2; p <= n; p++)
if (prime[p])
cout << p << " ";
}
inline ll modadd(ll a, ll b, ll m = MOD) {
a += b;
if (a >= m)
a -= m;
return a;
}
inline ll modmul(ll a, ll b, ll m = MOD) { return ((a % m) * (b % m)) % m; }
inline ll modi(ll a, ll m = MOD) { return power(a, m - 2, m); }
long long int inverse(long long int i, ll m = MOD) {
if (i == 1)
return 1;
return (m - ((m / i) * inverse(m % i)) % m + m) % m;
}
//================================CODE=============================================
int main() {
fastio;
ll r, c, k, a, b, m;
cin >> r >> c >> k;
vector<vvll> dp(3001, vvll(4, vll(3001, 0)));
vvll v(r, vll(c, 0));
fora(i, 0, k) {
cin >> a >> b >> m;
v[a - 1][b - 1] = m;
}
fora(i, 1, r + 1) {
fora(cn, 0, 4) {
fora(j, 1, c + 1) {
if (cn != 0) {
if (cn != 1)
dp[i][cn][j] = max({dp[i][cn][j], dp[i][cn][j - 1],
dp[i][cn - 1][j - 1] + v[i - 1][j - 1]});
else {
dp[i][cn][j] = max({dp[i][cn][j], dp[i][cn][j - 1],
dp[i][cn - 1][j - 1] + v[i - 1][j - 1],
max({dp[i - 1][0][j], dp[i - 1][1][j],
dp[i - 1][2][j], dp[i - 1][3][j]}) +
v[i - 1][j - 1]});
}
} else {
dp[i][cn][j] = max({dp[i][cn][j],
max({dp[i - 1][0][j], dp[i - 1][1][j],
dp[i - 1][2][j], dp[i - 1][3][j]}),
dp[i][cn][j - 1]});
}
}
}
}
cout << max({dp[r][0][c], dp[r][1][c], dp[r][2][c], dp[r][3][c]}) << endl;
run_time();
return 0;
} | replace | 174 | 175 | 174 | 175 | -11 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int r, c, k;
cin >> r >> c >> k;
vector<vector<ll>> item(r, vector<ll>(c));
ll dp[3100][3100][4];
for (int i = 0; i < r + 100; i++) {
for (int j = 0; j < c + 100; j++) {
for (int ii = 0; ii < 4; ii++) {
dp[i][j][ii] = 0;
}
}
}
for (int i = 0; i < k; i++) {
ll x, y, z;
cin >> x >> y >> z;
x--;
y--;
item[x][y] = z;
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
for (int ii = 0; ii < 4; ii++) {
if (item[i][j] == 0) {
dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
} else {
dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
if (ii < 3) {
dp[i][j + 1][ii + 1] =
max(dp[i][j + 1][ii + 1], dp[i][j][ii] + item[i][j]);
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii] + item[i][j]);
}
}
}
}
}
ll ans = 0;
for (int i = 0; i <= r; i++) {
for (int j = 0; j <= c; j++) {
for (int ii = 0; ii < 4; ii++) {
ans = max(ans, dp[i][j][ii]);
}
}
}
cout << ans << '\n';
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int r, c, k;
cin >> r >> c >> k;
vector<vector<ll>> item(r, vector<ll>(c));
vector<vector<vector<ll>>> dp(r + 100,
vector<vector<ll>>(c + 100, vector<ll>(4, 0)));
/* ll dp[3100][3100][4];
for (int i = 0; i < r + 100; i++) {
for (int j = 0; j < c + 100; j++) {
for (int ii = 0; ii < 4; ii++) {
dp[i][j][ii] = 0;
}
}
}*/
for (int i = 0; i < k; i++) {
ll x, y, z;
cin >> x >> y >> z;
x--;
y--;
item[x][y] = z;
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
for (int ii = 0; ii < 4; ii++) {
if (item[i][j] == 0) {
dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
} else {
dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
if (ii < 3) {
dp[i][j + 1][ii + 1] =
max(dp[i][j + 1][ii + 1], dp[i][j][ii] + item[i][j]);
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii] + item[i][j]);
}
}
}
}
}
ll ans = 0;
for (int i = 0; i <= r; i++) {
for (int j = 0; j <= c; j++) {
for (int ii = 0; ii < 4; ii++) {
ans = max(ans, dp[i][j][ii]);
}
}
}
cout << ans << '\n';
} | replace | 13 | 21 | 13 | 23 | -11 | |
p02586 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define ll long long
#define all(v) (v).begin(), (v).end()
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define ii pair<ll, ll>
#define sz(v) ((int)((v).size()))
#define lp(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define pf push_front
#define F first
#define S second
#define endl "\n"
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
const double PI = 3.14159265358979323846;
const ll inf = 2e9, MOD = 998244353, MAX = 3e2 + 2;
int r, c, k, grid[MAX][MAX];
ll dp[MAX][MAX][4];
ll sol(int x, int y, int cnt) {
if (x >= r || y >= c)
return -1e15;
if (x == r - 1 && y == c - 1) {
if (cnt < 3)
return grid[r - 1][c - 1];
return 0;
}
ll &ret = dp[x][y][cnt];
if (~ret)
return ret;
ret = 0;
if (cnt < 3) {
ret = max(sol(x, y + 1, cnt + (grid[x][y] != 0)) + grid[x][y],
max(ret, sol(x + 1, y, 0) + grid[x][y]));
}
ret = max(ret, max(sol(x + 1, y, 0), sol(x, y + 1, cnt)));
return ret;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
IOS;
cin >> r >> c >> k;
lp(i, 0, k) {
int a, b, v;
cin >> a >> b >> v;
grid[a - 1][b - 1] = v;
}
memset(dp, -1, sizeof(dp));
cout << sol(0, 0, 0);
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define ll long long
#define all(v) (v).begin(), (v).end()
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define ii pair<ll, ll>
#define sz(v) ((int)((v).size()))
#define lp(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define pf push_front
#define F first
#define S second
#define endl "\n"
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
const double PI = 3.14159265358979323846;
const ll inf = 2e9, MOD = 998244353, MAX = 3e3 + 2;
int r, c, k, grid[MAX][MAX];
ll dp[MAX][MAX][4];
ll sol(int x, int y, int cnt) {
if (x >= r || y >= c)
return -1e15;
if (x == r - 1 && y == c - 1) {
if (cnt < 3)
return grid[r - 1][c - 1];
return 0;
}
ll &ret = dp[x][y][cnt];
if (~ret)
return ret;
ret = 0;
if (cnt < 3) {
ret = max(sol(x, y + 1, cnt + (grid[x][y] != 0)) + grid[x][y],
max(ret, sol(x + 1, y, 0) + grid[x][y]));
}
ret = max(ret, max(sol(x + 1, y, 0), sol(x, y + 1, cnt)));
return ret;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
IOS;
cin >> r >> c >> k;
lp(i, 0, k) {
int a, b, v;
cin >> a >> b >> v;
grid[a - 1][b - 1] = v;
}
memset(dp, -1, sizeof(dp));
cout << sol(0, 0, 0);
} | replace | 38 | 39 | 38 | 39 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pvec(v) \
for (auto e : v) \
cout << e << " ";
#define IO \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define INF 10000000000000005
#define MOD 1000000007
#define all(v) v.begin(), v.end()
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
ll power(ll x, ll y, ll p = MOD) {
ll res = 1LL;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1LL)
res = (res * x) % p;
y = y >> 1LL;
x = (x * x) % p;
}
return res;
}
ll dp[100][100][4];
int r, c, k;
bool valid(int x, int y) {
if (x < 0 || y < 0 || x >= r || y >= c)
return false;
return true;
}
int main() {
// IO
cin >> r >> c >> k;
ll arr[r][c];
memset(arr, 0LL, sizeof(arr));
int x, y;
ll v;
bool p = false;
for (int i = 0; i < k; ++i) {
cin >> x >> y >> v;
--x;
--y;
if (x == 0 && y == 0) {
p = true;
}
arr[x][y] = v;
}
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
for (int k = 0; k < 4; ++k) {
dp[i][j][k] = -INF;
}
}
}
dp[0][0][0] = 0;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
if (arr[i][j]) {
for (int k = 2; k >= 0; --k) {
dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + arr[i][j]);
}
}
for (int k = 0; k < 4; ++k) {
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
}
}
}
ll mx = 0;
for (int i = 0; i < 4; ++i) {
mx = max(mx, dp[r - 1][c - 1][i]);
}
cout << mx << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pvec(v) \
for (auto e : v) \
cout << e << " ";
#define IO \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define INF 10000000000000005
#define MOD 1000000007
#define all(v) v.begin(), v.end()
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
ll power(ll x, ll y, ll p = MOD) {
ll res = 1LL;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1LL)
res = (res * x) % p;
y = y >> 1LL;
x = (x * x) % p;
}
return res;
}
ll dp[3005][3005][4];
int r, c, k;
bool valid(int x, int y) {
if (x < 0 || y < 0 || x >= r || y >= c)
return false;
return true;
}
int main() {
// IO
cin >> r >> c >> k;
ll arr[r][c];
memset(arr, 0LL, sizeof(arr));
int x, y;
ll v;
bool p = false;
for (int i = 0; i < k; ++i) {
cin >> x >> y >> v;
--x;
--y;
if (x == 0 && y == 0) {
p = true;
}
arr[x][y] = v;
}
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
for (int k = 0; k < 4; ++k) {
dp[i][j][k] = -INF;
}
}
}
dp[0][0][0] = 0;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
if (arr[i][j]) {
for (int k = 2; k >= 0; --k) {
dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + arr[i][j]);
}
}
for (int k = 0; k < 4; ++k) {
dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
}
}
}
ll mx = 0;
for (int i = 0; i < 4; ++i) {
mx = max(mx, dp[r - 1][c - 1][i]);
}
cout << mx << endl;
return 0;
} | replace | 45 | 46 | 45 | 46 | 0 | |
p02586 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};
// ll dp[3010][3010][5];
vector<VVL> dp(5, VVL(3010, VL(3010, 0)));
ll tbl[3010][3010];
void init() {
rep(i, 0, 3010) rep(j, 0, 3010) {
// rep(k, 0, 5) dp[i][j][k] = 0;
tbl[i][j] = 0;
}
}
int main() {
init();
// input
ll R, C, K;
cin >> R >> C >> K;
VL r(K), c(K), v(K);
rep(i, 0, K) {
cin >> r[i] >> c[i] >> v[i];
tbl[r[i] - 1][c[i] - 1] = v[i];
}
rep(i, 0, R + 1) {
rep(j, 0, C + 1) {
// (i, j) -> (i+1, j)
if (i < R) {
// i, j で取らない
rep(k, 0, 4) dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
// i, j で取る
rep(k, 0, 3) dp[i + 1][j][0] =
max(dp[i + 1][j][0], dp[i][j][k] + tbl[i][j]);
}
// (i, j) -> (i, j+1)
if (j < C) {
// i, jで取らない
rep(k, 0, 4) dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
// i, jで取る
rep(k, 0, 3) dp[i][j + 1][k + 1] =
max(dp[i][j + 1][k + 1], dp[i][j][k] + tbl[i][j]);
}
}
}
ll ans = 0;
rep(i, 0, 4) ans = max(dp[R][C][i], ans);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};
// ll dp[3010][3010][5];
vector<VVL> dp(3010, VVL(3010, VL(5, 0)));
ll tbl[3010][3010];
void init() {
rep(i, 0, 3010) rep(j, 0, 3010) {
// rep(k, 0, 5) dp[i][j][k] = 0;
tbl[i][j] = 0;
}
}
int main() {
init();
// input
ll R, C, K;
cin >> R >> C >> K;
VL r(K), c(K), v(K);
rep(i, 0, K) {
cin >> r[i] >> c[i] >> v[i];
tbl[r[i] - 1][c[i] - 1] = v[i];
}
rep(i, 0, R + 1) {
rep(j, 0, C + 1) {
// (i, j) -> (i+1, j)
if (i < R) {
// i, j で取らない
rep(k, 0, 4) dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
// i, j で取る
rep(k, 0, 3) dp[i + 1][j][0] =
max(dp[i + 1][j][0], dp[i][j][k] + tbl[i][j]);
}
// (i, j) -> (i, j+1)
if (j < C) {
// i, jで取らない
rep(k, 0, 4) dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
// i, jで取る
rep(k, 0, 3) dp[i][j + 1][k + 1] =
max(dp[i][j + 1][k + 1], dp[i][j][k] + tbl[i][j]);
}
}
}
ll ans = 0;
rep(i, 0, 4) ans = max(dp[R][C][i], ans);
cout << ans << endl;
return 0;
} | replace | 16 | 17 | 16 | 17 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02586 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
int R, C, K;
cin >> R >> C >> K;
map<int, map<int, ll>> goods;
int r, c, v;
for (int i = 0; i < K; ++i) {
cin >> r >> c >> v;
goods[r - 1][c - 1] = v;
}
ll dp[2][C][4];
memset(dp, 0, sizeof(dp));
for (int y = 0; y < R; ++y) {
int cy = y % 2;
int by = (y + 1) % 2;
for (int x = 0; x < C; ++x) {
ll mv = LLONG_MIN;
for (int c = 0; c <= 3; ++c) {
mv = max(mv, dp[by][x][c]);
}
dp[cy][x][0] = mv;
if (goods[y][x] > 0) {
dp[cy][x][1] = mv + goods[y][x];
}
}
for (int x = 1; x < C; ++x) {
for (int c = 0; c <= 3; ++c) {
dp[cy][x][c] = max(dp[cy][x][c], dp[cy][x - 1][c]);
if (c < 3 && goods[y][x] > 0) {
dp[cy][x][c + 1] =
max(dp[cy][x][c + 1], dp[cy][x - 1][c] + goods[y][x]);
}
}
}
}
ll ans = LLONG_MIN;
for (int c = 0; c <= 3; ++c) {
ans = max(ans, dp[(R - 1) % 2][C - 1][c]);
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
int R, C, K;
cin >> R >> C >> K;
int goods[R][C];
memset(goods, 0, sizeof(goods));
// map<int, map<int, ll>> goods;
int r, c, v;
for (int i = 0; i < K; ++i) {
cin >> r >> c >> v;
goods[r - 1][c - 1] = v;
}
ll dp[2][C][4];
memset(dp, 0, sizeof(dp));
for (int y = 0; y < R; ++y) {
int cy = y % 2;
int by = (y + 1) % 2;
for (int x = 0; x < C; ++x) {
ll mv = LLONG_MIN;
for (int c = 0; c <= 3; ++c) {
mv = max(mv, dp[by][x][c]);
}
dp[cy][x][0] = mv;
if (goods[y][x] > 0) {
dp[cy][x][1] = mv + goods[y][x];
}
}
for (int x = 1; x < C; ++x) {
for (int c = 0; c <= 3; ++c) {
dp[cy][x][c] = max(dp[cy][x][c], dp[cy][x - 1][c]);
if (c < 3 && goods[y][x] > 0) {
dp[cy][x][c + 1] =
max(dp[cy][x][c + 1], dp[cy][x - 1][c] + goods[y][x]);
}
}
}
}
ll ans = LLONG_MIN;
for (int c = 0; c <= 3; ++c) {
ans = max(ans, dp[(R - 1) % 2][C - 1][c]);
}
cout << ans << endl;
return 0;
}
| replace | 19 | 20 | 19 | 22 | TLE | |
p02586 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, k, n) for (int i = k; i <= n; i++)
#define repd(i, k, n) for (ll i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\n"
#define INF (ll) ll_MAX
typedef long long ll;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, ll>> vpp;
const ll N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;
using namespace std;
void solve() {
ll r, c, k;
cin >> r >> c >> k;
ll a[r + 3][c + 3];
memset(a, 0, sizeof(a));
ll dp[r + 3][c + 3][4];
memset(dp, 0, sizeof(dp));
for (ll i = 0; i < k; i++) {
ll t1, t2, t3;
cin >> t1 >> t2 >> t3;
a[t1][t2] = t3;
}
// cout<<dp[1][1];
for (ll i = r; i >= 1; i--) {
for (ll j = c; j >= 1; j--) {
// for(ll k1 = 0;k1<4;k1++){
// dp[i][j][k1] = max(dp[i+1][j][k1],dp[i][j+1][k1]);
// }
// if(a[i][j]){
// dp[i][j][0] = max()
// }
dp[i][j][0] = max({dp[i + 1][j][0], dp[i + 1][j][1], dp[i + 1][j][2],
dp[i + 1][j][3], dp[i][j + 1][0]});
dp[i][j][1] = max(
{dp[i][j][0] + a[i][j], dp[i][j + 1][0] + a[i][j], dp[i][j + 1][1]});
dp[i][j][2] = max(dp[i][j + 1][2], dp[i][j + 1][1] + a[i][j]);
dp[i][j][3] = max(dp[i][j + 1][3], dp[i][j + 1][2] + a[i][j]);
}
}
// cout<<dp[1][2][2]<<endl;
cout << max({dp[1][1][0], dp[1][1][1], dp[1][1][2], dp[1][1][3]});
}
int main() {
// #ifndef ONLINE_JUDGE
// // for getting input from input.txt
// freopen("input.txt", "r", stdin);
// // for writing output to output.txt
// freopen("output.txt", "w", stdout);
// #endif
ios_base::sync_with_stdio(0);
cin.tie(NULL);
ll t = 1;
// cin>>t;
ll ctr = 0;
while (t--) {
ctr++;
// cout<<"Case #"<<ctr<<": ";
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
} | #include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, k, n) for (int i = k; i <= n; i++)
#define repd(i, k, n) for (ll i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\n"
#define INF (ll) ll_MAX
typedef long long ll;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, ll>> vpp;
const ll N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;
using namespace std;
void solve() {
ll r, c, k;
cin >> r >> c >> k;
ll a[r + 3][c + 3];
memset(a, 0, sizeof(a));
vector<vector<vector<ll>>> dp(r + 3,
vector<vector<ll>>(c + 3, vector<ll>(4)));
// memset(dp,0,sizeof(dp));
for (ll i = 0; i < k; i++) {
ll t1, t2, t3;
cin >> t1 >> t2 >> t3;
a[t1][t2] = t3;
}
// cout<<dp[1][1];
for (ll i = r; i >= 1; i--) {
for (ll j = c; j >= 1; j--) {
// for(ll k1 = 0;k1<4;k1++){
// dp[i][j][k1] = max(dp[i+1][j][k1],dp[i][j+1][k1]);
// }
// if(a[i][j]){
// dp[i][j][0] = max()
// }
dp[i][j][0] = max({dp[i + 1][j][0], dp[i + 1][j][1], dp[i + 1][j][2],
dp[i + 1][j][3], dp[i][j + 1][0]});
dp[i][j][1] = max(
{dp[i][j][0] + a[i][j], dp[i][j + 1][0] + a[i][j], dp[i][j + 1][1]});
dp[i][j][2] = max(dp[i][j + 1][2], dp[i][j + 1][1] + a[i][j]);
dp[i][j][3] = max(dp[i][j + 1][3], dp[i][j + 1][2] + a[i][j]);
}
}
// cout<<dp[1][2][2]<<endl;
cout << max({dp[1][1][0], dp[1][1][1], dp[1][1][2], dp[1][1][3]});
}
int main() {
// #ifndef ONLINE_JUDGE
// // for getting input from input.txt
// freopen("input.txt", "r", stdin);
// // for writing output to output.txt
// freopen("output.txt", "w", stdout);
// #endif
ios_base::sync_with_stdio(0);
cin.tie(NULL);
ll t = 1;
// cin>>t;
ll ctr = 0;
while (t--) {
ctr++;
// cout<<"Case #"<<ctr<<": ";
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
} | replace | 44 | 46 | 44 | 47 | 0 | Time : 23.212ms
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.