Runtime error on problem name princess farida on spoj.

Revision en1, by maverick06, 2020-11-14 14:45:26

I had been trying to solve these problem from last three hours but I am not able to find where my code is giving tle . Please help to find where I am wrong.

Here is the problem: https://www.spoj.com/problems/FARIDA/

Here is my code:

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

int main() {
	// your code goes here
	ll t;
	cin>>t;
	ll k=1;
	while(t--)
	{
		ll n;
		cin>>n;
		vector<ll>arr;
		for(int i=0;i<n;i++)
		{
			ll num;
			cin>>num;
			arr.push_back(num);
		}
		ll dp[10000];
		dp[0] = 0;
		dp[1]=arr[0];
		if(n == 0)
		{
			printf("Case %d: %d\n",k,0);
		}
		if(n == 1)
		{
			printf("Case %d: %lld\n",k,dp[1]);
			continue;
		}
		dp[2] = max(arr[1],arr[2]);
		for(ll i=3;i<=n;i++)
		{
			dp[i] = max(arr[i-1]+dp[i-2],dp[i-1]);
		}
	   printf("Case %d: %lld\n",k,dp[n]);
	   k++;
	}
	
	return 0;
}

Thanks :)

Tags #dp, #cpp, #spoj, 0/1 knapsack

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English maverick06 2020-11-14 14:45:26 972 Initial revision (published)