Spoj easiest solution of ABSP1
#include<iostream>
using namespace std;
int main()
{
long long int t,n,m,s,count,i;
cin>>t;
while(t--)
{
count=0;
cin>>n;
long long int a[n];
s=0;
m=0;
for(i=0;i<n;i++)
{
cin>>a[i];
s=s+a[i];
}
for(i=0;i<n-1;i++)
{
count=count+(s-m)-((n-i)*a[i]);
m=m+a[i];
}
cout<<count<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
long long int t,n,m,s,count,i;
cin>>t;
while(t--)
{
count=0;
cin>>n;
long long int a[n];
s=0;
m=0;
for(i=0;i<n;i++)
{
cin>>a[i];
s=s+a[i];
}
for(i=0;i<n-1;i++)
{
count=count+(s-m)-((n-i)*a[i]);
m=m+a[i];
}
cout<<count<<endl;
}
return 0;
}
No comments:
Post a Comment