# CIRMERGE - Subtask 2 runs but 1 and 3 fails, can someone plz help me understand this?

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
unsigned long long int val;
struct node *next;
}node;
void delet(unsigned long long int l)
{
unsigned long long int copy=l;
while(l>1)
{
q=q->next;
l–;
}
l=copy;
if(!l)
{
q=q->next;
}
else if(q->next==tail)
{
tail=q;
}
else
{
q2=q;
q=q->next;
q2->next=q->next;
}
}
unsigned long long int *min(unsigned long long int a[],int l,unsigned long long int mm[])
{
unsigned long long int b[l];
mm[0]=a[0]+a[1];
mm[1]=0;
for(int i=0;i<l;i++)
{
b[i]=a[i]+a[(i+1)%l];
if(mm[0]>b[i])
{
mm[0]=b[i];
mm[1]=i;
}
}
return mm;
}
void replace(unsigned long long int x,unsigned long long int y)
{
while(y>0)
{
q=q->next;
y–;
}
q->val=x;
}
void copy(unsigned long long int a[])
{
int i=0;
{
a[i++]=q->val;
q=q->next;
}
a[i]=q->val;
}
void create(unsigned long long int x,int l)
{
node p=(node)malloc(sizeof(node));
p->val=x;
if(!l)
{
tail=p;
}
else
{
tail->next=p;
tail=p;
}
}
int main()
{
int c;
scanf("%d",&c);
while(c>0)
{
int l;
unsigned long long int x,pen=0;
scanf("%d",&l);
for(int i=0;i<l;i++)
{
scanf("%llu",&x);
create(x,i);
}
while(l>1)
{
unsigned long long int arr[l],m[2];
copy(arr);
min(arr,l,m);
pen+=m[0];
replace(m[0],m[1]);
delet((m[1]+1)%l);
l–;
}
printf("%llu\n",pen);
c–;
}
return 0;
}

1 Like

I haven’t studied your code yet, but Sub-task 2 in this problem can be solved using Greedy approach. So I assume that you have applied greedy approach,instead of Dynamic programming. So use DP instead of Greedy.