Problem Description

Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this task has a difficulty level yi. The machine whose level below this task’s level yi cannot complete this task. If the company completes this task, they will get (500xi+2yi) dollars.
The company has n machines. Each machine has a maximum working time and a level. If the time for the task is more than the maximum working time of the machine, the machine can not complete this task. Each machine can only complete a task one day. Each task can only be completed by one machine.
The company hopes to maximize the number of the tasks which they can complete today. If there are multiple solutions, they hopes to make the money maximum.

Input

The input contains several test cases.
The first line contains two integers N and M. N is the number of the machines.M is the number of tasks(1 < =N <= 100000,1<=M<=100000).
The following N lines each contains two integers xi(0<xi<1440),yi(0=<yi<=100).xi is the maximum time the machine can work.yi is the level of the machine.
The following M lines each contains two integers xi(0<xi<1440),yi(0=<yi<=100).xi is the time we need to complete the task.yi is the level of the task.

Output

For each test case, output two integers, the maximum number of the tasks which the company can complete today and the money they will get.

Sample Input

1 2
100 3
100 2
100 1

Sample Output

1 50004

Source Code:

#include<iostream>
#include<algorithm>

using namespace std;

struct Data{
    int minute;//时间  
    int level;//等级 
}mach[100001],task[100001]; 

/*时间最后与500相乘,比等级的权重更大,
优先按时间降序,相同时间下按等级降序。*/
bool cmp(Data a,Data b){
    if(a.minute!=b.minute)
        return a.minute>b.minute;
    else
        return a.level>b.level; 
}

int main(){

    int machNum,taskNum;//机器数,任务数 
    int i,j,k,count;//count记录所用机器数 
    long long money;//总钱数 

    while(~scanf("%d%d",&machNum,&taskNum)){

        int level[101]={0};//level数组记录各等级机器的动态数量 
        for(i=0;i<machNum;i++)
            cin >> mach[i].minute >> mach[i].level;
        for(i=0;i<taskNum;i++)
            cin >> task[i].minute >> task[i].level;

        sort(mach,mach+machNum,cmp);
        sort(task,task+taskNum,cmp);

        count=money=0;
        /*本题需要考虑2个最优子过程:任务数最多,总价值最多*/ 
        for(i=0,j=0;i<taskNum;i++){//从用时最长的任务开始分配机器 
            while(j<machNum&&mach[j].minute>=task[i].minute){//找到所有满足时间要求的机器 
                level[mach[j].level]++;//该等级可用的机器数+1 
                j++;
            }
            /*在满足时间条件的情况下,应该选择满足等级条件并且等级最小的机器, 
            这样可以留出等级更高的机器安排给其它的任务,从而完成更多的任务。*/ 
            for(k=task[i].level;k<=100;k++){//寻找满足等级要求的等级最小的机器 
                if(level[k]){//如果存在k级的机器 
                    count++; 
                    money+=500*task[i].minute+2*task[i].level;
                    level[k]--;//占用一个k级的机器
                    break;
                }
            }
        }
        cout << count << " " << money << endl;
    }

    return 0;
}