求助记忆化搜索!!
查看原帖
求助记忆化搜索!!
519573
Daniel_yao楼主2022/10/8 15:43
#include <bits/stdc++.h>
#define ll long long
#define H 19260817
#define rint register int
#define For(i,l,r) for(rint i=l;i<=r;++i)
#define FOR(i,r,l) for(rint i=r;i>=l;--i)
#define MOD 1000003
#define mod 1000000007

using namespace std;

inline int read() {
  rint x=0,f=1;char ch=getchar();
  while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
  while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
  return x*f;
}

void print(int x){
  if(x<0){putchar('-');x=-x;}
  if(x>9){print(x/10);putchar(x%10+'0');}
  else putchar(x+'0');
  return;
}

const int N = 200100; 

int n = read(), a[N], ans, f[N];

int dfs(int x) {
	if(f[x] != -1) return f[x];
	if(x == n + 1) {
		return 0;
	} 
	return f[x] = max(dfs(x + 1) + a[x], a[x]);
}

signed main() {
	memset(f, -1, sizeof f);
	For(i,1,n) a[i] = read();
	cout << dfs(1) << '\n';
	return 0;
}

2022/10/8 15:43
加载中...