Help me to debug the python code

Правка en3, от _trie_again, 2023-05-05 22:40:07

Hi everyone, i have started learning python . So i want to know what is the bug in my program: The same code 204659837 in c++ works fine, but in python its not even working on sample tests. Please enlighten me. My python code:

INF=2e18

def dfs(idx,cnt)->int:
	if(idx>=n):
		if(cnt!=3):
			return -INF
		return 0
	ans=dp[idx][cnt]
	if(vis[idx][cnt]):
		return ans
	vis[idx][cnt]=1
	ans=0
	ans=max(ans,dfs(idx+1,cnt))
	if(cnt<3):
		if(cnt==0):
			ans=max(ans,dfs(idx+1,cnt+1)+(a[idx]+idx+1));
		elif(cnt==1):
			ans=max(ans,dfs(idx+1,cnt+1)+a[idx]);
		elif(cnt==2):
			ans=max(ans,dfs(idx+1,cnt+1)+a[idx]-(idx+1));
	dp[idx][cnt]=ans
	return ans

for i in range(int(input())):
	n=int(input())
	a=list(map(int,input().split()))
	dp=[[-1]*5 for i in range(n+5)]
	vis=[[0]*5 for i in range(n+5)]
	print(dfs(0,0))

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en3 Английский _trie_again 2023-05-05 22:40:07 0 (published)
en2 Английский _trie_again 2023-05-05 22:39:41 46
en1 Английский _trie_again 2023-05-05 22:38:19 942 Initial revision (saved to drafts)