#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 10005;
typedef pair <int, int> PT;
int N;
int K[505];

int main()
{
	int k;
	int t = 0;
	vector <PT> a;
	vector <pair <PT, PT> > v;

	scanf("%d", &N);

	for (int i = 0; i < N; i++)
	{
		scanf("%d", K + i);
		t += K[i];
	}

	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < K[i]; j++)
		{
			scanf("%d", &k);
			a.push_back(PT(k, i));
		}
	}

	for (int i = 0; i < t; i++)
	{
		scanf("%d", &k);
		v.push_back(make_pair(PT(-k, a[i].first), PT(i + 1, a[i].second)));
	}

	sort(v.begin(), v.end());

	long long ans = 0;
	long long curt = 0;
	vector <int> av;

	for (int i = 0; i < v.size(); i++)
	{
		pair <PT, PT> p = v[i];
		if (p.second.second == -1) continue;

		curt += p.first.second;
		ans += curt * -p.first.first;
		av.push_back(p.second.first);

		for (int j = i + 1; j < v.size(); j++)
		{
			if (v[j].second.second == p.second.second)
			{
				curt += v[j].first.second;
				ans += curt * -v[j].first.first;
				av.push_back(v[j].second.first);
				v[j].second.second = -1;
			}
		}
	}

	cout << ans << endl;

	for (int i = 0; i < av.size(); i++)
	{
		printf("%d ", av[i]);
	}

	puts("");

	return 0;
}
