Codeforces Round #383 (Div. 2), problem: (D) Arpa’s weak amphitheater and Mehrdad’s valuable Hoses Solution in C/C++

#include<stdio.h>
int n, m, maxW, p[2000], w[2000], b[2000], prev[2000], next[2000], gb[2000],gw[2000];
int root(int i)
{
    if(p[i]!=i)
	p[i] = root(p[i]);
    return p[i];
}
int main()
{
    scanf("%i%i%i",&n,&m,&maxW);
    for(int i=0;i<n;i++)
	scanf("%i",w+i);
    for(int i=0;i<n;i++)
	scanf("%i",b+i);
    int x, y;
    for(int i=0;i<n;i++)
	p[i]=i;
    for(int i=0;i<m;i++)
    {
	scanf("%i%i",&x,&y);
	x--;
	y--;
	p[root(x)]=root(y);
    }
    prev[0] = 0;
    for(int v=1;v<=maxW;v++)
	prev[v] = -1;
    for(int i=0;i<n;i++)
	if(p[i]==i)
	{
	    int k = 0;
	    for(int j=0;j<n;j++)
		if(root(j)==i)
		{
		    gb[k]=b[j];
		    gw[k]=w[j];
		    k++;
		}

	    /* for(int j=0;j<k;j++) */
	    /* 	printf("%i ",gb[j]); */
	    /* printf("\n"); */
	    
	    for(int v=0;v<=maxW;v++)
		next[v]=prev[v];
	    for(int v=0;v<maxW;v++)
		if(prev[v]>=0)
		    for(int j=0;j<k;j++)
		    {
			int y = gw[j]+v;
			int s = gb[j]+prev[v];
			if(y<=maxW && s>next[y])
			    next[y] = s;
		    }
	    int tb = 0;
	    int tw = 0;
	    for(int j=0;j<k;j++)
	    {
		tb+=gb[j];
		tw+=gw[j];
	    }
	    for(int v=0;v<maxW;v++)
		if(prev[v]>=0 && v+tw<=maxW && prev[v]+tb>next[v+tw])
		    next[v+tw] = tb+prev[v];
	    
	    for(int v=0;v<=maxW;v++)
		prev[v] = next[v];
	}
    int max = 0;
    for(int v=0;v<=maxW;v++)
	if(prev[v]>max)
	    max = prev[v];
    printf("%i\n",max);
}
#include<stdio.h>
int n, m, maxW, p[2000], w[2000], b[2000], prev[2000], next[2000], gb[2000],gw[2000];
int root(int i)
{
    if(p[i]!=i)
	p[i] = root(p[i]);
    return p[i];
}
int main()
{
    scanf("%i%i%i",&n,&m,&maxW);
    for(int i=0;i<n;i++)
	scanf("%i",w+i);
    for(int i=0;i<n;i++)
	scanf("%i",b+i);
    int x, y;
    for(int i=0;i<n;i++)
	p[i]=i;
    for(int i=0;i<m;i++)
    {
	scanf("%i%i",&x,&y);
	x--;
	y--;
	p[root(x)]=root(y);
    }
    prev[0] = 0;
    for(int v=1;v<=maxW;v++)
	prev[v] = -1;
    for(int i=0;i<n;i++)
	if(p[i]==i)
	{
	    int k = 0;
	    for(int j=0;j<n;j++)
		if(root(j)==i)
		{
		    gb[k]=b[j];
		    gw[k]=w[j];
		    k++;
		}

	    /* for(int j=0;j<k;j++) */
	    /* 	printf("%i ",gb[j]); */
	    /* printf("\n"); */
	    
	    for(int v=0;v<=maxW;v++)
		next[v]=prev[v];
	    for(int v=0;v<maxW;v++)
		if(prev[v]>=0)
		    for(int j=0;j<k;j++)
		    {
			int y = gw[j]+v;
			int s = gb[j]+prev[v];
			if(y<=maxW && s>next[y])
			    next[y] = s;
		    }
	    int tb = 0;
	    int tw = 0;
	    for(int j=0;j<k;j++)
	    {
		tb+=gb[j];
		tw+=gw[j];
	    }
	    for(int v=0;v<maxW;v++)
		if(prev[v]>=0 && v+tw<=maxW && prev[v]+tb>next[v+tw])
		    next[v+tw] = tb+prev[v];
	    
	    for(int v=0;v<=maxW;v++)
		prev[v] = next[v];
	}
    int max = 0;
    for(int v=0;v<=maxW;v++)
	if(prev[v]>max)
	    max = prev[v];
    printf("%i\n",max);
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Stop Buying Cheap Alternatives This Viral Kitchen Gadgets Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

The Agentic Shift: How Meta Is Building the Infrastructure for AI-Powered Marketing

The digital advertising landscape is standing at the precipice of a massive structural evolution known as the agentic shift. As artificial intelligence advances beyond...

I Tried Viral Kitchen Gadgets For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.Most products in this category disappoint within weeks. Cheap materials, vague instructions, and results that...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!