#include <iostream>
#include <fstream>
#include <iomanip>
#define R 12
#define C 7
using namespace std;

void displayLine()
{
	int p = 0;
	while (p < 52)
	{
		cout << "-";
		p++;
	}
	cout << endl;
}

int findIndWinner(int total[], int &a, int &b)
{
	int high = -9999;
	for(int i = 1; i < 12; i++)
	{
		if(high < total[i])
		{
			high = total[i];
			a = i;
			if(a < 4)
				b = 1;
			if(a < 8)
				b = 2;
			if(a < 12)
				b = 3;
		}
	}
	return high;
}



int findTeamWinner(int totalScore[], int &c)
{
	int high = -9999;
	for (int i = 0; i < 3; i++)
	{
		if(totalScore[i] > high)
			high = totalScore[i];
			c = i;
	}
	
	return high;
}

int main()
{
	int marks[R][C], a, b, c;
	int total[R], totalScore[3] = {0,0,0};
	int highestScore, winner;
	ifstream inp;
	
	inp.open("input2.txt");
	
	if (!inp.is_open())
	{
		cout << "Sorry! An error has occured" << endl << "Please Try Again!!";
		exit(0);
	}
	
	while (!inp.eof())
	{
		for(int i = 0; i < R; i++)
		{
			for (int j = 0; j < C; j++)
			{
				inp >> marks[i][j];
			}
		}
	}	

	for(int i = 0; i < R; i++)
	{
		total[i] = 0;
		for (int j = 2; j < C; j++)
		{
			total[i] += marks[i][j];
		}
	}	
	
	for(int i = 0; i < 4; i++)
	{
		totalScore[0] += total[i];
	}

	for(int i = 4; i < 8; i++)
	{
		totalScore[1] += total[i];		
	}


	for(int i = 8; i < 12; i++)
	{
		totalScore[2] += total[i];	
	}
	

	highestScore = findIndWinner(total, a, b);
	winner = findTeamWinner(totalScore, c);
	
	
	displayLine();
	cout << "Id\tE1\tE2\tE3\tE4\tE5" << setw(10) << "Total" << endl;
	displayLine();
	
	cout << "TEAM " << marks[0][0] << endl;
	for(int i = 0; i < 4; i++)
	{
		for (int j = 1; j < C; j++)
		{
			cout << marks[i][j] << "\t";
		}
		cout << total[i] << endl;
	}
	
	cout << "TOTAL" << setw(46) << totalScore[0] << endl;
	displayLine();
	
	cout << "TEAM " << marks[4][0] << endl;
	for(int i = 4; i < 8; i++)
	{
		for (int j = 1; j < C; j++)
		{
			cout << marks[i][j] << "\t";
		}
		cout << total[i] << endl;
	}
	
	cout << "TOTAL" << setw(46) << totalScore[1] << endl;
	displayLine();
	
	cout << "TEAM " << marks[8][0] << endl;
	for(int i = 8; i < 12; i++)
	{
		for (int j = 1; j < C; j++)
		{
			cout << marks[i][j] << "\t";
		}
		cout << total[i] << endl;
	}

	cout << "TOTAL" << setw(46) << totalScore[2] << endl;	
	displayLine();
	
cout << "Winner for Individual Category: " << marks[a][1] <<" (Team " << b << ")" << endl;
cout << "Winner for Group Category: " << "Team " << c << " (Score : " << winner << ")"<< endl;

return 0;	

}
