/*
题目:C - Plane Ticket Pricing
链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=78470#problem/C
题意:给你n个座位,m天剩余,接下来m+1行代表剩余i天,对于每天有k种价格的机票,你必须挑选一种作为你当日的机票价,
每种机票价对应当日会销售掉的票数,不够就全卖掉,问你最大的销售利润(相等就取第一天票价小的方案),并且输出该种方法第一天机票的定价。
分析:朴素dp。
dp[w][j]:=第w周卖出j张票的最大利润
path[w][j]:=dp[w][j]对应方案的第一周的卖出的票的价格
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 305;
typedef long long LL;
const int INF = 0xfffffff;
int input()
{
int a;
scanf("%d", &a);
return a;
}
/*
dp[w][j]:=第w周卖出j张票的最大利润
path[w][j]:=dp[w][j]对应方案的第一周的卖出的票的价格
*/
int dp[maxn][maxn],path[maxn][maxn];
int num[maxn], cost[maxn][maxn], ticket[maxn][maxn];
int N, W;
int main()
{
#ifndef ONLINE_JUDGE
freopen("f:\\input.txt", "r", stdin);
#endif
while (~scanf("%d%d", &N, &W))
{
for (int w = 1; w <= W + 1; w++)
{
num[w] = input();
for (int j = 1; j <= num[w]; j++) cost[w][j] = input();
for (int j = 1; j <= num[w]; j++) ticket[w][j] = input();
}
memset(dp, -1, sizeof(dp));
for (int i = 1; i <= num[1]; i++)
{
int ret = min(N, ticket[1][i]);
dp[1][ret] = cost[1][i] * ret;
path[1][ret] = cost[1][i];
}
for (int w = 2; w <= W + 1; w++)//第w周
{
for (int j = 0; j <= N; j++)//old:卖出j张票
{
if (dp[w - 1][j] == -1) continue;
for (int k = 1; k <= num[w]; k++)//选择第k张票
{
int ret = min(N, j + ticket[w][k]);
if (dp[w][ret]<dp[w - 1][j] + (ret-j)*cost[w][k] || (dp[w][ret] == dp[w - 1][j] + (ret-j)*cost[w][k] && path[w][ret]>cost[w][k]))
{
dp[w][ret] = dp[w - 1][j] + (ret-j)*cost[w][k];
path[w][ret] = path[w-1][j];
}
}
}
}
int ans1=-1, ans2;
for (int i = 0; i <= N; i++)
{
if (ans1 < dp[W + 1][i])
{
ans1 = dp[W + 1][i];
ans2 = path[W + 1][i];
}
}
printf("%d\n%d\n", ans1, ans2);
//printf("%d\n%d\n", dp[W+1][N],path[W+1][N]);
}
return 0;
}