#include <iostream>
#include <vector>
#include "algotester.h"
using namespace std;
#define alpha _alpha
#define beta _beta
#define gamma _gamma
const int BILLION = 1'000'000'000;
struct ComputeUnit
{
long long u, f, d, c, e;
};
struct QueryParams
{
int N;
double tau;
vector<long long> I;
vector<long long> O;
};
int l;
int h;
long long F;
int n;
int m;
double alpha;
double beta;
double gamma;
vector<ComputeUnit> units;
vector<QueryParams> queries;
void read_test(AlgotesterReader& test)
{
l = test.readInt();
h = test.readInt();
F = test.readInt();
alpha = test.readDouble();
beta = test.readDouble();
gamma = test.readDouble();
n = test.readInt();
m = test.readInt();
for (int i = 0; i < n; i++)
{
int u = test.readInt();
long long f = test.readInt() * BILLION;
long long d = test.readInt() * BILLION;
long long c = test.readInt() * BILLION;
long long e = test.readInt() * BILLION;
units.push_back({u, f, d, c, e});
}
for (int j = 0; j < m; j++)
{
int N = test.readInt();
double tau = test.readDouble();
auto I = test.readVectorInt(N);
auto O = test.readVectorInt(N);
queries.push_back({N, tau, I, O});
}
}
vector<int> p, t, b;
vector<vector<int> > g, w;
vector<vector<int> > A;
vector<int> inst;
int P;
void read_user_output(AlgotesterReader& reader)
{
p.resize(n);
t.resize(n);
b.resize(n);
P = 0;
for (int i = 0; i < n; i++)
{
p[i] = reader.readInt(1, units[i].u, "p_i");
t[i] = reader.readInt(1, units[i].u, "t_i");
check(p[i] * t[i] == units[i].u, "p_i * t_i sould be equal to u_i");
b[i] = reader.readInt(1, 1000, "b_i");
P += p[i];
}
for (int i = 0; i < n; i++)
{
for (int x = 0; x < p[i]; x++)
{
inst.push_back(i);
}
}
g.resize(m);
w.resize(m);
A.resize(m);
for (int j = 0; j < m; j++)
{
g[j].resize(queries[j].N);
w[j].resize(queries[j].N);
A[j].resize(P);
for (int r = 0; r < queries[j].N; r++)
{
g[j][r] = reader.readInt(1, P, "g_j^r");
w[j][r] = reader.readInt("w_j^r");
g[j][r]--;
w[j][r]--;
A[j][g[j][r]]++;
}
for (int s = 0; s < P; s++)
{
A[j][s] = (A[j][s] + b[inst[s]] - 1) / b[inst[s]];
}
vector<vector<int> > batch_size(P); // [pipeline_index][batch_index_within_this_pipeline]
for (int s = 0; s < P; s++)
{
batch_size[s].assign(A[j][s], 0);
}
for (int r = 0; r < queries[j].N; r++)
{
check(0 <= w[j][r] && w[j][r] < A[j][g[j][r]], "w_j^r out of range");
batch_size[g[j][r]][w[j][r]]++;
}
for (int s = 0; s < P; s++)
{
for (int x = 0; x < A[j][s] - 1; x++)
{
check(batch_size[s][x] == b[inst[s]], "each batch except the last one has to be complete");
}
if (A[j][s] > 0)
{
check(batch_size[s][A[j][s] - 1] <= b[inst[s]], "the last batch is too large");
}
}
}
}
long long get_max_io(int j)
{
long long res = 0;
for (int r = 0; r < queries[j].N; r++)
{
res = max(res, queries[j].I[r] + queries[j].O[r]);
}
return res;
}
void check_memory_constraints()
{
for (int j = 0; j < m; j++)
{
long long max_io = get_max_io(j);
for (int i = 0; i < n; i++)
{
long long left = units[i].d * t[i];
long long model = 2 * F;
long long vcache = 4 * b[i] * l * h * max_io;
long long right = model + vcache;
check(left >= right, "memory constraint not satisfied");
}
}
}
int main(int argc, char* argv[])
{
//ios::sync_with_stdio(false); cin.tie(0);
auto readers = initChecker(argc, argv);
auto test = readers[0];
auto user = readers[1];
read_test(test);
read_user_output(user);
check_memory_constraints();
return 0;
}