Problem:
ICPC Ranking

Your mission in this problem is to write a program
which, given the submission log of an ICPC (International Collegiate Programming Contest),
determines team rankings.

The log is a sequence of records of program submission
in the order of submission.
A record has four fields: elapsed time, team number,
problem number, and judgment.
The elapsed time is the time elapsed from the beginning of the contest
to the submission.
The judgment field tells whether the submitted program was
correct or incorrect, and when incorrect,
what kind of an error was found.

The team ranking is determined according to the following rules.
Note that the rule set shown here is one used in the real ICPC World Finals and Regionals,
with some detail rules omitted for simplification.

Teams that solved more problems are ranked higher.

Among teams that solve the same number of problems,
ones with smaller total consumed time are ranked higher.

If two or more teams solved the same number of problems, and their
total consumed times are the same, they are ranked the same.

The total consumed time is the sum of the consumed time for each problem solved.
The consumed time for a solved problem is the elapsed time
of the accepted submission plus 20 penalty minutes for every previously rejected submission for that problem.

If a team did not solve a problem, the consumed time for the problem is zero, and thus even if there are several incorrect submissions, no penalty is given.

You can assume that a team never submits a program for a problem
after the correct submission for the same problem.

Input

The input is a sequence of datasets each in the following format.
The last dataset is followed by a line with four zeros.

M T P R

m1 t1 p1 j1

m2 t2 p2 j2

.....

mR tR pR jR

The first line of a dataset contains four integers
M, T, P, and R.
M is the duration of the contest.
T is the number of teams.
P is the number of problems.
R is the number of submission records.
The relations
120 ≤ M ≤ 300,
1 ≤ T ≤ 50,
1 ≤ P ≤ 10,
and 0 ≤ R ≤ 2000
hold for these values.
Each team is assigned a team number between 1 and T, inclusive.
Each problem is assigned a problem number between 1 and P, inclusive.

Each of the following R lines contains a submission record
with four integers
mk, tk,
pk, and jk
(1 ≤ k ≤ R).
mk is the elapsed time.
tk is the team number.
pk is the problem number.
jk is the judgment
(0 means correct, and other values mean incorrect).
The relations
0 ≤ mk ≤ M−1,
1 ≤ tk ≤ T,
1 ≤ pk ≤ P,
and 0 ≤ jk ≤ 10
hold for these values.

The elapsed time fields are rounded off to the nearest minute.

Submission records are given in the order of submission.
Therefore, if i < j,
the i-th submission is done before the j-th submission
(mi ≤ mj).
In some cases, you can determine the ranking of two teams
with a difference less than a minute, by using this fact.
However, such a fact is never used in the team ranking.
Teams are ranked only using time information in minutes.

Output

For each dataset, your program should output team numbers (from 1 to T),
higher ranked teams first.
The separator between two team numbers should be a comma.
When two teams are ranked the same, the separator between them
should be an equal sign.
Teams ranked the same should be listed
in decreasing order of their team numbers.

Sample Input

300 10 8 5
50 5 2 1
70 5 2 0
75 1 1 0
100 3 1 0
150 3 2 0
240 5 5 7
50 1 1 0
60 2 2 0
70 2 3 0
90 1 3 0
120 3 5 0
140 4 1 0
150 2 4 1
180 3 5 4
15 2 2 1
20 2 2 1
25 2 2 0
60 1 1 0
120 5 5 4
15 5 4 1
20 5 4 0
40 1 1 0
40 2 2 0
120 2 3 4
30 1 1 0
40 2 1 0
50 2 2 0
60 1 2 0
120 3 3 2
0 1 1 0
1 2 2 0
300 5 8 0
0 0 0 0

Output for the Sample Input

3,1,5,10=9=8=7=6=4=2
2,1,3,4,5
1,2,3
5=2=1,4=3
2=1
1,2,3
5=4=3=2=1

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharpSample01
{
    struct Team
    {
        public int sum_time;
        public int solve_pro_num;
        public Problem[] pro_data;
        public int team_number;

        public Team(int P, int sum_time, int solve_pro_num, int num)
        {
            pro_data = new Problem[P];
            this.sum_time = sum_time;
            this.solve_pro_num = solve_pro_num;
            team_number = num;
            for(int i = 0; i < P; i++)
            {
                pro_data[i] = new Problem(0, 0, 1);
            }
        }      
    }

    struct Problem
    {
        public int num_submit;
        public int last_solved_time;
        public int result;

        public Problem(int n, int f, int r)
        {
            num_submit = n;
            last_solved_time = f;
            result = r;
        }
    }

    class Contest
    {
        int num_of_pro;
        int num_of_team;
        int open_period;        
        public Team[] team_data;

        public Contest(int M, int T, int P)
        {
            num_of_pro = P;
            num_of_team = T;
            open_period = M;
            CreateTeamStruct(T, P);
        }

        void CreateTeamStruct(int T, int P)
        {
            team_data = new Team[T];
            for(int i = 0; i < T; i++)
            {
                team_data[i] = new Team(P, 0, 0, i + 1);
            }
        }

        //問題の提出回数、結果、最終提出時間、チームの正解回数を更新
        public void Updata_Team_Data(int time, int team, int pro, int result)
        {            
            team_data[team].pro_data[pro].result = result;
            team_data[team].pro_data[pro].num_submit++;
            team_data[team].pro_data[pro].last_solved_time = time;
            if(result == 0)
            {
                team_data[team].solve_pro_num++;
            }
        }

        public void Calc_AllTeam_SumTime()
        {

            for(int i = 0; i < num_of_team; i++)
            {
                for(int j = 0; j < num_of_pro; j++)
                {
                    //解けていたらペナルティーを付加してチームの合計所要時間に加算
                    if(team_data[i].pro_data[j].result == 0)
                    {
                        int sub_num = team_data[i].pro_data[j].num_submit;
                        int last_time = team_data[i].pro_data[j].last_solved_time;
                        team_data[i].sum_time += last_time + 20 * (sub_num - 1);
                    }
                }
            }

        }

        public void Show_Ranking()
        {
            Calc_AllTeam_SumTime();

            var team_data_sorted = team_data.OrderByDescending(x => x.solve_pro_num).ThenBy(x => x.sum_time).ThenByDescending(x => x.team_number).ToArray();

            int old_solve_num = team_data_sorted[0].solve_pro_num;
            int old_sum_time = team_data_sorted[0].sum_time;
            int old_team = team_data_sorted[0].team_number;
            Console.Write(old_team);
            for(int i = 1; i < num_of_team; i++)
            {
                string head_str;

                if(team_data_sorted[i].solve_pro_num == old_solve_num && team_data_sorted[i].sum_time == old_sum_time)
                {
                    head_str = "=";
                }
                else
                {
                    head_str = ",";
                }

                old_solve_num = team_data_sorted[i].solve_pro_num;
                old_sum_time = team_data_sorted[i].sum_time;
                old_team = team_data_sorted[i].team_number;

                Console.Write(head_str + team_data_sorted[i].team_number.ToString());

            }

            Console.WriteLine();

        }
        
    }
    

    class Program
    {
        
        
        static void Main(string[] args)
        {

            while (true)
            {
                string[] input = Console.ReadLine().Split(' ');
                int M = int.Parse(input[0]);
                int T = int.Parse(input[1]);
                int P = int.Parse(input[2]);
                int R = int.Parse(input[3]);

                if(M == 0 && T == 0 && P == 0 && R == 0)
                {
                    break;
                }

                Contest contest = new Contest(M, T, P);

                for(int i = 0; i < R; i++)
                {
                    int[] sub_data = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
                    contest.Updata_Team_Data(sub_data[0], sub_data[1] - 1, sub_data[2] - 1, sub_data[3]);
                }

                contest.Show_Ranking();
              
            }
            
        }
    }
}