This documentation is automatically generated by online-judge-tools/verification-helper
// #define PROBLEM "https://codeforces.com/contest/2026/problem/F"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"
/*
* Educational Codeforces Round 171 (Div. 2) F. Bermart Ice Cream
* https://codeforces.com/contest/2026/submission/315943049
*/
#include "../../Src/DataStructure/SWAG/FoldableDeque.hpp"
#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <vector>
const int P = 2000;
struct S {
using Element = std::pair<int, int>; // {p, t}
using Fold = struct {
using Element = std::vector<int>;
static Element identity() {
Element res(P + 1, -1);
res[0] = 0;
return res;
}
};
using F = typename Fold::Element;
static F convert(Element v) {
F res = Fold::identity();
res[v.first] = v.second;
return res;
}
static F pushBack(F dp, Element v) {
for (int i = P ; i >= v.first ; i--) if (dp[i - v.first] != -1) {
dp[i] = std::max(dp[i], dp[i - v.first] + v.second);
}
return dp;
}
static F pushFront(F dp, Element v) {
for (int i = P ; i >= v.first ; i--) if (dp[i - v.first] != -1) {
dp[i] = std::max(dp[i], dp[i - v.first] + v.second);
}
return dp;
}
};
int Q, T[30030], x[30030], p[30030], t[30030], ans[30030], pop_id[30030], SIZE = 1;
std::vector<std::pair<int, int>> g[30030];
std::vector<int> need[30030];
int make_node(int par, int id) {
g[par].push_back({SIZE, id});
return SIZE++;
}
zawa::FoldableDeque<S> deq;
void dfs(int v) {
if (need[v].size()) {
auto [dp, ep] = deq.get();
for (int i = 1 ; i <= P ; i++) {
dp[i] = std::max(dp[i], dp[i - 1]);
ep[i] = std::max(ep[i], ep[i - 1]);
}
for (int id : need[v]) {
ans[id] = 0;
for (int i = 0 ; i <= p[id] ; i++) ans[id] = std::max(ans[id], dp[i] + ep[p[id] - i]);
}
}
for (auto [x, id] : g[v]) {
if (T[id] == 2) {
deq.pushBack({p[id], t[id]});
dfs(x);
deq.popBack();
}
else if (T[id] == 3) {
const auto ice = deq.popFront();
dfs(x);
deq.pushFront(ice);
}
else assert(false);
}
}
void solve() {
std::cin >> Q;
std::vector<int> cur(1);
for (int i = 0 ; i < Q ; i++) {
ans[i] = -1;
std::cin >> T[i] >> x[i];
x[i]--;
if (T[i] == 1) {
cur.push_back(cur[x[i]]);
}
else if (T[i] == 2) {
std::cin >> p[i] >> t[i];
cur[x[i]] = make_node(cur[x[i]], i);
}
else if (T[i] == 3) {
const int v = cur[x[i]];
if (pop_id[v] == 0) {
pop_id[v] = cur[x[i]] = make_node(v, i);
}
else {
cur[x[i]] = pop_id[v];
}
}
else if (T[i] == 4) {
std::cin >> p[i];
need[cur[x[i]]].push_back(i);
}
else assert(false);
}
dfs(0);
for (int i = 0 ; i < Q ; i++) if (ans[i] != -1) std::cout << ans[i] << '\n';
}
int main() {
#ifdef ONLINE_JUDGE
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
solve();
#else
std::cout << "Hello World\n";
#endif
}
#line 1 "Test/CF/EC171-F.test.cpp"
// #define PROBLEM "https://codeforces.com/contest/2026/problem/F"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"
/*
* Educational Codeforces Round 171 (Div. 2) F. Bermart Ice Cream
* https://codeforces.com/contest/2026/submission/315943049
*/
#line 2 "Src/DataStructure/SWAG/FoldableDeque.hpp"
#line 2 "Src/DataStructure/SWAG/SWAGable.hpp"
#line 2 "Src/Algebra/Monoid/MonoidConcept.hpp"
#line 2 "Src/Algebra/Semigroup/SemigroupConcept.hpp"
#include <concepts>
namespace zawa {
namespace concepts {
template <class T>
concept Semigroup = requires {
typename T::Element;
{ T::operation(std::declval<typename T::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Element>;
};
} // namespace concepts
} // namespace zawa
#line 4 "Src/Algebra/Monoid/MonoidConcept.hpp"
#line 6 "Src/Algebra/Monoid/MonoidConcept.hpp"
namespace zawa {
namespace concepts {
template <class T>
concept Identitiable = requires {
typename T::Element;
{ T::identity() } -> std::same_as<typename T::Element>;
};
template <class T>
concept Monoid = Semigroup<T> and Identitiable<T>;
} // namespace
} // namespace zawa
#line 5 "Src/DataStructure/SWAG/SWAGable.hpp"
#line 7 "Src/DataStructure/SWAG/SWAGable.hpp"
namespace zawa {
namespace concepts {
template <class T>
concept SWAGable = requires {
typename T::Element;
typename T::Fold;
typename T::Fold::Element;
{ T::convert(std::declval<typename T::Element>()) } -> std::same_as<typename T::Fold::Element>;
{ T::pushBack(std::declval<typename T::Fold::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Fold::Element>;
{ T::pushFront(std::declval<typename T::Fold::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Fold::Element>;
};
} // namespace concepts
template <concepts::Semigroup S>
class SemigroupSWAGable {
public:
using Element = typename S::Element;
using Fold = S;
using F = Fold::Element;
static F convert(Element v) {
return v;
}
static F pushBack(F f, Element v) {
return S::operation(f, v);
}
static F pushFront(F f, Element v) {
return S::operation(v, f);
}
static F operation(F l, F r) {
return S::operation(l, r);
}
};
template <concepts::Monoid S>
class MonoidSWAGable {
public:
using Element = typename S::Element;
using Fold = S;
using F = Fold::Element;
static F convert(Element v) {
return v;
}
static F pushBack(F f, Element v) {
return S::operation(f, v);
}
static F pushFront(F f, Element v) {
return S::operation(v, f);
}
static F identity() {
return S::identity();
}
static F operation(F l, F r) {
return S::operation(l, r);
}
};
} // namespace zawa
#line 2 "Src/Template/TypeAlias.hpp"
#include <cstdint>
#include <cstddef>
namespace zawa {
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using usize = std::size_t;
} // namespace zawa
#line 7 "Src/DataStructure/SWAG/FoldableDeque.hpp"
#include <cassert>
#include <optional>
#include <vector>
namespace zawa {
template <concepts::SWAGable S>
class FoldableDeque {
public:
using V = typename S::Element;
using Fold = typename S::Fold;
using F = typename Fold::Element;
FoldableDeque() = default;
inline usize size() const {
return m_front.size() + m_back.size();
}
inline bool empty() const {
return m_front.empty() and m_back.empty();
}
void pushBack(const V& v) {
m_back.push_back({
m_back.empty() ? S::convert(v) : S::pushBack(m_back.back().first, v),
v
});
}
void pushFront(const V& v) {
m_front.push_back({
m_front.empty() ? S::convert(v) : S::pushFront(m_front.back().first, v),
v
});
}
std::pair<F, F> get() const requires concepts::Identitiable<typename S::Fold> {
return {
m_front.empty() ? Fold::identity() : m_front.back().first,
m_back.empty() ? Fold::identity() : m_back.back().first
};
}
std::pair<std::optional<F>, std::optional<F>> get() const {
return {
m_front.empty() ? std::nullopt : std::optional<F>{m_front.back().first},
m_back.empty() ? std::nullopt : std::optional<F>{m_back.back().first}
};
}
F product() const requires concepts::Monoid<typename S::Fold> {
auto [f, b] = get();
return Fold::operation(f, b);
}
F product() const requires concepts::Semigroup<typename S::Fold> {
assert(m_front.size() or m_back.size());
if (m_front.empty()) return m_back.back().first;
if (m_back.empty()) return m_front.back().first;
return S::Fold::operation(m_front.back().first, m_back.back().first);
}
V popFront() {
assert(size());
if (m_front.empty()) moveBtoF();
V res = std::move(m_front.back().second);
m_front.pop_back();
return res;
}
V popBack() {
assert(size());
if (m_back.empty()) moveFtoB();
V res = std::move(m_back.back().second);
m_back.pop_back();
return res;
}
private:
std::vector<std::pair<F, V>> m_front, m_back;
void moveFtoB() {
const usize sz = m_front.size() >> 1;
std::vector<V> tmp(sz);
for (usize i = 0 ; i < sz ; i++) {
tmp[i] = m_front.back().second;
m_front.pop_back();
}
while (m_front.size()) {
pushBack(m_front.back().second);
m_front.pop_back();
}
for (usize i = sz ; i-- ; ) {
pushFront(tmp[i]);
}
}
void moveBtoF() {
const usize sz = m_back.size() >> 1;
std::vector<V> tmp(sz);
for (usize i = 0 ; i < sz ; i++) {
tmp[i] = m_back.back().second;
m_back.pop_back();
}
while (m_back.size()) {
pushFront(m_back.back().second);
m_back.pop_back();
}
for (usize i = sz ; i-- ; ) {
pushBack(tmp[i]);
}
}
};
} // namespace zawa
#line 10 "Test/CF/EC171-F.test.cpp"
#include <algorithm>
#include <array>
#line 14 "Test/CF/EC171-F.test.cpp"
#include <iostream>
#line 16 "Test/CF/EC171-F.test.cpp"
const int P = 2000;
struct S {
using Element = std::pair<int, int>; // {p, t}
using Fold = struct {
using Element = std::vector<int>;
static Element identity() {
Element res(P + 1, -1);
res[0] = 0;
return res;
}
};
using F = typename Fold::Element;
static F convert(Element v) {
F res = Fold::identity();
res[v.first] = v.second;
return res;
}
static F pushBack(F dp, Element v) {
for (int i = P ; i >= v.first ; i--) if (dp[i - v.first] != -1) {
dp[i] = std::max(dp[i], dp[i - v.first] + v.second);
}
return dp;
}
static F pushFront(F dp, Element v) {
for (int i = P ; i >= v.first ; i--) if (dp[i - v.first] != -1) {
dp[i] = std::max(dp[i], dp[i - v.first] + v.second);
}
return dp;
}
};
int Q, T[30030], x[30030], p[30030], t[30030], ans[30030], pop_id[30030], SIZE = 1;
std::vector<std::pair<int, int>> g[30030];
std::vector<int> need[30030];
int make_node(int par, int id) {
g[par].push_back({SIZE, id});
return SIZE++;
}
zawa::FoldableDeque<S> deq;
void dfs(int v) {
if (need[v].size()) {
auto [dp, ep] = deq.get();
for (int i = 1 ; i <= P ; i++) {
dp[i] = std::max(dp[i], dp[i - 1]);
ep[i] = std::max(ep[i], ep[i - 1]);
}
for (int id : need[v]) {
ans[id] = 0;
for (int i = 0 ; i <= p[id] ; i++) ans[id] = std::max(ans[id], dp[i] + ep[p[id] - i]);
}
}
for (auto [x, id] : g[v]) {
if (T[id] == 2) {
deq.pushBack({p[id], t[id]});
dfs(x);
deq.popBack();
}
else if (T[id] == 3) {
const auto ice = deq.popFront();
dfs(x);
deq.pushFront(ice);
}
else assert(false);
}
}
void solve() {
std::cin >> Q;
std::vector<int> cur(1);
for (int i = 0 ; i < Q ; i++) {
ans[i] = -1;
std::cin >> T[i] >> x[i];
x[i]--;
if (T[i] == 1) {
cur.push_back(cur[x[i]]);
}
else if (T[i] == 2) {
std::cin >> p[i] >> t[i];
cur[x[i]] = make_node(cur[x[i]], i);
}
else if (T[i] == 3) {
const int v = cur[x[i]];
if (pop_id[v] == 0) {
pop_id[v] = cur[x[i]] = make_node(v, i);
}
else {
cur[x[i]] = pop_id[v];
}
}
else if (T[i] == 4) {
std::cin >> p[i];
need[cur[x[i]]].push_back(i);
}
else assert(false);
}
dfs(0);
for (int i = 0 ; i < Q ; i++) if (ans[i] != -1) std::cout << ans[i] << '\n';
}
int main() {
#ifdef ONLINE_JUDGE
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
solve();
#else
std::cout << "Hello World\n";
#endif
}