Found the bug. When k=1 I returned 0 irrespective of whether it is going forward or backward.
Found the bug. When k=1 I returned 0 irrespective of whether it is going forward or backward.
1 testcase is failing in subtask1 of this code. Can someone help me in understanding why it is failing?
#include <bits/stdc++.h>using namespace std;int back[1000005];int front[1000005];int b (int arr[], int n, int k){if(k==1)return 0;if(back[k]!=INT_MIN)return back[k];int a1 = arr[k-1]+b(arr,n,k-1);int a2 = INT_MIN;if(k>=3)a2 = arr[k-2]+b(arr,n,k-2);int ans = max(a1,a2);return back[k]=ans;}int f(int arr[], int n, int k){if(k==1)return 0;if(front[k]!=INT_MIN){return front[k];}int a1 = arr[k-1]+b(arr,n,k-1);int a2 = INT_MIN, a3= INT_MIN, a4=INT_MIN;if(k>=3)a2 = arr[k-2]+b(arr,n,k-2);int ans = max(a1,a2);if(k<n){a3 = arr[k+1]+f(arr,n,k+1);}ans = max(ans,a3);if(k<(n-1)){a4 = arr[k+2]+f(arr,n,k+2);}ans =max(ans,a4);//cout<<a1<<" "<<a2<<" "<<a3<<" "<<a4<<endl;return front[k]=ans;}int main(){int n,k;cin>>n>>k;int arr[n+1];for(int i=1;i<=n;i++){cin>>arr[i];}if(n==1){cout<<0<<endl;}else{for(int i=0;i<1000005;i++){ front[i]=INT_MIN;back[i]= INT_MIN;}//cout<<back[0]<<endl;cout<<f(arr,n,k)<<endl;}}
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
if(n==1||n==2)
cout<<0<<endl;
else{
int dp[2][n+1];
If 1st person is sent first then the time taken will be a1+max(d1,a2)+d2
If 2nd person is sent first then the time taken will be a2+max(d2,a1)+d1
Shouldn't these two values be compared in deciding who goes first? Can someone verify this logic and tell if there is anything wrong with it?
My 2nd, 4th and 7th task is failing. Can someone please help?
#include <bits/stdc++.h>using namespace std;int main(){int n,x,y;cin>>n>>x>>y;pair<int,int> p[n];for(int i=0;i<n;i++){cin>>p[i].first;cin>>p[i].second;}int v[x],w[y];for(int i=0;i<x;i++){cin>>v[i];}for(int j=0;j<y;j++){cin>>w[j];}sort(v,v+x);sort(w,w+y);int ans=INT_MAX;for(int i=0;i<n;i++){int sum=p[i].second-p[i].first;