Codeforces:Problem in Taxi(158B)

My code is not working for 3 3 2 testcase. Please help me in figuring out why should the answer for this tc be 2 and how can I modify my code to get it accepted.

THANKS IN ADVANCE…

//JUST LIKE ANIMALS !!!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
ll n,i,child,res=0,tmp=0;cin>>n;
for(i=0;i<n;i++){
    cin>>child;
    tmp+=child;
    }
    cout<<(tmp/4)+(tmp%4?1:0);


return 0;

}

For 3,3,2 the answer will be 3. You can put group of 3 people in taxi 1, another group of 3 people in taxi 2 and last group of people 2 in taxi 3. The optimum way of doing this problem is to take one taxi for 4 number of people in a group then take n/2 taxi for 2 number of people in a group where n is number of groups having 2 people and then combine group having number of people equal to 1 and 3. Check for remaining groups.

Similar problem - http://www.spoj.com/problems/EGYPIZZA/

1 Like