#include <bits/stdc++.h>
using namespace std;
// class priority
class priority
{
public:
string name;
int burst;
int prio;
int arvt;
int waiting;
// constructor use
priority()
{
cin >> name >> burst >> prio >> arvt;
}
// show process burst priority arival time
void show()
{
cout << name << " " << burst << " " << prio << " " << arvt << endl;
}
};
int main()
{
int n;
cin >> n;
vector<priority> a(n);
// sorting Arrival Time
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[j].arvt < a[i].arvt)
swap(a[i], a[j]);
}
}
vector<string> pp;
vector<priority> p;
for (int i = 0; i < n; i++)
{
p.push_back(a[i]);
int n2 = p.size();
for (int i = 0; i < n2 - 1; i++)
{
for (int j = i + 1; j < n2; j++)
{
if (p[j].prio < p[i].prio)
swap(p[i], p[j]);
}
}
pp.push_back(p[0].name);
p[0].burst--;
if (p[0].burst == 0)
p.erase(p.begin() + 0);
}
// non-preemptive
while (!p.empty())
{
int n2 = p.size();
pp.push_back(p[0].name);
p[0].burst--;
if (p[0].burst == 0)
p.erase(p.begin() + 0);
}
for (int i = 0; i < pp.size(); i++)
{
cout << pp[i] << " ";
}
cout << endl
<< endl
<< "Waiting Times : " << endl;
// finding waiting times for each process
for (int i = 0; i < n; i++)
{
int ct = 0;
for (int j = 0; j < pp.size(); j++)
{
if (a[i].name == pp[j])
ct = j + 1;
}
a[i].waiting = ct - (a[i].arvt + a[i].burst);
}
// Calculating Avg. Waiting Times
float wt = 0;
for (int i = 0; i < n; i++)
{
cout << a[i].name << " " << a[i].waiting << endl;
wt += 1.0 * a[i].waiting;
}
cout << endl;
cout << "Avg. Waitimg Time = " << wt / n << endl;
return 0;
}