Problem:
Score : 100 points

Problem Statement

We have A apples and P pieces of apple.

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

Find the maximum number of apple pies we can make with what we have now.

Constraints

All values in input are integers.

0 \leq A, P \leq 100

Input

Input is given from Standard Input in the following format:

A P

Output

Print the maximum number of apple pies we can make with what we have.

Sample Input 1

1 3

Sample Output 1

3

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.

Sample Input 2

0 1

Sample Output 2

0

We cannot make an apple pie in this case, unfortunately.

Sample Input 3

32 21

Sample Output 3

58

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <string.h>

int V[50];

long long int MAX(long long int a, long long int b, long long int c) {
	if (a >= b && a >= c) {
		return a;
	}
	else if (b >= a && b >= c) {
		return b;
	}
	else {
		return c;
	}
}

long long int suteru(int sum, int k, int fu[50], int cnt) {
	int i;
	for (i = cnt - 1; i >= 0 && k != 0; i--) {
		sum = sum - fu[i];
		k--;
	}
	return sum;
}

long long int rec(long long int sum, int left, int right, int value, int k, int* fu, int cnt) {
	int i, j;
	int temp[50];

	for (j = cnt - 1; j >= 0; j--) {
		temp[j] = fu[j];
	}

	if (value < 0) {
		for (i = cnt; i > 0 && temp[i - 1] < value; i--) {
			temp[i] = temp[i - 1];
		}		
		temp[i] = value;
		cnt++;
	}

	sum = sum + value;

	if (k == 0 || left > right) {
		return suteru(sum, k, temp, cnt);
	}

	return MAX(suteru(sum, k, temp, cnt), rec(sum, left + 1, right, V[left], k - 1, temp, cnt), rec(sum, left, right - 1, V[right], k - 1, temp, cnt));
}

int main() {
	int N, K, i, fu[50], cnt = 0;
	long long int vmax = 0;

	memset(fu, 0, sizeof(fu));

	scanf("%d %d", &N, &K);
	for (i = 0; i < N; i++) {
		scanf("%d", &V[i]);
	}

	vmax = MAX(0, rec(0, 1, N - 1, V[0], K - 1, fu, cnt), rec(0, 0, N - 2, V[N - 1], K - 1, fu, cnt));

	printf("%lld\n", vmax);

	return 0;
}