// Including required header files
#include<iostream>
#include<fstream>
using namespace std;
 
// Function to display line using loop
void displayLine()
{
    for(int i = 0; i<52; i++)
        cout << '-';
    cout << endl;
}
 
// Function to find and display Individual winner
void findIndWinner(int individual_score[], int marks[][7])
{
    int maxi = 0, id, team;
    for(int i = 0; i<12; i++)
    {
        if(individual_score[i] > maxi)
        {
            maxi = individual_score[i];
            id = marks[i][1];
            team = i/4 + 1;
        }
    }
    cout << "Winner for Individual Category: " << id << " (Team " << team << ")" << endl;
}
 
// Function to find and display Team Winner
void findTeamWinner(int teams_score[])
{
    int maxi = 0, team;
    for(int i = 0; i<3; i++)
    {
        if(teams_score[i] > maxi)
        {
            maxi = teams_score[i];
            team = i+1;
        }
    }
    cout << "Winner for Group Category: Team " << team << " (Score = " << maxi << ")" << endl;
}
 
// Driver Code
int main()
{
    int marks[12][7];               // Marks array
    int teams_score[3] = {0};       // Teams scores array
    int individual_score[12] = {0}; // Individual scores array
    ifstream fin("input.txt");      // Opening input file
    if(fin.is_open())
    {
        for(int i = 0; i<12; i++)
        {
            // Reading input
            for(int j = 0; j<7; j++)
                fin >> marks[i][j];
        }
        // Calculating total individual marks
        for(int i = 0; i<12; i++)
        {
            for(int j = 2; j<7; j++)
                individual_score[i] += marks[i][j]; 
        }
        // Calculating total team marks
        for(int i = 0; i<12; i++)
        {
            teams_score[i/4] += individual_score[i];
        }
        
        // Displaying ouput
        displayLine();
        cout << "Id\tE1\tE2\tE3\tE4\tE5\tTotal" << endl;
        displayLine();
        for(int i = 0; i<3; i++)
        {
            cout << "TEAM " << i + 1 << endl;
            for(int j = 4 * i; j< 4 * i + 4; j++)
            {
                for(int k = 1; k<7; k++)
                    cout << marks[j][k] << "\t";
                cout << individual_score[j] << endl;
            }
            cout << "TOTAL\t\t\t\t\t\t" << teams_score[i] << endl;
            displayLine();
        }
        cout << endl;
        findIndWinner(individual_score, marks);
        findTeamWinner(teams_score);
 
        /*
        Opening the output file and writing the output again to file
        */
        freopen ("output.txt","w",stdout);
        displayLine();
        cout << "Id\tE1\tE2\tE3\tE4\tE5\tTotal" << endl;
        displayLine();
        for(int i = 0; i<3; i++)
        {
            cout << "TEAM " << i + 1 << endl;
            for(int j = 4 * i; j< 4 * i + 4; j++)
            {
                for(int k = 1; k<7; k++)
                    cout << marks[j][k] << "\t";
                cout << individual_score[j] << endl;
            }
            cout << "TOTAL\t\t\t\t\t\t" << teams_score[i] << endl;
            displayLine();
        }
        cout << endl;
        findIndWinner(individual_score, marks);
        findTeamWinner(teams_score);
    }
    else 
    {
        // Error when input file could not be opened
        cout << "Sorry, input file doesn't exist!" << endl;
    }
    return 0;
}
