"Gold" Midas touch dfs

When prompted Game: Magic and wealth value initial value 0

Title Description

Pa autumn Li mastered the metallic magic

She decided to pick up some of the stones, cast magic Midas touch

Pa autumn Li n to pick up stones in a row, and some of the stones point decided to gold

For the i stones, if it into gold, will increase the wealth ai, bi consumed magic (Incidentally, even if not the magic value, can operate, the operation of magic value zero)

Otherwise, Pa autumn Li ci magic will return, but to reduce the di wealth (wealth value Similarly, you can reduce unlimited)

Pa autumn Li would like to know, in order to operate in the order of 1-n every stone, how decisions are made, you can make your own final value of the maximum income

Only need to output the maximum benefit

Return value = wealth value * Mana

(Hint: do not value becomes negative, that is, any time, if the value is less than 0, it immediately becomes 0)

Enter a description:

A first line integer n
Next n lines of four numbers, representing the corresponding stone a, b, c, d values

Output Description:

An integer that represents the answer

Entry 

2
1926 817 2003 627
1949 1001 1234 4321

Export

1952898

1≤n≤15,0≤ai, bi, ci, di≤1,000,000

Small amount of data, dfs can

Why is the game I did not write it? ? ?

#include <bits/stdc++.h>
using namespace std;
const int N = 20;
typedef long long ll;
ll a[N], b[N], c[N], d[N];
int n;
ll dfs(int number, ll money, ll mofa){
	if(number == n + 1){
		return money * mofa;
	}
	ll temp = mofa - b[number];
	ll res = dfs(number + 1, money + a[number], temp < 0 ? 0 : temp);
	temp = money - d[number];
	res = max(res, dfs(number + 1, temp < 0 ? 0 : temp, mofa + c[number]));
	return res;
}
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++){
		scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
	}
	ll res = dfs(1, 0, 0);
	printf("%lld\n", res);
	return 0;
}

 

Published 143 original articles · won praise 11 · views 8209

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/103246330
dfs