using System;

namespace Petlja
{
    class Program
    {

        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int m = int.Parse(Console.ReadLine());
            int[] a = new int[n], b = new int[n], c = new int[m], d = new int[m];
            int[,] k = new int[1, m];
            for (int i = 0; i < n; i++)
            {
                string[] s = Console.ReadLine().Split();
                a[i] = int.Parse(s[0]);
                b[i] = int.Parse(s[1]);
            }
            for (int i = 0; i < m; i++)
            {
                string[] s = Console.ReadLine().Split();
                c[i] = int.Parse(s[0]);
                d[i] = int.Parse(s[1]);
            }
            for (int i = 0; i < m; i++)
            {
                k[0, i] = c[i];
                b[0] -= c[i];
            }
            int su = 0;
            for (int i = 0; b[0] > 0 && i < m; i++)
            {
                if (d[i] - c[i] > b[0])
                {
                    k[0, i] += b[0];
                    b[0] = 0;
                }
                else
                {
                    k[0, i] = d[i];
                    b[0] -= d[i] - c[i];
                }
                su += k[0, i];
            }
            Console.WriteLine(su);
            Console.WriteLine(m);
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    Console.Write("{0} ", k[0, j]);
                }
            }

        }
    }
}