-
Notifications
You must be signed in to change notification settings - Fork 464
/
Sequence weightage
96 lines (86 loc) · 1.23 KB
/
Sequence weightage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<stdio.h>
#include<limits.h>
int position(int x)
{
int s=0,d;
while(x>0)
{
d=x%10;
s+=d;
x=x/10;
}
return s;
}
int main()
{
int i, j, n, m,pos, s=0,maxsum=0,flag=0;
scanf("%d",&n);
int po[n];
int a[n];
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
pos=position(a[i]);
po[i]=pos;
}
for (i=0;i<n;i++)
{
s+=po[i]*(i+1);
}
printf("%d\n",s);
int temp,temp2;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(po[j]>po[j+1])
{
temp2=po[j];
po[j]=po[j+1];
po[j+1]=temp2;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n-1;i++)
{
if(po[i]==po[i+1])
{
flag=1;
break;
}
}
int t=n+2;
if(flag==1)
{
for(i=0;i<n;i++)
{
if(i==t)
{
continue;
}
if(po[i]==po[i+1])
{
printf("%d %d ",a[i+1],a[i]);
t=(i+1);
}
else
if(i==n-1)
printf("%d",a[i]);
else
printf("%d ",a[i]);
}
printf("\n");
}
for(i=0;i<n;i++)
{
if(i==n-1)
printf("%d",a[i]);
else
printf("%d ",a[i]);
maxsum+=po[i]*(i+1);
}
printf("\n%d",maxsum);
}