#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <random>

using namespace std;
long long n, m, p;

struct fish
{
    long long x, a;
    fish(): x(0), a(0) {}
};

struct port
{
    long long y, b, c;
    port(): y(0), b(0), c(0) {}
};

vector <fish> t;
vector <port> s;



long long solve()
{

    set <pair <long long, long long> > used, left;
    long long flft = 0, ans = 0, mans = 0;
    long long i = 0, j = 0;
    while (i != n || j != m)
    {
        if (i != n && (j == m || t[i].x < s[j].y))
        {
            flft += t[i].a;
            while (flft > 0 && left.size() > 0)
            {
                long long c = -left.begin()->first, b = left.begin()->second;
                left.erase(left.begin());
                long long sold = min(b, flft);
                ans += sold * c;
                flft -= sold;
                b -= sold;
                used.insert(make_pair(c, sold));
                if (b > 0)
                    left.insert(make_pair(-c, b));
            }
            mans = max(mans, ans - p * t[i].x);
            ++i;
        }
        else
        {
            long long leftb = s[j].b, leftc = s[j].c;
            if (flft > 0)
            {
                long long sold = min(leftb, flft);
                ans += sold * leftc;
                flft -= sold;
                leftb -= sold;
            }
            while (leftb > 0 && used.size() > 0 && used.begin()->first < s[j].c)
            {
                long long c = used.begin()->first, b = used.begin()->second;
                used.erase(used.begin());
                long long sold = min(leftb, b);
                b -= sold;
                leftb -= sold;
                ans += sold * (s[j].c - c);
                left.insert(make_pair(-c, sold));
                if (b > 0)
                    used.insert(make_pair(c, b));
            }
            if (leftb > 0)
                left.insert(make_pair(-s[j].c, leftb));
            if (s[j].b != leftb)
                used.insert(make_pair(s[j].c, s[j].b - leftb));


            mans = max(mans, ans - p * s[j].y);
            j++;
        }
    }
    return mans;
}



bool cmps(port q, port w)
{
    return q.c > w.c;
}


int main()
{
    freopen("fisher.in", "r", stdin);
    freopen("fisher.out", "w", stdout);
    //cin >> n >> m >> p;


    /*for (long long i = 0; i < n; ++i)
        cin >> t[i].x >> t[i].a;
    for (long long i = 0; i < m; ++i)
        cin >> s[i].y >> s[i].b >> s[i].c;
    */
    for (int _ = 0; _ < 100000; ++_)
    {
        m = rand() % 15 + 1;
        n = rand() % 15 + 1;

        t.resize(n);
        s.resize(m);
        t[0].x = rand() % 100;
        t[0].a = rand() % 1500;
        for (int i = 1; i < n; ++i)
            t[i].a = rand() % 1000, t[i].x = t[i - 1].x + rand() % 100;
        s[0].y = rand() % 100;
        t[0].a = rand() % 1500;
        for (int i = 1; i < m; ++i)
            s[i].b = rand(), s[i].c = rand(), s[i].y = s[i - 1].y + rand() % 100;
        long long first = solve();

            long long am = 0;
            for (int i = 0; i < n; ++i)
                am += t[i].a;
            sort(s.begin(), s.end(), cmps);
            long long ans = 0;
            int i = 0;
            while (i < m && am > 0)
            {
                int sold = min(1ll * s[i].b, am);
                ans += 1ll * s[i].c * sold;
                am -= sold;
                i++;
            }

        if (first != ans)
        {
            cout << first << ' ' << ans << endl;
            for (int i = 0; i < n; ++i)
                cout << t[i].x << ' ' << t[i].a << endl;
            for (int i = 0; i < m; ++i)
                cout << s[i].y << ' ' << s[i].b << ' ' << s[i].c << endl;
            return 0;
        }
    }
}
