1

I am practicing a problem on codechef and I encountered a sigsegv error and when I see the problem's setter solution I find it similar to mine.

Is not using the proper datatypes an issue or is it something else?

Constraints are t,each element of arrays a & c <=10^6 memory limit 256MB

My Code:

#include <iostream>
#include <stdio.h>
#include "algorithm"

using namespace std;

int main() 
{
long long int t;
int i;
scanf("%lld",&t);
long long int a[t],c[t],temp=0;


for(i=0;i<t;i++)
{
    scanf("%lld",&a[i]);
}

for(i=0;i<t;i++)
{
    scanf("%lld",&c[i]);
}

sort(a,a+t);
sort(c,c+t);

for(i=0;i<t;i++){
    temp=temp+a[i]*c[i];
}
printf("%lld",temp);
return 0;   
}

Problem setter's code:

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
vector<long long> cats, cal;
int main(){
int n;
long long temp;
scanf("%d", &n);
for(int i=0;i<n;i++){
scanf("%lld", &temp);
cats.push_back(temp);
}
for(int i=0;i<n;i++){
scanf("%lld", &temp);
cal.push_back(temp);
}
sort(cats.begin(), cats.begin() + n);
sort(cal.begin(), cal.begin() + n);
temp = 0;
for(int i=0;i<n;i++)
temp = temp + cats[i]*cal[i];
printf("%lld\n", temp);
return 0;
}
user1683894
  • 405
  • 1
  • 6
  • 20

0 Answers0