Bear Limak likes watching sports on TV. He is going to watch a game today. The game lasts 90 minutes and there are no breaks.Each minute can be either interesting or boring. If 15 consecutive minutes are boring then Limak immediately turns TV off.You know that there will be n interesting minutes t1, t2, ..., tn. Your task is to calculate for how many minutes Limak will watch the game.

The first line of the input contains one integer n (1 ≤ n ≤ 90) — the number of interesting minutes. The second line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... tn ≤ 90), given in the increasing order.

Print the number of minutes Limak will watch the game.

NoteIn the first sample, minutes 21, 22, ..., 35 are all boring and thus Limak will turn TV off immediately after the 35-th minute. So, he would watch the game for 35 minutes.In the second sample, the first 15 minutes are boring.In the third sample, there are no consecutive 15 boring minutes. So, Limak will watch the whole game.

#include<stdio.h>
int main()
{
    int n,a,b,c,d,e,i,s,x;
    scanf("%d",&n);
    int ara[n];
    for(i=0; i<n; i++)
        scanf("%d",&ara[i]);

    if(ara[0]>15)
        printf("15\n");
    else
    {
        for(i=0; i<n; i++)
        {
            s=ara[i]+15;

            if(s!=ara[i+1])
                if(s>ara[i+1] && s<90)
                    ara[i]=ara[i+1];
                else if(s<ara[i+1])
                    break;
        }

            if(s>=90)

                printf("90\n");
        if(s<90)
            printf("%d\n",s);
    }

        return 0;
    }

#include<stdio.h>
int main() {
	int n;
	scanf("%d",&n);
	int i;
	int now=0;
	for(i=0;i<n;i++){
		int t;
		scanf("%d",&t);
		if(t-now<=15){
			now=t;
			if(i==n-1){
				if(now+15<=90){
					now=now+15;
				}else{
					now=90;
				}
			}
		}else{
			now=now+15;
			break;
		}
	}
	printf("%d\n",now);
	return 0;
}

#include<stdio.h>
int main(void)
{
    int i,a,x=0,time=15,n;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a);
        if(a<=time&&x==0)
            time=a+15;
        else
            x++;
    }
    if(time>90)
        time=90;
    printf("%d\n",time);
    return 0;
}

#include<stdio.h>
int main()
{
    int n,i,t,J,I=0;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d",&n);
        if(n-I>15)
        {
            break;
        }
        I=n;
    }
    if(I+15<=90)
    {
        printf("%d",I+15);
    }
    else
    {
        printf("90");
    }

    return 0;
}

#include<stdio.h>
int n;
int time[91];
int ans;
int times;
int main()
{
    int i,j;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        scanf("%d",&j);
        time[j]++;
    }
    while(ans<15&&times<90)
    {
        ans++;
        times++;
        if(time[times]!=0)ans=0;
    }
    printf("%d",times);
    return 0;
}

#include <stdio.h>

int main()
{
	int n,s,fst=0,i,cnt=0;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&s);
		if(s-fst>15) break;
		fst=s;
	}
	int p=(fst+15>90)?90:fst+15;
	printf("%d\n",p);

	return 0;
}

#include<stdio.h>
int main ()
{
    int x,y,z,i,j,a;
    int sum=0,ck=0;
    int arr[1001];
    scanf("%d",&i);
    for(j=0;j<i;j++){
        scanf("%d",&arr[j]);
    }
    arr[j]=90;
    for(j=0;j<=i;j++){
        a=arr[j]-sum;
        if(a>15){
            printf("%d\n",sum+15);
            ck=1;
            break;
        }
        sum=arr[j];
    }
    if(ck==0){
        printf("%d\n",90);
    }
    return 0;
}

#include<stdio.h>
int main()
{
    int n,i;
    scanf("%d", &n);
    int arr[n+1];
    arr[0]=0;
    for(i=1;i<=n;i++)
    {
        scanf("%d", &arr[i]);
    }
    int sum=0,x=0;
    for (i=1;i<=n;i++)
    {
        if((arr[i]-arr[i-1])>15)
        {
            sum=arr[i-1]+15;
            x=1;
            break;
        }
        else
        {
            sum=arr[i];
        }

    }
    if(x==0)
    {
        sum=sum+15;
        if(sum>90)
    {
        sum=90;
    }
    }

    printf("%d\n", sum);
    return 0;

}

#include <stdio.h>

int main()
{
    int n, i; scanf("%d", &n);
    int ans = 15, buf;
    for(i = 0; i < n; i++)
    {
	scanf("%d", &buf);
	if(buf > ans) break;
	else ans = buf + 15;
    }
    printf("%d\n", ans<90?ans:90);
    return 0;
}

#include <stdio.h>
#include <stdbool.h>

#define minimum(x,y)((x)<(y) ? (x) : (y))
#define maximum(x,y)((x)>(y) ? (x) : (y))

int main(){
    int n,i,step,res,t;
    step =0;
    res = 0;
    scanf("%d",&n);

    for(i=0;i<n;i++){
      scanf("%d",&t);
      // printf("%d\n", t);
      if(t-step>15){
          printf("%d", step+15);
          return 0;
      }
      step=t;
    }

    printf("%d", minimum(90,step+15));
    return 0;
}

#include <stdio.h>

int main() {
	int i, n, cnt;
	static int a[91];

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	a[n] = cnt = 90;
	if (a[0] > 15)
		cnt = 15;
	else
		for (i = 1; i <= n; i++)
			if (a[i] - a[i - 1] - 1 >= 15) {
				cnt = a[i - 1] + 15;
				break;
			}
	printf("%d\n", cnt);
	return 0;
}

#include<stdio.h>

int main()
{
    long long int i,sum=0,n;
    scanf("%I64d",&n);
    long long int a[n];
    for(i=0;i<n;i++)
        scanf("%I64d",&a[i]);
    if(a[0]>15)
    {
        printf("15\n");
        return 0;
    }
    for(i=0;i<=n-1;i++)
    {
        if(a[i+1]-a[i]<=15)
            sum=a[i];
        else
        {
            sum=a[i];
            break;
        }

    }

        sum+=15;
     if(sum>=90)
        sum=90;
    printf("%I64d\n",sum);
    return 0;
}

#include<stdio.h>

int main(){
        int n, curMin, min;

        scanf("%d", &n);
        curMin = 15;
        while(n--){
                scanf("%d", &min);
                if (min <= curMin){
                        curMin = min + 15;
                        continue;
                }
        }

        if (curMin > 90)
                curMin = 90;

        printf("%d", curMin);

        return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i,min=0,bear[100];

    scanf(" %d", &n);
    bear[0]=0;

    for(i=1;i<=n;i++)
    {
        scanf(" %d", &bear[i]);
    }

    for(i=0;i<n;i++)
    {
        if((bear[i+1]-bear[i])>15)
        {
            min += bear[i]+15;
            printf("%d", min);
            break;
        }
    }
    if(min==0 && bear[n]<75)
	{
		min = bear[n] + 15;
		printf("%d", min);
	}
	if (min == 0)
		{
		    printf("%d", 90);
        }

    printf("\n");

    return 0;
}

#include <stdio.h>

int main() {
	int n, a, b;

	scanf("%d", &n);
	a = 0;
	while (n--) {
		scanf("%d", &b);
		if (b - a > 15)
			break;
		a = b;
	}
	printf("%d\n", a + 15 <= 90 ? a + 15 : 90);
	return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
    int a,l,i,q;
    scanf("%d",&a);
    int s[a];
    for(i=0;i<a;i++){
        scanf("%d",&s[i]);
    }
    for(i=0;i<a;i++){
        if(s[0]>=16){
            printf("15\n");
            goto A;
        }
        else if((s[i+1]-s[i])>15){
            q=s[i]+15;
            if(q>90){
                printf("90\n");
            }
            else{
                printf("%d\n",q);
            }
            goto A;
        }
    }
    A:return 0;
}

#include<stdio.h>
int main()
{int tmin,i,j,arr[90],plus,count=0;
    scanf("%d",&tmin);

    for (i=0;i<tmin;i++)
    {
        scanf("%d",&arr[i]);
    }
    if (tmin!=1)
    {

    if (arr[0]>15)
        plus=0;
    else {
        for (j=0;j<tmin-1;j++)

        {if (arr[j+1]<=15+arr[j])
        count++;
        else
            {break;}

        }

    if (count!=tmin-1)
    {
        plus=arr[count];
    }
    else {if(arr[tmin-1]>=75)
            plus=75;
            else plus=arr[tmin-1];
    }
    }
    printf("%d",plus+15);}
    else {if(arr[0]>15){
    plus=15;}
    else plus=15+arr[0];
     printf("%d",plus);
}}

#include<stdio.h>
int main()
{
    int array[10000],i,n,time,t;
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    if(n==1&&array[0]<15)
        printf("%d",array[0]+15);
    else if(array[0]>15)
        printf("15");

    else
    {
        for(i=0; i<n; i++)
        {
            t=array[i];
            if(array[i+1]-array[i]>15)
            {
                break;
            }
        }
        if(t+15<=90)
            printf("%d",(t+15));
        else
            printf("90");
    }
    return 0;
}

#include<stdio.h>
int main()
{
    int i,n,a=0;
    scanf("%d",&n);
    int t[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&t[i]);
    }
    for(i=0;i<n;i++)
    {
        if(t[i]-a<=15) a=t[i];
        else break;
    }
    if(90-a<=15){
    a=90;
    }
    else a=a+15;

    printf("%d",a);
    return 0;
}

#include <stdio.h>

int main()
{
    int n, i, t, a, b = 0;

    scanf("%d", &n);

    for(i = 0; i < n; i++) {
        scanf("%d", &t);
        a = t;
        if(a - b > 15) {
            printf("%d\n", b + 15);
            return 0;
        }
        else b = t;
    }
    if(b >= 75) printf("90\n");
    else printf("%d\n", b + 15);

    return 0;
}

/* https://codeforces.com/contest/673/submission/19410860 (Dukkha) */
#include <stdio.h>

int main() {
	int n, a, b;

	scanf("%d", &n);
	a = 0;
	while (n--) {
		scanf("%d", &b);
		if (b - a > 15)
			break;
		a = b;
	}
	printf("%d\n", a + 15 <= 90 ? a + 15 : 90);
	return 0;
}

#include<stdio.h>
int main(){
    int n,i,c=0;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    c=0;
    for(i=0;i<n;i++){
        if(a[i]-c<=15){c=a[i];}
        else break;
    }
    if(c+15>90)printf("90");
    else printf("%d",c+15);
    return 0 ;
}

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
	int i,n,watch=15;
	scanf("%d",&n);
	int time[n];
	for (i = 0; i < n; i += 1)
		scanf("%d",&time[i]);
	for (i = 0; i < n; i += 1)
	{
		if(time[i]<=watch)
			watch=time[i]+15;
		else
			break;
	}
	if(watch>90)
		watch=90;
	printf("%d \n", watch);
	return 0;
}

#include  <stdio.h>
#include  <string.h>
#include  <math.h>
#include  <ctype.h>
#include  <stdlib.h>
#include  <conio.h>
int main ()
{int n,i,sum=0,j,ok=0;
scanf ("%d",&n);
int t [100];
for (i=0;i <n;i++)
scanf  ("%d",&t [i]);
if (t [0]>15)
{sum=15;
ok=1;}
if (t [0]<=15)
{sum=t [0];
if (n==1)
sum+=15;
}
if (ok==0)
{for (i=1;i <n;i++)
{if (t[i]-t [i-1]<=15)
{
	sum+=t [i]-t [i-1];
	if (t [i]==t [n-1]&&t [i]<=75)
sum+=15;
else
if (t [i]==t [n-1])
sum+=90-t [i];
}
else
	{sum+=15;
break;}
}}

printf  ("\n%d",sum);
return  0;
}

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int n ;
		int i;
		int t=0, b=0,a = 0, c = 0,y;
		scanf("%d",&n);
		for (i=1; i<n+1; i++){
			 scanf("%d",&b);
			 y=b-t;
			 if (y>15){
				    c= t+15; a=1;
				    i= n+1;
				      }
			 t=b;
		}
		if(a==0){
			if(t<85  ) c= t+15;
		else c=90;
		}

		if(c>90  ) c=90;
		printf("%d",c);
	return 0;
}

#include<stdio.h>
int main()
{	int time=90;
	int N;
	scanf("%d",&N);
	int A[N];
	int i;
	for(i=0;i<N;i++)
	{	scanf("%d",&A[i]);
	}
	int diff[N+1];

	diff[0]=A[0];
	for(i=1;i<N;i++)
	{	diff[i]=A[i]-A[i-1];
	}
	diff[N]=90-A[N-1];

	for(i=0;i<N+1;i++)
	{	//printf("%d\n",diff[i]);
		if(diff[i]>15)
		{	if(i==0)
				time=15;
			else time=A[i-1]+15;
			break;
		}

	}

	printf("%d\n",time);
}

#include<stdio.h>
#define Max 100
int main()
{
	int n,a[Max],s=0,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	i=0;
	while(a[i]-s<=15 && i<n)
	{
		s=a[i];
		i++;
	}
	if(s+15>90)
		printf("%d\n",90);
	else
		printf("%d\n",s+15);
	return 0;
}

#include<stdio.h>
int main()
{
    int test;
    while(scanf("%d",&test)==1)
    {
        int a[1000],i,b,c;
        b=15;
        for( i=0; i<test; i++)
            scanf("%d",&a[i]);
        for( i=0; i<test; i++)
        {
            c=a[i];
            if(c>b)
            {
                printf("%d\n",b);
                break;
            }
            else if(c>=75||b>90)
            {
                printf("90\n");
                break;
            }
            else if(i==test-1)
            {
                printf("%d\n",a[test-1]+15);
            }
            else
            {
                b=a[i]+15;
            }
        }
    }
    return 0;
}

#include<stdio.h>
int main()
{
    int i,n,a=0;
    scanf("%d",&n);
    int t[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&t[i]);
    }
    for(i=0;i<n;i++)
    {
        if(t[i]-a<=15) a=t[i];
        else break;
    }
    if(90-a<=15) a=90;
    else a=a+15;
    printf("%d",a);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int n, i, s[90], result, time, flag;

int main()
{
    scanf("%d", &n);

    for (i = 0; i < n; i++)
        scanf("%d", &s[i]);

    result = 0;
    flag = 0;
    time = 0;
    i = 0;

    while (flag != 1)
    {
        if ((time+1) != s[i])
        {
            result++;
        }
        else
        {
            result = 0;
            i++;
        }
        if (result == 15) {
            flag = 1;
        }
        time++;
        if (time == 90)
        {
            flag = 1;
        }
    }

    printf("%d", time);
    return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	int n, i, sum;
	scanf("%d", &n);
	int ara[n+1];
	ara[0] = 0;
	for(i = 1; i <= n; i++)
	{
		scanf("%d", &ara[i]);
	}
	for(i = 1; i <= n; i++)
	{

		if(ara[i] - ara[i - 1] > 15)
		{
			sum = ara[i-1] + 15;
			break;
		}
		else if((i==n) && ara[i] - ara[i - 1] <= 15 )
		{
			sum = ara[i] + 15;
			if(sum>90) sum = 90;

		}
		else sum = ara[i];
	}

	 printf("%d", sum);

	return 0;
}

#include<stdio.h>
int main()
{
	int n,m,i;
	scanf("%d",&n);
	int ary[n+2];
	ary[0]=0;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&ary[i]);
	}
	ary[i]=90;
	m=90;
	for(i=0;i<=n;i++)
	{
		if(ary[i]+15<ary[i+1]){

		m=ary[i]+15;
		break;}
	}

printf("%d",m);
	return 0;
}

#include<stdio.h>
int main()
{
int n,t[90],i,time,j,u;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&t[i]);
t[n+1]=90;
if(t[1]>15)
time=15;
else
{
for(i=1;i<=n;i++)
{
u=t[i+1]-t[i];
time=t[i];
if(u<16)
{time=t[i+1];
}
else
{time=time+15;
break;}
}
}
printf("%d",time);
return 0;
}

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int i,A[n];
    int countn=1;
    for(i=0;i<n;i++)
        scanf("%d",&A[i]);
    if(n==1)
    {
        if(A[0]>15)
            printf("15");
        else
            printf("%d",A[0]+15);
            return 0;
    }
    if(A[0]>15)
        printf("15");
    else
     for(i=0;i<n-1;i++)
    {
        if(A[i+1]-A[i]<=15)
           {
               countn++;
               continue;
           }

        if(A[i]<76)
           {
               printf("%d",A[i]+15);
               break;
           }
       else
            {
                printf("90");
                break;
            }
    }
    if(countn==n)
        {
            if(A[i]<76)
                printf("%d",A[i]+15);
            else
                printf("90");
        }
    return 0;

}

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int n;
	scanf("%d",&n);
	int mins[n];
	for(int o=0;o<n;o++)
	{
		scanf("%d",&mins[o]);
	}
	int count=0;
	if(n==1)
	{
		if(mins[0]<=15)
		{
		printf("%d",mins[0]+15);
		exit(0);
		}
		else printf("15");
	exit(0);
	}
		if(mins[0]>15)
		{
			printf("15");
			exit(0);

		}
		 else count+=mins[0];
	for(int p=0;p<n-1;p++)
	{

		if(mins[p+1]-mins[p]<=15)
		{
			count+=mins[p+1]-mins[p];

		}
		else {
			printf("%d",count+15);
			exit(0);
		}

	}
	if(90-mins[n-1]<=15)
	{
		count+=90-mins[n-1];
		printf("%d",count);
	}
	else
	{
		if(90-mins[n-1]>15)

	printf("%d",count+15);
	 }

}

#include<stdio.h>

int main()
{
	int n;
	scanf("%d",&n);

	int time[n],i;
	for (i=0; i<n; i++)
	{
		scanf("%d",&time[i]);
	}

	int pre=0,tot=0;
	for (i=0; i<n; i++)
	{
		if ((time[i]-pre)<=15)
		{
			tot = time[i];
			pre = time[i];
		}
		else
		{
			break;
		}
	}
	if (90-tot<=15)
	{
		tot = 90;
	}
	else
	{
		tot+=15;
	}

	printf("%d\n",tot);

	return 0;
}

#include <stdio.h>

int main() {
	int n, res = 0;
	scanf("%d", &n);

	int current, last = 0;
	for (int i = 0; i < n; i++) {
		scanf("%d", &current);
		if (current - last > 15) {
			printf("%d", last + 15);
			return 0;
		}
		last = current;
	}

	if (last > 75)
		printf("%d", 90);
	else
		printf("%d", last + 15);

	return 0;
}

#include <stdio.h>

int main(void) {
	int n,a,i,d,p=0,s=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a);
		d=a-p;
		if(d<=15)
		{
			if (i==n)
			{
			if((s+d+15)>90)s=90;
			else s=s+d+15;
			}
			else
			s=s+d;
		}
		else
		{
			s=s+15;
			break;
		}
		p=a;
	}
	printf("%d\n",s);
	return 0;
}

#include<stdio.h>
int main()
{
    int i , n , a = 0 ;
    scanf("%d",&n);
    int t[n];
    for(i = 0 ; i < n ; i++)
    {
        scanf("%d",&t[i]);
    }
    for(i = 0 ; i < n ; i++)
    {
        if(t[i] - a <= 15)
        {
            a = t[i] ;
        }
        else break ;
    }
    if(90-a <= 15)a = 90 ;
    else a = a + 15 ;
    printf("%d",a);
    return 0 ;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned int n, T[90], kq= 15, i;
    T[0]=0;
    scanf("%u",&n);
    for (i=1; i<=n; i++) {
        scanf("%u",&T[i]);
        if (T[i]-T[i-1]>15) { break; }
        kq = 15 +T[i];
    }
    if (kq<=90) { printf("%u",kq); }
    else { printf("90");}
    return 0;
}

#include<stdio.h>

int main()
{
	int n,i,s=15;
	scanf("%d",&n);
	int a[n];

	for(i=0;i<n;i++)
		scanf("%d",&a[i]);

	for(i=0;i<n;i++)
	{
		if(s-a[i]<0)
			break;
		else
			s=a[i]+15;
	}
	if(s>90)
	printf("90");
	else
	printf("%d",s);
}

#include<stdio.h>
#define Max 100
int main()
{
	int n,a[Max],s=0,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	i=0;
	while(a[i]-s<=15 && i<n)
	{
		s=a[i];
		i++;
	}
	if(s+15>90)
		printf("%d\n",90);
	else
		printf("%d\n",s+15);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
  int ara[100];
    int n,x=0,i;
    scanf("%d",&n);
    ara[0]=0;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&ara[i]);
    }
    for(i=0;i<n;i++)
    {

        if(ara[i+1]-ara[i]>15)
        {

            break;
        }

    }
    x=ara[i]+15;
    if(x>90)

    x=90;
    printf("%d\n",x);
    return 0;
}

#include<stdio.h>
int main()
{
int n,a[90];
scanf("%d",&n);
int i;
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
i=0;
int kar,count=0;
for(kar=1;kar<=90;kar++){
if(kar == a[i]){count=0;i++;}
else count++;
if (count == 15) {printf("%d",kar); break;}
}
if(count<15) printf("90");
return 0;}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned int n, T[90], kq= 15, i;
    T[0]=0;
    scanf("%u",&n);
    for (i=1; i<=n; i++) {
        scanf("%u",&T[i]);
        if (T[i]-T[i-1]>15) { break; }
        kq = 15 +T[i];
    }
    printf("%u",kq);
    return 0;
}

#include<stdio.h>

int main()
{
    int n;

    scanf("%d",&n);

    int a[n],Loop,l=1,s,count=0,m;

    for(Loop=0;Loop<n;Loop++)
        scanf("%d",&a[Loop]);

    m=a[0];

    for(Loop=0;Loop<n;Loop++)
    {
        s=m-l;

        if(s>15)
        {
            count++;
            Loop--;
            s=Loop;
            break;
        }
        else
        {
            l=a[Loop];
            m=a[Loop+1];
        }

    }

    if(count==0 && (a[n-1]+15)<90)
        printf("%d\n",a[n-1]+15);
    else if(count==0)
        printf("90\n");
    else if(Loop<0)
        printf("15\n");
    else
    {
        m=a[s]+15;
        printf("%d\n",m);
    }

    return 0;
}

#include<stdio.h>

int main()
{
	int n;
	scanf("%d",&n);

	int time[n],i;
	for (i=0; i<n; i++)
	{
		scanf("%d",&time[i]);
	}

	int pre=0,tot=0;
	for (i=0; i<n; i++)
	{
		if ((time[i]-pre)<=15)
		{
			tot = time[i];
			pre = time[i];
		}
		else
		{
			tot+=15;
			break;
		}
	}
	if (90-tot<=15)
	{
		tot = 90;
	}
	else
	{
		tot+=15;
	}

	printf("%d\n",tot);

	return 0;
}

#include <stdio.h>

int main()
{
    //printf("Hello World!\n");
    int n;
    scanf("%d", &n);
    int i, arr[n], brr[n];
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    brr[0]=arr[0];
    for(i=1; i<n; i++)
    {
        brr[i]=arr[i]-arr[i-1];
    }
    /*for(i=0; i<n; i++)
    {
        printf("%d ", brr[i]);
    }*/
    if(n==1)
    {
        if(arr[0]>15)
        {
            printf("15");
        }
        else
            printf("%d", arr[0]+15);
    }
    else{
    int count=0;
    for(i=0; i<n; i++)
    {
       if(brr[i]>15)
       {
           count=1;
           break;
       }
    }
    int sum=0;
    //printf("%d\n", count);
    if(count==1 && i==0)
    {
        printf("15");
    }

    else if(count==1 && i!=0)
    {
        sum=arr[i-1]+15;
        if(sum>90)
        {
            printf("90");
        }
        else
            printf("%d", sum);
    }
    else if(count==0)
    {
        printf("90");
    }
    }
    return 0;
    }

#include<stdio.h>

int main(){
        int n, curMin, min;

        scanf("%d", &n);
        curMin = 0;
        while(n--){
                scanf("%d", &min);
                if (min-15 <= curMin){
                        curMin = min;
                        continue;
                }
                else {
                        curMin = curMin + 15;
                        break;
                }
        }

        printf("%d", curMin);

        return 0;
}

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int i,A[n];
    int countn=1;
    for(i=0;i<n;i++)
        scanf("%d",&A[i]);
    if(n==1)
    {
        if(A[0]>15)
            printf("15");
        else
            printf("%d",A[0]+15);
            return 0;
    }
    if(A[0]>15)
        printf("15");
     for(i=0;i<n-1;i++)
    {
        if(A[i+1]-A[i]<=15)
           {
               countn++;
               continue;
           }

        if(A[i]<76)
           {
               printf("%d",A[i]+15);
               break;
           }
       else
            {
                printf("90");
                break;
            }
    }
    if(countn==n)
        {
            if(A[i]<76)
                printf("%d",A[i]+15);
            else
                printf("90");
        }
    return 0;

}

#include<stdio.h>
#include<string.h>
int main()
{
    int n,i,m,sum=0;
    scanf("%d",&n);
    int arr[n+1];
    for(i=0 ; i<n ; i++)
        {
            scanf("%d",&m);
            if(m-sum>15)
                break;
            sum=m;
        }
    if(sum<=85)
        printf("%d\n",sum+15);
    else
        printf("90\n");

    return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
    int i,j,k,n,c=0;

    scanf("%d",&n);

    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    a[-1]=0;
    for(i=-1;i<n-1;i++)
    {
        if(a[i+1]-a[i]>15)
        {
            printf("%d",a[i]+15);
            break;
        }
        else
        {
             c++;
             continue;
        }

    }

    if(c==n)
        printf("90");

    return 0;
}

#include<stdio.h>
main()
{
    int n,i;
    scanf("%d",&n);
    int ara[n];

    for(i=0;i<n;i++) scanf("%d",&ara[i]);

    if(ara[0]-0<=15) {
        for(i=1;i<n;i++) {
            if(ara[i]-ara[i-1]>15) {
                printf("%d",ara[i-1]+15);
                return 0;
            }
            if(i==n-1) printf("%d",ara[i]);
        }

    }

    else printf("15");
}

#include<stdio.h>
int main()
{
    int n,a[1000],i,sum;
    while(scanf("%d",&n)==1)
    {
        for(i=0; i<n; i++)
            scanf("%d",&a[i]);
        if(a[0]<=15)
        {
            sum=a[0];
            for(i=1; i<n; i++)
            {
                if(a[i]-a[i-1]<=15)
                    sum=a[i];
                else
                {
                    if(sum<=75)
                        sum+=15;
                    else
                        sum=90;
                    break;
                }
            }
            if(sum<=75)
            sum+=15;
            else
                sum=90;
        }
        else
            sum=15;
        printf("%d\n",sum);
    }
    return 0;
}

#include<stdio.h>
int main()
{
	int n,t[91],i,ans,tmp;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	scanf("%d",&t[i]);
	ans=0;
	tmp=0;
	for(i=0;i<n;i++)
	{
		if(t[i]-tmp<=15)
		{
			ans=t[i];
			tmp=t[i];
		}
		else
		{
			if(tmp+15<=90)
			ans=tmp+15;
			else
			ans=90;
			break;
		}
	}
	printf("%d\n",ans);
	return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
    int n,i,m,sum=0;
    scanf("%d",&n);
    int arr[n+1];
    for(i=0 ; i<n ; i++)
        {
            scanf("%d",&m);
            if(m-sum>15)
                break;
            sum=m;
        }
    if(sum<=85)
        printf("%d\n",sum+15);
    else
        printf("%d\n",sum);

    return 0;
}

#include<stdio.h>
int main()
{
	int n;
	scanf("%d",&n);

	int t[n],i,min=0,flag=1,ref=0;

	for(i=0;i<n;i++)
	{
		scanf("%d",&t[i]);
		if(flag)
		{
			if(t[i]-ref>15)
			{
				//printf("Flagged at %d time\n",t[i]);
				min=ref+15;
				flag=0;
			}
			else
				ref=t[i];
		}
	}
	//printf("THe ref time: %d\n",ref);
	if(flag)
		min=90;
	printf("%d\n",min);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, x, i, min = 0;
    scanf("%d", &n);
    for (i = 0; i < n; i++){
        scanf("%d", &x);
        min += 15;
        if (min >= x) min = x;
        else break;
    }
    printf("%d\n", min);
    return 0;
}

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,count1=0,a,b,a2,b2,a3,b3;

scanf("%lld%lld",&n,&b);
  if(b>15){
            printf("15");
            return 0;
        }
for(i=1;i<n;i++){
    scanf("%lld",&a);
    if(a-b>15){
      printf("%lld",b+15);
        return 0;
    }
        b=a;
}
printf("90");
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

int n, s, result, time;
//int i, j;
//char str[100];
//int *a;  // указатель на массив

int main()
{
    scanf("%d", &n);

    // Выделение памяти
    /*a = (int*) malloc(n*sizeof(int));
    // Ввод элементов массива
    for(i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }*/

    result = 0;

    while (n--)
    {
        scanf("%d", &s);
        result = s - time;
        time = time + result;
        //printf("1 %d\n", time);
        if (result > 15)
        {
            time = time - (result - 15);
            //printf("2 %d\n", time);
            break;
        }
    }

    /*scanf("%s", &str);
    for (i = 1; i < strlen(str); i++)
    {
        if (str[i] == str[i-1])
            sum++;
        else
            sum = 1;
        if (sum == 7)
            result = 1;
    }*/

    printf("%d\n", time);

    //free(a);
    return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
    int i,j,k,n,c=0;

    scanf("%d",&n);

    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    a[-1]=0;
    a[n]=0;
    for(i=-1;i<n-1;i++)
    {
        if(abs(a[i+1]-a[i])>15)
        {
            printf("%d",a[i]+15);
            break;
        }
        else
        {
             c++;
             continue;
        }

    }

    if(c==n && c>1)
        printf("90");
        else if(c==1)
        {
            printf("16");
        }

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
	int i,n,watch=0;
	scanf("%d",&n);
	int time[n];
	for (i = 0; i < n; i += 1)
		scanf("%d",&time[i]);
	for (i = 0; i < n; i += 1)
	{
		if(watch+15>=time[i] && i!=n-1)
		{
			if(i==0)
				watch+=time[i];
			else
				watch+=time[i]-time[i-1];
		}
		else if (watch+15>=90)
		{
			watch=90;
			break;
		}
		else
		{
			watch+=15;
			break;
		}
	}
	printf("%d \n", watch);
	return 0;
}

#include <stdio.h>
int main()
{
    int i,a[92],j=1;
    int m;
    int n;
    scanf("%d",&n);
    i=0;
    j=0;
    while(i<n) {scanf("%d",&a[i]);i++;}
    for(i=0;i<n;i++)
    {
        m=a[i]-j;
        if(m>15)
            {printf("%d",j+15); return 0;}
        j=a[i];
    }
    printf("90");
    return 0;
}

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n,a,b=0,time=0,diff;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&a);

        diff=abs(a-b);
        if(diff>15)
        {
            time =time+15;
            goto end;
        }
        else if(diff<16)
        {
            time=a;

        }
        b=a;
    }
    if(time>74 && time<91) time=90;
    else if(time>0 && time <16) time=time+15;
    end:
    printf("%d",time);
    return 0;
 }

#include <stdio.h>

int main()
{
    int n, i, temp, count = 0;
    scanf("%d", &n);
    int arr[n];

    for(i=0; i<n; i++)
        scanf("%d", &arr[i]);

    for(i=0; i<n; i++){
        if(arr[i] - count <= 15)
            count = arr[i];
        else{
            printf("%d", count + 15);
            return 0;
        }
    }
    printf("90");

	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int main() {
	int n,x=0,k=0,p=0,j;
	scanf("%d",&n);
	int a[n+2]; a[0]=0;a[n+1]=90;
	for (int i = 1;i<=n;i++){
		scanf("%d",&a[i]);
	}for ( j = 0;j<=n+1;j++){
		x = a[j+1]-a[j];

			if (x>MAX){
				k = a[j]+MAX;
				printf("%d\n",k);
				break;

		}else if (a[n+1]-a[n] < MAX){
			printf("%d",90);break;
		}
	}
   return 0;
}

#include<stdio.h>
int main()
{
	int n;
	scanf("%d",&n);

	int t[n],i,min=0,flag=1,ref=0;

	for(i=0;i<n;i++)
	{
		scanf("%d",&t[i]);
		if(flag)
		{
			if(t[i]-ref>15)
			{
				printf("Flagged at %d time\n",t[i]);
				min=ref+15;
				flag=0;
			}
			else
				ref=t[i];
		}
	}
	//printf("THe ref time: %d\n",ref);
	if(flag)
		min=90;
	printf("%d\n",min);
	return 0;
}

#include<stdio.h>

int main()
{
	int input=0;
	int input2[91]={0,};
	int i=0;
	scanf("%d", &input);
	for(i=0; i<input; i++)
	{
		scanf("%d", &input2[i+1]);
	}

	for(i=0; i<input+1; i++)
	{
	if(input2[i+1]>input2[i]+15||i==input)
			{
				printf("%d", input2[i]+15);
				break;

			}

		else if(input2[i]>=75)
				{printf("90");
				break;
			}
	}

	return 0;
}

#include<stdio.h>

int main()
{
        int input=0;
        int input2[91]={0,};
        int i=0;
        scanf("%d", &input);
        for(i=0; i<input; i++)
        {
                scanf("%d", &input2[i+1]);
        }

        for(i=0; i<input+1; i++)
        {
        if(input2[i+1]>=input2[i]+15||i==input)
                        {
                                printf("%d", input2[i]+15);
                                break;

                        }

                else if(input2[i]>=75)
                                {printf("90");
                                break;
                        }
        }

        return 0;
}

int main()
{
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++)
        scanf("%d",&arr[i]);
    int d,t=arr[0]+15,flag=0;
    d=arr[0];
    if(arr[0]>15)
        t=15;
    for(int i=1;i<n;i++)
    {
        if(d>15)
        {
            flag++;
            printf("%d",t);
            return 0;
        }
        else
        {
            d=arr[i]-arr[i-1];
            t=arr[i-1]+15;
        }
    }
    if(flag==0)
    {
        if(t<=90)
        {
            printf("%d",t);
            return 0;
        }
        else
            printf("90");

    }
    return 0;
}

#include<stdio.h>
int main(void)
{
    int a,ara[1000],b=0,i,j,k;
    scanf("%d",&a);
    for(i=0;i<a;i++)
    {
        scanf("%d",&ara[i]);
    }

    for(j=0;j<a;j++)
    {
        if(ara[a-1]==90){
            printf("90");
            break;
        }
        k=ara[j];
        b=ara[j+1]-k;

        if(b>15){
        printf("%d",ara[j]+15);

        break;
        }

    }

}

#include<stdio.h>

int main()
{
        int input=0;
        int input2[92]={0,};
        int i=0;
        scanf("%d", &input);
        for(i=0; i<input; i++)
        {
                scanf("%d", &input2[i+1]);
        }

        for(i=0; i<input+1; i++)
        {
        if(input2[i+1]>input2[i]+15||i==input)
                        {
                                printf("%d", input2[i]+15);
                                break;

                        }

                else if(input2[i]>=75)
                                {printf("90");
                                break;
                        }
        }

        return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<complex.h>
#include<string.h>
#define prime 1000000007
int main()
{
	int temp,num,i,arr[90];
	scanf("%d",&num);
	for(i=0;i<num;i++)
	{
		scanf("%d",&arr[i]);
	}
	if(num==1)
	{
		if(num+15<=90)
			printf("%d\n",num+15);
		else
			printf("90\n");
	}
	if(arr[0]>15)
	{
		printf("15\n");
	}
	else
	{
		temp=arr[0];
		for(i=1;i<num;i++)
		{
			if((temp+15)>=arr[i])
			{
				if(temp+15>=90)
				{
					printf("90\n");
					break;
				}
				else
				{
					temp=arr[i];
					if(i==num-1)
					{
						printf("%d\n",temp+15);
						break;
					}
				}
			}
			else if(i==num-1)
			{
				printf("%d\n",temp+15);
				break;
			}
			else
			{
				printf("%d\n",temp+15);
				break;
			}
		}
	}
	return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
	int n, i, sum;
	scanf("%d", &n);
	int ara[n+1];
	ara[0] = 0;
	for(i = 1; i <= n; i++)
	{
		scanf("%d", &ara[i]);
	}
	for(i = 1; i <= n; i++)
	{

		if(ara[i] - ara[i - 1] > 15)
		{
			sum = ara[i-1] + 15;
			break;
		}
		else sum = ara[i];
	}

	printf("%d\n", sum);

	return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int size, aux=0, i=0, j=0;
    scanf("%i", &size);
    int a[size];
    for(i;i<size; i++)
    {
        scanf("%i",&a[i]);
        if(a[0]>15)
        {
            aux=15;
            j=1;
        }
        else if((a[i]-aux)<=15 && j!=1)
        {
            aux=a[i];
            if((aux+15)>=90)
            {
                aux=90;
                j=1;
            }
            else if((i+1)==size)
            {
                aux+=15;
            }
        }
        else
        {
            aux=a[i-1]+15;
            j=1;
        }
    }
    printf("%i", aux);
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<complex.h>
#include<string.h>
#define prime 1000000007
int main()
{
	int temp,num,i,arr[90];
	scanf("%d",&num);
	for(i=0;i<num;i++)
	{
		scanf("%d",&arr[i]);
	}
	if(num==1)
	{
		if(arr[0]+15<=30)
			printf("%d\n",arr[0]+15);
		else if(arr[0]>15)
			printf("15\n");
	}
	else if(arr[0]>15)
	{
		printf("15\n");
	}
	else
	{
		temp=arr[0];
		for(i=1;i<num;i++)
		{
			if((temp+15)>=arr[i])
			{
				if(temp+15>=90)
				{
					printf("90\n");
					break;
				}
				else
				{
					temp=arr[i];
					if(i==num-1)
					{
						printf("%d\n",temp+15);
						break;
					}
				}
			}
			else if(i==num-1)
			{
				printf("%d\n",temp+15);
				break;
			}
			else
			{
				printf("%d\n",temp+15);
				break;
			}
		}
	}
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
	int i,n,watch=0;
	scanf("%d",&n);
	int time[n];
	for (i = 0; i < n; i += 1)
		scanf("%d",&time[i]);
	for (i = 0; i < n; i += 1)
	{
		if(watch+15>=time[i])
		{
			if(i==0)
				watch+=time[i];
			else
				watch+=time[i]-time[i-1];
		}
		else
		{
			watch+=15;
			break;
		}
	}
	printf("%d \n", watch);
	return 0;
}

#include<stdio.h>
int main() {
	int n, a[100], i;
	//freopen("input.txt", "r", stdin);
	scanf("%d", &n);
	for (i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	a[0] = 0;
	for (i = 1; i <= n; i++) {
		if (a[i] - a[i - 1] > 15)
			break;
	}
	if (i > n) printf("90");
	else printf("%d", a[i - 1] + 15);
	return 0;
}

#include <stdio.h>

int main(void) {
	int n,i,a[100],d,s=0,p=0;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		d=a[i]-p;
		{
			if(d<=15)s=s+d;
			else
			{
				s=s+15;
				break;
			}
			p=a[i];
		}

	}
	printf("%d\n",s);
	return 0;
}

#include <stdio.h>

int main()
{
    int n, pa=0, ca;
    scanf("%d", &n);
    if(n==1)
    {
        scanf("%d", &ca);
        if((ca-pa)>15)
            printf("%d\n", pa+15);
        else
            printf("%d\n", ca+15);
        return 0;
    }
    while(n--)
    {
        scanf("%d", &ca);
        if((ca-pa) > 15)
        {
            printf("%d\n", pa+15);
            return 0;
        }
        pa = ca;
    }
    if(pa > 85)
        printf("90\n");
    else
        printf("%d\n", pa+15);

    return 0;
}

#include<stdio.h>

int main()
{
	int input=0;
	int input2[90]={0,};
	int i=0;
	scanf("%d", &input);
	for(i=0; i<input; i++)
	{
		scanf("%d", &input2[i+1]);
	}

	for(i=0; i<input+1; i++)
	{
	if(input2[i+1]>=input2[i]+15)
			{
				printf("%d", input2[i]+15);
				break;

			}

		else if(input2[i]>=75)
				{printf("90");
				break;
			}
	}

	return 0;
}

#include<stdio.h>
int main(){
    int n,a;
    scanf("%d\n",&n);
    int i=0;
    while(n--){
        scanf("%d ",&a);
        if(a-i>15){printf("%d",i+15);return 0;}
        i=a;
    }
    int k=90;
    if(n==0)printf("%d",k);
    return 0;
}

#pragma warning(disable:4996)
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define N 100
#define EPS 1E-5
//#define min(a,b) (a>b) ? b : a
int lol(const void *x1, const void *x2)
{
	return *(int*)x1 - *(int*)x2;
}
/*
int gcd(int a, int b)
{
	int c;
	while (b)
	{
		c = a % b;
		a = b;
		b = c;
	}
	return a;
}*/
int Min(int a, int b)
{
	return (a > b) ? b : a;
}
int main(void)
{
	int n, i, temp, last = 0;
	scanf("%i", &n);
	for (i = 0; i < n; i++)
	{
		scanf("%i", &temp);
		if (temp - last > 15)
		{
			printf("%i", last + 15);
			return 0;
		}
		else
			last = temp;
	}
	printf("90");
	return 0;
}

#include <stdio.h>

int main()
{
    int a[90], i, check, n, flag=-100;
    scanf("%d", &n);
    for(i=0; i<n; i++)
        scanf("%d", &a[i]);
    for(i=0; i<n; i++)
    {
        if(i==0)
        {
            check = a[i];
            flag = a[i];
            if(check>15)
            {
                flag = 0;
                break;
            }
        }
        else
        {
            check = a[i]-a[i-1];
            if(check>15)
            {
                flag = a[i-1];
                break;
            }
        }
    }
    if(flag<0||flag>=75)
        printf("90\n");
    else
        printf("%d\n", flag+15);
}

#include<stdio.h>
int main()
{
    int i,n,times[100],total=0;
    scanf("%d",&n);
    times[0]=0;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&times[i]);
        if(times[i]-times[i-1]>15)
        {
            total=total+15;
            break;
        }
        else
        {
            total=total+times[i]-times[i-1];
        }

    }
    printf("%d\n",total);
    return 0;
}

#include<stdio.h>
int main()
{
    int n,i,j;
    scanf("%d",&n);
    int t[n];
    for(i=0;i<n;i=i+1)
    {
        scanf("%d",&t[i]);
    }
    j=1;
    int count=0;
    for(i=0;i<n;i=i+1)
    {
       if(t[i]-j>=15)
       {
           printf("%d",count+15);
           exit(0);
       }
       else
       {
           j=t[i];
           count=t[i];
       }
    }
    printf("90");
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, x, i, min = 0;
    scanf("%d", &n);
    for (i = 0; i < n; i++){
        scanf("%d", &x);
        min += 15;
        if (min >= x) min = x;
        else break;
    }
    if (min >= 75) printf("90\n");
    else printf("%d\n", min);
    return 0;
}

#include<stdio.h>
int main()
{
    int i,n,times[100];
    scanf("%d",&n);
   times[0]=0;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&times[i]);
    }
    for(i=1;i<=n;i++)
    {
        if(times[i]-times[i-1]>15)
        {
            times[i-1]=times[i-1]+15;
            break;
        }
    }

    printf("%d\n",times[i-1]);

    return 0;
}

#include<stdio.h>

int main()
{
	int n;
	scanf("%d",&n);

	int time[n],i;
	for (i=0; i<n; i++)
	{
		scanf("%d",&time[i]);
	}

	int pre=0,tot=0;
	for (i=0; i<n; i++)
	{
		if ((time[i]-pre)<=15)
		{
			tot = time[i];
			pre = time[i];
		}
		else
		{
			tot+=15;
		}
	}
	printf("%d\n",tot);

	return 0;
}

#include <stdio.h>
int main()
{
        int k=0,n,*a,i,t;
    scanf("%d",&n);
    a=malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
   {
       scanf("%d",&a[i]);
    if(a[i]-k>15)
        {t=k+15;
        break;}
    else
       {
           k=a[i];
        if(k>=75)
            t=90;}
        }
        printf("%d",t);
        return 0;

}

#include<stdio.h>

int main()
{
    int n, a[90], i, p, q;

    scanf("%d", &n);
    for(i=0;i<n;i++)
         scanf("%d", &a[i]);
    q=a[0];
    if(q>15)
         q=15;
    else if(n==1)
        q=q+15;
    else
    {
    for(i=1;i<n;i++)
    {
        q=q+15;
         if(q>a[i])
               q=a[i];
        else
          break;
    }

    }
    p=90-q;
    if(p>15)
        printf("%d\n", q);
    else
        printf("90\n");

    return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int size, aux=0, i=0, j=0;
    scanf("%i", &size);
    int a[size];
    for(i;i<size; i++)
    {
        scanf("%i",&a[i]);
        if((a[i]-aux)<=15 && j!=1)
        {
            aux=a[i];
            if((aux+15)>=90)
            {
                aux=90;
                j=1;
            }
            else if((i+1)==size)
            {
                aux+=15;
            }

        }
    }
    printf("%i", aux);
    return 0;
}


Natasha's favourite numbers are $$$n$$$ and $$$1$$$, and Sasha's favourite numbers are $$$m$$$ and $$$-1$$$. One day Natasha and Sasha met and wrote down every possible array of length $$$n+m$$$ such that some $$$n$$$ of its elements are equal to $$$1$$$ and another $$$m$$$ elements are equal to $$$-1$$$. For each such array they counted its maximal prefix sum, probably an empty one which is equal to $$$0$$$ (in another words, if every nonempty prefix sum is less to zero, then it is considered equal to zero). Formally, denote as $$$f(a)$$$ the maximal prefix sum of an array $$$a_{1, \ldots ,l}$$$ of length $$$l \geq 0$$$. Then: $$$$$$f(a) = \max (0, \smash{\displaystyle\max_{1 \leq i \leq l}} \sum_{j=1}^{i} a_j )$$$$$$Now they want to count the sum of maximal prefix sums for each such an array and they are asking you to help. As this sum can be very large, output it modulo $$$998\: 244\: 853$$$.

The only line contains two integers $$$n$$$ and $$$m$$$ ($$$0 \le n,m \le 2\,000$$$).

Output the answer to the problem modulo $$$998\: 244\: 853$$$.


Consider a linear function f(x) = Ax + B. Let's define g(0)(x) = x and g(n)(x) = f(g(n - 1)(x)) for n > 0. For the given integer values A, B, n and x find the value of g(n)(x) modulo 109 + 7.

The only line contains four integers A, B, n and x (1 ≤ A, B, x ≤ 109, 1 ≤ n ≤ 1018) — the parameters from the problem statement. Note that the given value n can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

Print the only integer s — the value g(n)(x) modulo 109 + 7.

#include <stdio.h>

#define nMod 1000000007

long long power(long long a, long long x)
{
    long long s;
    if (x == 1) return a;
    if (x == 0) return 1;
    s = power(a, x/2);
    s = (s * s) % nMod;
    if (x % 2 == 1)
        s = (s * a) % nMod;
    return s % nMod;
}

int main()
{
    long long a, b, n, x, s = 0,k;
    scanf("%I64d %I64d %I64d %I64d", &a, &b, &n, &x);
    s = (power(a,n)*x) % nMod;
    s = (s + b) % nMod;
    if (a == 1)
        s += (b*((n-1) % nMod));
    else
    s = (s + (b*((((power(a,n)-a) + nMod) %nMod)*(power(a-1,nMod-2))% nMod))% nMod) % nMod;
    k = power(a-1,nMod-2);
    printf("%I64d", (s + nMod) % nMod);
    return 0;
}

#include <stdio.h>

#define M	1000000007

struct matrix {
	int a, b, c, d;
} m;

int mult(int a, int b, int c, int d) {
	return ((long long) a * b % M + (long long) c * d % M) % M;
}

struct matrix power(struct matrix m, long long n) {
	struct matrix m_;
	int a, b, c, d;

	if (n == 0) {
		m_.a = 1;
		m_.b = 0;
		m_.c = 0;
		m_.d = 1;
	} else {
		m_ = power(m, n / 2);
		a = m_.a;
		b = m_.b;
		c = m_.c;
		d = m_.d;
		m_.a = mult(a, a, c, b);
		m_.b = mult(b, a, d, b);
		m_.c = mult(a, c, c, d);
		m_.d = mult(b, c, d, d);
		if (n % 2 != 0) {
			a = m_.a;
			b = m_.b;
			c = m_.c;
			d = m_.d;
			m_.a = mult(a, m.a, c, m.b);
			m_.b = mult(b, m.a, d, m.b);
			m_.c = mult(a, m.c, c, m.d);
			m_.d = mult(b, m.c, d, m.d);
		}
	}
	return m_;
}

int main() {
	int a, b, x;
	long long n;

	scanf("%d%d%lld%d", &a, &b, &n, &x);
	m.a = a;
	m.b = b;
	m.c = 0;
	m.d = 1;
	m = power(m, n);
	printf("%lld\n", (((long long) x * m.a % M) + m.b) % M);
	return 0;
}

#include <stdio.h>

#define M	1000000007

struct matrix {
	int a, b, c, d;
} m;

struct matrix power(struct matrix m, long long n) {
	struct matrix m_;
	int a, b, c, d;

	if (n == 0) {
		m_.a = 1;
		m_.b = 0;
		m_.c = 0;
		m_.d = 1;
	} else {
		m_ = power(m, n / 2);
		a = m_.a;
		b = m_.b;
		c = m_.c;
		d = m_.d;
		m_.a = ((long long) a * a % M + (long long) c * b % M) % M;
		m_.b = ((long long) b * a % M + (long long) d * b % M) % M;
		m_.c = ((long long) a * c % M + (long long) c * d % M) % M;
		m_.d = ((long long) b * c % M + (long long) d * d % M) % M;
		if (n % 2 != 0) {
			a = m_.a;
			b = m_.b;
			c = m_.c;
			d = m_.d;
			m_.a = ((long long) a * m.a % M + (long long) c * m.b % M) % M;
			m_.b = ((long long) b * m.a % M + (long long) d * m.b % M) % M;
			m_.c = ((long long) a * m.c % M + (long long) c * m.d % M) % M;
			m_.d = ((long long) b * m.c % M + (long long) d * m.d % M) % M;
		}
	}
	return m_;
}

int main() {
	int a, b, x;
	long long n;

	scanf("%d%d%lld%d", &a, &b, &n, &x);
	m.a = a;
	m.b = b;
	m.c = 0;
	m.d = 1;
	m = power(m, n);
	printf("%lld\n", (((long long) x * m.a % M) + m.b) % M);
	return 0;
}

/*
   Sachin Chandani
   IIIT Hyderabad
 */

#include<stdio.h>
#include<limits.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdbool.h>
# define M 1000000007
long long int fast_mul (long long int n,long long int x) ;
long long int m (long long int a) ;
long long int fast_mul (long long int n,long long int x) {
	long long int p ;
	if (x==0)
		return 1 ;
	else if (x==1)
		return n%M ;
	else
	{  p=fast_mul(n,x/2) ;
		p=(p*p)%M ;
		if (x%2==1)
			p=(p*n)%M ;
		return p ;
	}
}
long long int m (long long int a) {
	return fast_mul(a,M-2) ;
}
int main () {
	long long int a,b,n,x ,i,sum,aux,mmi ;
	scanf("%lld%lld%lld%lld",&a,&b,&n,&x) ;
	a=a%M ;
	x=x%M ;
	b=b%M ;
	aux=fast_mul(a,n) ;
	mmi=m(a-1) ;
	sum=(aux*x)%M ;
	if (a==1)
		n=n%M ;
	else
		n=((aux-1)*mmi)%M ;
	n=n%M ;
	n=(n*b)%M ;
	sum=(sum+n)%M ;
	printf("%lld\n",sum) ;
	return 0 ;
}

#include<stdio.h>
long long int a,b,n,x,y,z;
long long int mod = 1000000007;
long long int modpowfast(long long int base,long long int e)
{
	long long int ans=1;
	while(e>0)
	{
		if(e&1)
			ans=(ans*base)%mod;
		base=(base*base)%mod;
		e=e>>1;
	}
	return ans;
}

int main()
{
	long long int ans=0;
	scanf("%lld %lld %lld %lld",&a,&b,&n,&x);
	if(a==1)
	{
		n=n%mod;
		ans  =  (x + (n*b)%mod)%mod;
	}
	else
	{
		y = modpowfast(a,n);
		z = modpowfast(a-1,mod-2);
//		printf("%lld %lld\n",y,z);
		ans = ((y*x)%mod + (((y-1)*z)%mod)*b )%mod;
	}
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>

unsigned long long int m=1000000007;
unsigned long long int bigmod(unsigned long long int x, unsigned long long int n)
{
    if(n==0) return 1;

    if(n%2==0)
    {
    unsigned long long int num= bigmod(x,n/2);
    return ((num%m)*(num%m))%m;
    }

    else
        return ((x%m)*(bigmod(x,n-1)%m))%m;

}

int main()
{
    unsigned long long int  sum1=0,sum2=0,sum=0,n,A,B,x;

    scanf("%llu %llu %llu %llu",&A,&B,&n,&x);

    if(A==1) printf("%llu\n",((x%m)+((B%m)*(n%m))%m)%m);
    else{

    sum1=bigmod(A,n)%m;
    sum2=(sum1-1)%m;

    sum1=((sum1%m)*(x%m))%m;
    sum2=((sum2%m)*(B%m))%m;

    sum2=((sum2%m)* (bigmod(A-1,m-2))%m)%m;

    sum=((sum1%m)+(sum2%m))%m;

    printf("%llu\n",sum%m);

    }
    return 0;
}

#include<stdio.h>
const int mod = 1e9+7;
typedef long long ll;
ll quick_pow(ll a, ll b)
{
    ll ans = 1;
    while(b)
    {
        if(b&1)
        {
            ans = (ans * a) % mod;
        }
        a = (a*a) % mod;
        b >>= 1;
    }
    return ans;
}
ll inv(int x,int p)
{
    return quick_pow(x,p-2);
}
int main()
{
    ll a, b, x;
    ll n,ans, s, A, B;
    scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&x);
    {
        if(a == 1)
        {
            ans = (x + (n%mod)*b) % mod;
        }
        else if(n == 1)
        {
            ans = (a*x+b*n%mod) % mod;
        }
        else
        {
            ans = (quick_pow(a,n) * x) % mod; //a^n * b
            s = (  (quick_pow(a,n) - 1) * b) % mod;
            A = inv(a-1,mod);
            ans = (ans+A*s)%mod;
        }
    }
    printf("%I64d\n",ans);
}

#include <stdio.h>
#include <stdlib.h>
long long pp=1e9+7;
long long VLL(long long a,long long b,long long p)
{
    long long ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%p;
        a=a*a%p;
        b>>=1;
    }
    return ans;
}
int main()
{
    long long a,b,c,d,x,m,n;
    scanf("%lld %lld %lld %lld",&a,&b,&n,&x);
    if(a!=1)
    {
        c=VLL(a,n,pp); d=VLL(a-1,pp-2,pp);
        d=(((c-1)%pp)*(d%pp))%pp;
    }
    else
        d=n,c=1;
    d=((d%pp)*(b%pp))%pp;
    c=(((c%pp)*(x%pp))%pp+d)%pp;
    printf("%lld\n",c);
    return 0;
}

#include<stdio.h>

long long m=1000000007;

long long big(long long a,long long n)
{
    long long x=1;
    while(n)
    {
        if(n%2) x = (x*a)%m;
        a = (a*a)%m;
        n = n/2;
    }
    return x%m;
}

long long inv(long long a,long long n)
{
    long long p,q,r,s;
    if(a==1)    return (n%m);
    else
    {
        p = big(a,n)%m;
        q = p-1;
        r = big(a-1,m-2)%m;
        s = (q*r)%m;
        return s;
    }
}

int main()
{
    long long a,b,n,x,p,q,r;
    scanf("%lld %lld %lld %lld",&a,&b,&n,&x);
    p = (big(a,n)*(x%m))%m;
    q = (inv(a,n)*b)%m;
    r = (p+q)%m;
    printf("%lld\n",r);
    return 0;
}

#include <stdio.h>
#define M 1000000007
long long int fast_mul (long long int n,long long int x) {
	long long int p ;
	if (x==0)
	  return 1 ;
	else if (x==1)
	  return n%M ;
	else
	{  p=fast_mul(n,x/2) ;
	   p=(p*p)%M ;
	   if (x%2==1)
	     p=(p*n)%M ;
	   return p ;
	}
}
long long int m (long long int a) {
	return fast_mul(a,M-2) ;
}
int main () {
	long long int a,b,n,x ,i,sum,aux,mmi ;
	scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&x) ;
	a=a%M ;
	x=x%M ;
	b=b%M ;
	aux=fast_mul(a,n) ;
	mmi=m(a-1) ;
	sum=(aux*x)%M ;
	if (a==1)
	 n=n%M ;
	else
	n=((aux-1)*mmi)%M ;
	n=n%M ;
	n=(n*b)%M ;
	sum=(sum+n)%M ;
	printf("%I64d\n",sum) ;
	return 0 ;
}

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define MOD 1000000007
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

long long expo(long long x, long long n){
    x %= MOD;
    long long res = 1;

    while (n){
        if (n & 1LL) res = (res * x) % MOD;
        x = (x * x) % MOD;
        n >>= 1LL;
    }

    return (res % MOD);
}

long long sumpow(long long x, long long n){
    if (n == 1) return (x % MOD);
    if (n & 1) return (sumpow(x, n - 1) + expo(x, n)) % MOD;
    else{
        long long r = sumpow(x, n >> 1);
        return (r + (r * expo(x, n >> 1))) % MOD;
    }
}

int main(){
    long long a, b, n, x, y, z, res;

    while (scanf("%lld %lld %lld %lld", &a, &b, &n, &x) != EOF){
        res = (expo(a, n) * x + b) % MOD;
        if (n > 1) res = (res + (b * sumpow(a, n - 1))) % MOD;
        printf("%lld\n", res % MOD);
    }
    return 0;
}

/*
 * 678D
 *
 * (A-1)y = (A-1)xA^n + BA^n - B
 */
#include <stdio.h>
#include <stdlib.h>
#ifndef _TJYTLXWXHYZQFW_COMMON_H
#define _TJYTLXWXHYZQFW_COMMON_H
#include <limits.h>
#define offset(t,m) ((unsigned long)&(((t*)0)->m))
#define container_of(a,t,m) ((t*)((unsigned long)a-offset(t,m)))
#define ULL1 ((unsigned long long )1)
#define LL(x) ((long long)(x))
#define SWP(a, b) if ((a) != (b)) do {(a) ^= (b); (b) ^= (a); (a) ^= (b);} while(0)
#define MAX(x,y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define IOF(p) ((p) = ((p) < 0 ? INT_MAX : (p)))
#define LLOF(p) ((p) = ((p) < 0 ? LLONG_MAX :(p)))
#define IR(x, start, end) ((x) >= (start) && (x) < (end))
#define IRE(x, start, end) ((x) >= (start) && (x) <= (end))
#define ABS(x) ((x) < 0 ? (x) * (-1) : (x))
#define forn(i, n) for ((i) = 0; (i) < (n); (i) += 1)
#define fore(i, n) for ((i) = 0; (i) <= (n); (i) += 1)
#define forr(i, s, e) for((i) = (s); (i) < (e); (i) += 1)
#define forre(i, s, e) for((i) = (s); (i) <= (e); (i) += 1)
#endif
#ifndef _WCC_NUMTH_H
#define _WCC_NUMTH_H
#include <assert.h>
#define NUMTH_CORRECT(x, mod) ((x) += ((x) < 0 ? (mod) : 0))
#define NUMTH_ENS (-1)
long long numth_modexp(long long a, long long b, long long n);
long long numth_gcd(long long a, long long b);
void numth_gcd_e(long long a, long long b, long long *d, long long *x, long long *y);
int numth_mle(long long a, long long b, long long n, long long *x, long long *gcd);
#define numth_for_each_solution(i, x, n, d) \
	for ((i) = 0; (i) < (d); (i) += 1, (x) = ((x) + (n) / (d)) % (n))
int numth_divisors(long long k, long long divs[], long long *ndivs);
#endif
#define P LL(1000000007)
/* x, y, n must not be negitive */
#define modmul(x, y, n) ((((x) % (n)) * ((y) % (n))) % (n))
#define modpls(x, y, n) ((((x) % (n)) + ((y) % (n))) % (n))
int main(void)
{
	long long A, B, n, x;
	long long right, q, ans, d;
	scanf("%lld%lld%lld%lld", &A, &B, &n, &x);
	if (A == 1) {
		ans = modpls(x, modmul(B, n, P), P);
		goto print_answer;
	}
	q = numth_modexp(A, n, P);
	//printf("q: %lld\n", q);
	right = modmul(modmul(A - 1, x, P), q, P) + modmul(B, q, P) - B;
	//printf("right: %lld\n", right);
	NUMTH_CORRECT(right, P);
	numth_mle(A - 1, right, P, &ans, &d);
	assert(d == 1);
	print_answer:
	printf("%lld\n", ans);
	return 0;
}

static long long numth_do_gcd(long long a, long long b);
static void numth_do_gcd_e(long long grt, long long les, long long *d, long long *x, long long *y);

long long numth_modexp(long long a, long long b, long long n)
{
	static long long bit63 = (unsigned long long)0x8000000000000000;
	long long square = 1;
	int i;

	assert(b >= 0 && n > 0);

	//FIXME: '==' > '&'
	for(i = 63; i >= 0 && (b & bit63) == 0; --i)
		b <<= 1;
	for(; i >= 0; --i) {
		square = (square * square) % n;
		if (b & bit63)
			square = (square * a) % n;
		b <<= 1;
	}

	return square;
}

long long numth_gcd(long long a, long long b)
{
	if (a == 0 && b == 0)
		return 0;
	if (a < 0)
		a *= -1;
	if (b < 0)
		b *= -1;
	/* We DON'T need a > b */
	return numth_do_gcd(a, b);
}

/* 大数除小数, 小数除余数 */
static long long numth_do_gcd(long long a, long long b)
{
	long long r;

	if (!b)
		return a;

	for (r = a % b; r != 0; r = a % b) {
		a = b;
		b = r;
	}

	return b;
}

void numth_gcd_e(long long a, long long b, long long *d, long long *x, long long *y)
{
	if (a == 0 && b == 0) {
		*d = *x = *y = 0;
		return;
	}

	if (a < 0)
		a *= -1;
	if (b < 0)
		b *= -1;

	if (a < b)
		numth_do_gcd_e(b, a, d, y, x);
	else
		numth_do_gcd_e(a, b, d, x, y);
}

static void numth_do_gcd_e(long long a, long long b, long long *d, long long *x, long long *y)
{
	long long tmp;

	if (b == 0) {
		*d = a;
		*x = 1;
		*y = 0;
		return;
	}
	numth_do_gcd_e(b, a % b, d, x, y);
	tmp = *x;
	*x = *y;
	//TODO: overflow?
	*y = (tmp - (a / b) * (*y));
}

int numth_mle(long long a, long long b, long long n, long long *solution, long long *gcd)
{
	long long d, x, y;

	assert(n != 0);

	if (n < 0)
		n *= (-1);
	if (a < 0)
		a *= (-1);
	if (b < 0)
		b *= (-1);

	numth_gcd_e(a, n, &d, &x, &y);
	assert(d != 0); //d=0 if and only if a=b=0

	if (b % d)
		return NUMTH_ENS;

	*solution = ((b / d) * (x % n)) % n;
	NUMTH_CORRECT(*solution, n);
	*gcd = d;

	return 0;
}

int numth_divisors(long long k, long long divs[], long long *ndivs)
{
	long long i, n;

	assert(k > 0);

	n = 0;
	for (i = 1; i * i < k; ++i)
		if (k % i == 0)
			divs[n++] = i;

	if (i * i == k) {
		divs[n++] = i;
		i = n - 2;
	} else {
		i = n - 1;
	}

	for (; i >= 0; --i)
		divs[n++] = k / divs[i];

	*ndivs = n;

	return 0;
}

#include <stdio.h>
# define m 1000000007
long long int power(long long int x,long long int y)
{
	if(y==0)
	return 1;
	else if(y%2==0)
	{
		long long int z;
		z=power(x,y/2)%m;
		return (z*z)%m;
	}
	else
	{
		long long int z;
		z=power(x,y/2)%m;
		return ((x%m*z%m)%m*z)%m;
	}
}
int main()
{
	long long int n,a,b,x,i,ans=0;
	scanf("%lld %lld %lld %lld",&a,&b,&n,&x);
       if(a!=1)
{
//	for(i=0;i<n;i++)
//	{
		ans=(power(a,n)-1)%m;
		if(ans<0)
		ans=ans+m;
		ans=(ans%m*b%m)%m;
		if(ans<0)
		ans+=m;
		if(a-1>0)
		ans=((ans%m)*power(a-1,m-2)%m)%m;
		 ans=ans%m;
//	}
	ans=(ans+(power(a,n)%m*x%m)%m)%m;
	ans=ans%m;}
else
ans=((n%m*b%m)%m+x)%m;
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>
#define MOD 1000000007

int main(){
	long long int n,a,b,x;
	scanf("%lld%lld%lld%lld",&a,&b,&n,&x);
	while(n>0){
		if(n%2){
			x=a*x+b;
			x%=MOD;
		}
		b=a*b+b;
		a=a*a;
		a%=MOD;
		b%=MOD;
		n/=2;
	}
	printf("%lld\n",x);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//input definitions
#define sd(x) scanf("%d",&x);
#define slld(x) scanf("%lld",&x);
#define ss(x) scanf("%s",x);
#define sf(x) scanf("%f",&x);
#define slf(x) scanf("%lf",&x);

//output definitions
#define pd(x) printf("%d",x);
#define plld(x) printf("%lld",x);
#define ps(x) printf("%s",x);
#define pf(x) printf("%f",x);
#define plf(x) printf("%lf",x);
#define pnl printf("\n");
#define psp printf(" ");

//define datatypes
typedef long long int ll;

//loops
#define upto(x) for(;i<=x;i++)
#define fl(i,a,b,c) for(i=a;i<=b;i+=c)
#define wl(x) while(x--)

#define MOD 1000000007

ll power(ll a, ll b){
	ll ans = 1;
	while(b > 0){
		if(b & 1){
			ans = (ans * a) % MOD;
		}
		a = (a*a) % MOD;
		b/=2;
	}
	return ans%MOD;
}

int main(void) {
	ll a,b,c,d,n,x;
	slld(a) slld(b) slld(n) slld(x)
	if(a == 1){
		plld((x+((n%MOD)*b)%MOD)%MOD)
	}
	else if(n == 1){
		plld(((a*x)%MOD + b) % MOD)
	}
	else{
		ll ans = (power(a,n) * x) % MOD;
		ans = (ans + (((((power(a,n) - 1 + MOD) % MOD) * power(a-1,MOD-2)) % MOD) * b) % MOD ) % MOD;
		plld(ans)
	}
	return 0;

}

#include<stdio.h>
#include<string.h>
#include<math.h>
typedef unsigned long long LL;
LL m=1000000007;

LL bigmod(LL a,LL b, LL m)
{
    if(b==0)    return 1%m;
    LL x=bigmod(a,b/2,m);
    x=(x*x)%m;
    if(b%2==1)
        x=(x*a)%m;
    return x;
}
int main()
{
    LL n, a, b, x, f, ans, f2, f3;
    char  s[200010];
    scanf("%llu %llu %llu %llu",&a,&b,&n,&x);
    a=a%m;
    b=b%m;
    f = bigmod(a, n, m);
    f2 = (f * (x % m)) % m;
    if(a==1)
    {
        ans = (f2 + (b* (n%m) )% m) % m;
    }
    else
    {
        f3 = bigmod(a-1, m-2, m);
        //ans = (f2+(b*((f-1)*f3)%m)%m)%m;
        ans = (b * (f-1))%m;
        ans = (ans* f3)%m;
        ans = (ans+ f2) %m;
    }

    printf("%llu\n",ans);
    return 0;

}

    #include <stdio.h>
	int mod=1000000007;
    long long int power(long long int base,long long int exponent)
    {
    	long long int res=1;
    	while(exponent)
    	{
    		if(exponent%2)
    			res=(res*base)%mod;
    		base=(base*base)%mod;
    		exponent=exponent/=2;
    	}
    	return res%mod;
    }

    long long int geometricsum(long long int r,long long int n)
    {
    	if(r==1)
    		return n%mod;
    	return (1-power(r,n))*power(1-r,mod-2)%mod;
    }

    int main()
    {
    	long long int a,b,n,x;
    	scanf("%lld %lld %lld %lld", &a,&b,&n,&x);
    	long long int answer=(power(a,n)*x+b*geometricsum(a,n))%mod;
    	printf("%lld", answer);
    	return 0;

    }

#include <stdio.h>
typedef long long ll;
ll MOD = 1000000007 ;
ll MOD2 = 1000000006 ;

ll gcdExtended(ll a, ll b, ll *x, ll *y);

ll power(ll x, ll y, ll p) ;

// Function to find modulo inverse of a
ll modInverse(ll a, ll m)
{
    ll x, y;
    ll g = gcdExtended(a, m, &x, &y);

        // m is added to handle negative x
        ll res = (x%m + m) % m;
        return res ;

}

// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
    // Base Case
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1; // To store results of recursive call
    ll gcd = gcdExtended(b%a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;
        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

ll main(ll argc, char const *argv[])
{
    ll A,B,n,x;
    scanf("%lld %lld %lld %lld",&A,&B,&n,&x);
    ll answer = 0;
    ll apart = 1;
    ll itna = n%(MOD2);
    if(itna==0)
    {
        printf("%lld\n",x%MOD);
    }
    else
    {
    ll second = 1;
    if(A!=1)
    {
    ll dot = power(A,itna,MOD);
    answer = (answer +((dot*x) % MOD))%MOD ;
    ll ert  = dot-1 ;
    ll gh = A-1 ;
    answer = (answer+(((modInverse(A-1,MOD)*ert)%MOD)*B)%MOD)%MOD;
    }
    else if(A==1)
    {
        answer =  (answer + x)%MOD ;
    //    printf("yele %lld\n\n",itna);
        answer = (answer + ((B%MOD)*(n%MOD)%MOD))%MOD ;
    }
    //prllf("%lld\n",);
    printf("%lld\n",answer);
    }
    return 0;
}

#include<stdio.h>
#include<math.h>
long long int power(long long int x,long long int y)
{
	int hel=1;
	x= x%1000000007;
	while(y>0)
	{
		if((y&1)==1)
		{
			hel=(hel*x)%1000000007;
		}
		x= (x*x)%1000000007;
		y= y>>1;
	}
	return hel%1000000007;
}
long long int cal(long long int x,long long int y)
{
	if(x==1)
	{
		return y%1000000007;
	}
	else
	{
		return (1-power(x,y))*power(1-x,1000000005)%1000000007;
	}
}

int main()
{
	long long int a,b,n,x,sum;
	scanf("%lld%lld%lld%lld",&a,&b,&n,&x);
	sum=(power(a,n)*x+(b*cal(a,n)))%1000000007;
	printf("%lld\n",sum);
	return 0;
}

long long int modular_pow(long long int base, long long int exponent, long long int modulus)
{
    long long int result = 1;
    while (exponent > 0)
    {
        if (exponent % 2 == 1)
            result = (result * base) % modulus;
        exponent = exponent >> 1;
        base = (base * base) % modulus;
    }
    return result;
}
int powercal(int x, unsigned int y)
{
    int res = 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = res*x;

        // n must be even now
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}
int main ()
{
    long long int  n,a,b,x,ans,e12,ef,e,e1,e2,pr,en;
    long long int p=1000000007;
    scanf ("%I64d %I64d %I64d %I64d",&a,&b,&n,&x);
    //printf ("%I64d\n",(-1)%3);
    if (a!=1)
    {
        e1=b%p;
        pr=modular_pow(a,n,p);//(a^n)%p
        e2=(pr+(p-1))%p;
        //printf ("%I64d ",e2);// (a^n-1) %p
        en=modular_pow(a-1,p-2,p);
        e2=((e2%p)*en)%p;
        e12=(e1*e2)%p;
        e=((x%p)*pr)%p;
        ef=(e12+e)%p;
        printf ("%I64d",ef);
        return 0;
    }
    else
    {
        e=((n%p)*(b%p))%p;
        ef=(e+(x%p))%p;
      printf ("%I64d",ef);
      return 0;
    }
}

#include<stdio.h>
#include<math.h>
long long n;
long long a,b,x;
long long que[100];
int q;
int main()
{
	scanf("%lld%lld%lld%lld",&a,&b,&n,&x);
	long long an=1;
	long long chuc=a-1;
	long long two=pow(2,62);
	long long mod=pow(10,9)+7;
	if(a==1){
		printf("%lld",(x+(b*(n%mod)%mod))%mod);
	}
	else{
		while(n>0){
			if(n>=two){
				n-=two;
				q++;
				que[q]=two;
			}
			two=two/2;
		}
		two=1;
		long long c=a;
		while(q>0){
			if(two==que[q]){
				an=(an*a)%mod;
				q--;
			}
			a=(a*a)%mod;
			two=two*2;
		}
		int chumod=mod-2;
		two=pow(2,50);
		while(chumod>0){
			if(chumod>=two){
				chumod-=two;
				q++;
				que[q]=two;
			}
			two=two/2;
		}
		two=1;
		long long chu=1;
		while(q>0){
			if(two==que[q]){
				chu=(chu*chuc)%mod;
				q--;
			}
			chuc=(chuc*chuc)%mod;
			two=two*2;
		}
		printf("%lld",((an*x)%mod+((((an-1)*chu)%mod)*b)%mod)%mod);
	}
	return 0;
 }

#include <stdio.h>

#define nMod 1000000007

long long power(long long a, long long x)
{
    long long s;
    if (x == 1) return a;
    if (x == 0) return 1;
    s = power(a, x/2);
    s = (s * s) % nMod;
    if (x % 2 == 1)
        s = (s * a) % nMod;
    return s % nMod;
}

int main()
{
    long long a, b, n, x, s = 0,k;
    scanf("%I64d %I64d %I64d %I64d", &a, &b, &n, &x);
    s = (power(a,n)*x) % nMod;
    s = (s + b) % nMod;
//    k = (((power(a,n)-a) + nMod) % nMod) /(a-1);
    if (a == 1) s += n-1;
    else
    s = (s + (b*(((((power(a,n)-a) + nMod) %nMod)/(a-1))% nMod))% nMod) % nMod;
    printf("%I64d", (s + nMod) % nMod);
    return 0;
}

#include <stdio.h>

#define nMod 1000000007

long long power(long long a, long long x)
{
    long long s;
    if (x == 1) return a;
    if (x == 0) return 1;
    s = power(a, x/2);
    s = (s * s) % nMod;
    if (x % 2 == 1)
        s = (s * a) % nMod;
    return s % nMod;
}

int main()
{
    long long a, b, n, x, s = 0,k;
    scanf("%I64d %I64d %I64d %I64d", &a, &b, &n, &x);
    s = (power(a,n)*x) % nMod;
    s = (s + b) % nMod;
//    k = (((power(a,n)-a) + nMod) % nMod) /(a-1);
    if (a == 1) s += n;
    else
    s = (s + (b*(((((power(a,n)-a) + nMod) %nMod)/(a-1))% nMod))% nMod) % nMod;
    printf("%I64d", (s + nMod) % nMod);
    return 0;
}

#include <stdio.h>

#define nMod 1000000007

long long power(long long a, long long x)
{
    long long s;
    if (x == 1) return a;
    if (x == 0) return 1;
    s = power(a, x/2);
    s = (s * s) % nMod;
    if (x % 2 == 1)
        s = (s * a) % nMod;
    return s % nMod;
}

int main()
{
    long long a, b, n, x, s = 0,k;
    scanf("%I64d %I64d %I64d %I64d", &a, &b, &n, &x);
    s = (power(a,n)*x) % nMod;
    s = (s + b) % nMod;
//    k = (((power(a,n)-a) + nMod) % nMod) /(a-1);
    if (a == 1) s += n-1;
    else
    s = (s + (b*((((power(a,n)-a) + nMod) %nMod)*(power(a-1,nMod-2))% nMod))% nMod) % nMod;
    printf("%I64d", (s + nMod) % nMod);
    return 0;
}

#include <stdio.h>

#define M	1000000007

struct matrix {
	int a, b, c, d;
} m;

struct matrix power(struct matrix m, int n) {
	struct matrix m_;
	int a, b, c, d;

	if (n == 0) {
		m_.a = 1;
		m_.b = 0;
		m_.c = 0;
		m_.d = 1;
	} else {
		m_ = power(m, n / 2);
		a = m_.a;
		b = m_.b;
		c = m_.c;
		d = m_.d;
		m_.a = ((long long) a * a % M + (long long) c * b % M) % M;
		m_.b = ((long long) b * a % M + (long long) d * b % M) % M;
		m_.c = ((long long) a * c % M + (long long) c * d % M) % M;
		m_.d = ((long long) b * c % M + (long long) d * d % M) % M;
		if (n % 2 != 0) {
			a = m_.a;
			b = m_.b;
			c = m_.c;
			d = m_.d;
			m_.a = ((long long) a * m.a % M + (long long) c * m.b % M) % M;
			m_.b = ((long long) b * m.a % M + (long long) d * m.b % M) % M;
			m_.c = ((long long) a * m.c % M + (long long) c * m.d % M) % M;
			m_.d = ((long long) b * m.c % M + (long long) d * m.d % M) % M;
		}
	}
	return m_;
}

int main() {
	int a, b, n, x;

	scanf("%d%d%d%d", &a, &b, &n, &x);
	m.a = a;
	m.b = b;
	m.c = 0;
	m.d = 1;
	m = power(m, n);
	printf("%lld\n", ((long long) x * m.a + m.b) % M);
	return 0;
}

#include <stdio.h>

#define M	1000000007

struct matrix {
	int a, b, c, d;
} m;

struct matrix power(struct matrix m, int n) {
	struct matrix m_;
	int a, b, c, d;

	if (n == 0) {
		m_.a = 1;
		m_.b = 0;
		m_.c = 0;
		m_.d = 1;
	} else {
		m_ = power(m, n / 2);
		a = m_.a;
		b = m_.b;
		c = m_.c;
		d = m_.d;
		m_.a = ((long long) a * a % M + (long long) c * b % M) % M;
		m_.b = ((long long) b * a % M + (long long) d * b % M) % M;
		m_.c = ((long long) a * c % M + (long long) c * d % M) % M;
		m_.d = ((long long) b * c % M + (long long) d * d % M) % M;
		if (n % 2 != 0) {
			a = m_.a;
			b = m_.b;
			c = m_.c;
			d = m_.d;
			m_.a = ((long long) a * m.a % M + (long long) c * m.b % M) % M;
			m_.b = ((long long) b * m.a % M + (long long) d * m.b % M) % M;
			m_.c = ((long long) a * m.c % M + (long long) c * m.d % M) % M;
			m_.d = ((long long) b * m.c % M + (long long) d * m.d % M) % M;
		}
	}
	return m_;
}

int main() {
	int a, b, n, x;

	scanf("%d%d%d%d", &a, &b, &n, &x);
	m.a = a;
	m.b = b;
	m.c = 0;
	m.d = 1;
	m = power(m, n);
	printf("%lld\n", (long long) x * m.a + m.b);
	return 0;
}

#include <stdio.h>

#define M	1000000007

struct matrix {
	int a, b, c, d;
} m;

struct matrix power(struct matrix m, int n) {
	struct matrix m_;
	int a, b, c, d;

	if (n == 0) {
		m_.a = 1;
		m_.b = 0;
		m_.c = 0;
		m_.d = 1;
	} else {
		m_ = power(m, n / 2);
		a = m_.a;
		b = m_.b;
		c = m_.c;
		d = m_.d;
		m_.a = ((long long) a * a % M + (long long) c * b % M) % M;
		m_.b = ((long long) b * a % M + (long long) d * b % M) % M;
		m_.c = ((long long) a * c % M + (long long) c * d % M) % M;
		m_.d = ((long long) b * c % M + (long long) d * d % M) % M;
		if (n % 2 != 0) {
			a = m_.a;
			b = m_.b;
			c = m_.c;
			d = m_.d;
			m_.a = ((long long) a * m.a % M + (long long) c * m.b % M) % M;
			m_.b = ((long long) b * m.a % M + (long long) d * m.b % M) % M;
			m_.c = ((long long) a * m.c % M + (long long) c * m.d % M) % M;
			m_.d = ((long long) b * m.c % M + (long long) d * m.d % M) % M;
		}
	}
	return m_;
}

int main() {
	int a, b, n, x;

	scanf("%d%d%d%d", &a, &b, &n, &x);
	m.a = a;
	m.b = b;
	m.c = 0;
	m.d = 1;
	m = power(m, n);
	printf("%lld\n", (((long long) x * m.a % M) + m.b) % M);
	return 0;
}

#include <stdio.h>

#define M	1000000007

struct matrix {
	int a, b, c, d;
} m;

struct matrix power(struct matrix m, int n) {
	struct matrix m_;
	int a, b, c, d;

	if (n == 0) {
		m_.a = 1;
		m_.b = 0;
		m_.c = 0;
		m_.d = 1;
	} else {
		m_ = power(m, n / 2);
		a = m_.a;
		b = m_.b;
		c = m_.c;
		d = m_.d;
		m_.a = ((long long) a * a % M + (long long) c * b % M) % M;
		m_.b = ((long long) b * a % M + (long long) d * b % M) % M;
		m_.c = ((long long) a * c % M + (long long) c * d % M) % M;
		m_.d = ((long long) b * c % M + (long long) d * d % M) % M;
		if (n % 2 != 0) {
			a = m_.a;
			b = m_.b;
			c = m_.c;
			d = m_.d;
			m_.a = ((long long) a * m.a % M + (long long) c * m.b % M) % M;
			m_.b = ((long long) b * m.a % M + (long long) d * m.b % M) % M;
			m_.c = ((long long) a * m.c % M + (long long) c * m.d % M) % M;
			m_.d = ((long long) b * m.c % M + (long long) d * m.d % M) % M;
		}
	}
	return m_;
}

int main() {
	int a, b, x;
	long long n;

	scanf("%d%d%lld%d", &a, &b, &n, &x);
	m.a = a;
	m.b = b;
	m.c = 0;
	m.d = 1;
	m = power(m, n);
	printf("%lld\n", (((long long) x * m.a % M) + m.b) % M);
	return 0;
}

/*
   Sachin Chandani
   IIIT Hyderabad
   		*/

#include<stdio.h>
#include<limits.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdbool.h>
int min(int a,int b);
int cmp(const void *a,const void *b)
{
	return(*(int*)a-*(int*)b);

}
int min(int a,int b)
{
	return(a>b?b:a);
}

int main()
{
	long long int A,B,n,x;
	scanf("%lld%lld%lld%lld",&A,&B,&n,&x);
	long long int ans,temp;
	if(A==1)
	{
		printf("%lld",(x+(n*B)%1000000007)%1000000007);
		return 0;
	}
	ans=((long long int)(pow(A,n)))%1000000007;
	ans=((ans*x)%1000000007);
	temp=((B*(((long long int)(pow(A,n))-1)))/(A-1))%1000000007;
	ans=(ans+temp)%1000000007;
	printf("%lld",ans);
	return 0;
}

#include<stdio.h>
long long int a,b,n,x,y,z;
long long int mod = 1000000007;
long long int modpowfast(long long int base,long long int e)
{
	long long int ans=1;
	while(e>0)
	{
		if(e&1)
			ans=(ans*base)%mod;
		base=(base*base)%mod;
		e=e>>1;
	}
	return ans;
}

int main()
{
	long long int ans=0;
	scanf("%lld %lld %lld %lld",&a,&b,&n,&x);
	n=n%mod;
	if(a==1)
	{
		ans  =  (x + (n*b)%mod)%mod;
	}
	else
	{
		y = modpowfast(a,n);
		z = modpowfast(a-1,mod-2);
//		printf("%lld %lld\n",y,z);
		ans = ((y*x)%mod + (((y-1)*z)%mod)*b )%mod;
	}
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>
long long int a,b,n,x,y,z;
long long int mod = 1000000007;
long long int modpowfast(long long int base,long long int e)
{
	long long int ans=1;
	while(e>0)
	{
		if(e&1)
			ans=(ans*base)%mod;
		base=(base*base)%mod;
		e=e>>1;
	}
	return ans;
}

int main()
{
	long long int ans=0;
	scanf("%lld %lld %lld %lld",&a,&b,&n,&x);
	if(a==1)
	{
		ans  =  (x + (n*b)%mod)%mod;
	}
	else
	{
		y = modpowfast(a,n);
		z = modpowfast(a-1,mod-2);
//		printf("%lld %lld\n",y,z);
		ans = ((y*x)%mod + (((y-1)*z)%mod)*b )%mod;
	}
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>

unsigned long long int m=1000000007;
unsigned long long int bigmod(unsigned long long int x, unsigned long long int n)
{
    if(n==0) return 1;

    if(n%2==0)
    {
    unsigned long long int num= bigmod(x,n/2);
    return ((num%m)*(num%m))%m;
    }

    else
        return ((x%m)*(bigmod(x,n-1)%m))%m;

}

int main()
{
    unsigned long long int  sum1=0,sum2=0,sum=0,n,A,B,x;

    scanf("%llu %llu %llu %llu",&A,&B,&n,&x);

    sum1=bigmod(A,n)%m;
    sum2=(sum1-1)%m;

    sum1=((sum1%m)*(x%m))%m;
    sum2=((sum2%m)*(B%m))%m;

    sum2=((sum2%m)* (bigmod(A-1,m-2))%m)%m;

    sum=((sum1%m)+(sum2%m))%m;

    printf("%llu\n",sum%m);

    return 0;
}

#include <stdio.h>
# define m 1000000007
long long int power(long long int x,long long int y)
{
	if(y==0)
	return 1;
	else if(y%2==0)
	{
		long long int z;
		z=power(x,y/2)%m;
		return (z*z)%m;
	}
	else
	{
		long long int z;
		z=power(x,y/2)%m;
		return ((x%m*z%m)%m*z)%m;
	}
}
int main()
{
	long long int n,a,b,x,i,ans=0;
	scanf("%lld %lld %lld %lld",&a,&b,&n,&x);
//	for(i=0;i<n;i++)
//	{
		ans=ans+(power(a,n)-1);
		if(ans<0)
		ans=ans+m;
		ans=(ans%m*b%m)%m;
		ans=((ans%m)*power(a-1,m-2)%m)%m;
//	}
	ans=ans+(power(a,n)%m*x%m)%m;
	ans=ans%m;
	printf("%lld\n",ans);
	return 0;
}

#include <stdio.h>
# define m 1000000007
long long int power(long long int x,long long int y)
{
	if(y==0)
	return 1;
	else if(y%2==0)
	{
		long long int z;
		z=power(x,y/2)%m;
		return (z*z)%m;
	}
	else
	{
		long long int z;
		z=power(x,y/2)%m;
		return ((x%m*z%m)%m*z)%m;
	}
}
int main()
{
	long long int n,a,b,x,i,ans=0;
	scanf("%lld %lld %lld %lld",&a,&b,&n,&x);
//	for(i=0;i<n;i++)
//	{
		ans=(power(a,n)-1);
		if(ans<0)
		ans=ans+m;
		ans=(ans%m*b%m)%m;
		if(ans<0)
		ans+=m;
		if(a-1>0)
		ans=((ans%m)*power(a-1,m-2)%m)%m;
//	}
	ans=ans+(power(a,n)%m*x%m)%m;
	ans=ans%m;
	printf("%lld\n",ans);
	return 0;
}

#include <stdio.h>
# define m 1000000007
long long int power(long long int x,long long int y)
{
	if(y==0)
	return 1;
	else if(y%2==0)
	{
		long long int z;
		z=power(x,y/2)%m;
		return (z*z)%m;
	}
	else
	{
		long long int z;
		z=power(x,y/2)%m;
		return ((x%m*z%m)%m*z)%m;
	}
}
int main()
{
	long long int n,a,b,x,i,ans=0;
	scanf("%lld %lld %lld %lld",&a,&b,&n,&x);
//	for(i=0;i<n;i++)
//	{
		ans=(power(a,n)-1)%m;
		if(ans<0)
		ans=ans+m;
		ans=(ans%m*b%m)%m;
		if(ans<0)
		ans+=m;
		if(a-1>0)
		ans=((ans%m)*power(a-1,m-2)%m)%m;
		 ans=ans%m;
//	}
	ans=(ans+(power(a,n)%m*x%m)%m)%m;
	ans=ans%m;
	printf("%lld\n",ans);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//input definitions
#define sd(x) scanf("%d",&x);
#define slld(x) scanf("%lld",&x);
#define ss(x) scanf("%s",x);
#define sf(x) scanf("%f",&x);
#define slf(x) scanf("%lf",&x);

//output definitions
#define pd(x) printf("%d",x);
#define plld(x) printf("%lld",x);
#define ps(x) printf("%s",x);
#define pf(x) printf("%f",x);
#define plf(x) printf("%lf",x);
#define pnl printf("\n");
#define psp printf(" ");

//define datatypes
typedef long long int ll;

//loops
#define upto(x) for(;i<=x;i++)
#define fl(i,a,b,c) for(i=a;i<=b;i+=c)
#define wl(x) while(x--)

#define MOD 1000000007

ll power(ll a, ll b){
	ll ans = 1;
	while(b > 0){
		if(b & 1){
			ans = (ans * a) % MOD;
		}
		a = (a*a) % MOD;
		b/=2;
	}
	return ans%MOD;
}

int main(void) {
	ll a,b,c,d,n,x;
	slld(a) slld(b) slld(n) slld(x)
	ll ans = (power(a,n) * x) % MOD;
	ans = (ans + (((((power(a,n) - 1 + MOD) % MOD) * power(a-1,MOD-2)) % MOD) * b) % MOD ) % MOD;
	plld(ans)
	return 0;

}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//input definitions
#define sd(x) scanf("%d",&x);
#define slld(x) scanf("%lld",&x);
#define ss(x) scanf("%s",x);
#define sf(x) scanf("%f",&x);
#define slf(x) scanf("%lf",&x);

//output definitions
#define pd(x) printf("%d",x);
#define plld(x) printf("%lld",x);
#define ps(x) printf("%s",x);
#define pf(x) printf("%f",x);
#define plf(x) printf("%lf",x);
#define pnl printf("\n");
#define psp printf(" ");

//define datatypes
typedef long long int ll;

//loops
#define upto(x) for(;i<=x;i++)
#define fl(i,a,b,c) for(i=a;i<=b;i+=c)
#define wl(x) while(x--)

#define MOD 1000000007

ll power(ll a, ll b){
	ll ans = 1;
	if(b == 1)
		return a;
	while(b > 0){
		if(b & 1){
			ans = (ans * a) % MOD;
		}
		a = (a*a) % MOD;
		b/=2;
	}
	return ans%MOD;
}

int main(void) {
	ll a,b,c,d,n,x;
	slld(a) slld(b) slld(n) slld(x)
	ll ans = (power(a,n) * x) % MOD;
	ans = (ans + ((((power(a,n) - 1 + MOD) % MOD) * power(a-1,MOD-2)) % MOD * b) % MOD ) % MOD;
	plld(ans)
	return 0;

}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//input definitions
#define sd(x) scanf("%d",&x);
#define slld(x) scanf("%lld",&x);
#define ss(x) scanf("%s",x);
#define sf(x) scanf("%f",&x);
#define slf(x) scanf("%lf",&x);

//output definitions
#define pd(x) printf("%d",x);
#define plld(x) printf("%lld",x);
#define ps(x) printf("%s",x);
#define pf(x) printf("%f",x);
#define plf(x) printf("%lf",x);
#define pnl printf("\n");
#define psp printf(" ");

//define datatypes
typedef long long int ll;

//loops
#define upto(x) for(;i<=x;i++)
#define fl(i,a,b,c) for(i=a;i<=b;i+=c)
#define wl(x) while(x--)

#define MOD 1000000007

ll power(ll a, ll b){
	ll ans = 1;
	while(b > 0){
		if(b & 1){
			ans = (ans * a) % MOD;
		}
		a = (a*a) % MOD;
		b/=2;
	}
	return ans%MOD;
}

int main(void) {
	ll a,b,c,d,n,x;
	slld(a) slld(b) slld(n) slld(x)
	if(n == 1){
		plld(((a*x)%MOD + b) % MOD)
	}
	else{
		ll ans = (power(a,n) * x) % MOD;
		ans = (ans + (((((power(a,n) - 1 + MOD) % MOD) * power(a-1,MOD-2)) % MOD) * b) % MOD ) % MOD;
		plld(ans)
	}
	return 0;

}

#include<stdio.h>
#include<string.h>
#include<math.h>
typedef unsigned long long LL;
LL m=1000000007;

LL bigmod(LL a,LL b, LL m)
{
    if(b==0)    return 1%m;
    LL x=bigmod(a,b/2,m);
    x=(x*x)%m;
    if(b%2==1)
        x=(x*a)%m;
    return x;
}
int main()
{
    LL n, a, b, x, f, ans, f2, f3;
    char  s[200010];
    scanf("%llu %llu %llu %llu",&a,&b,&n,&x);
    f = bigmod(a, n, m);
    f2 = (f * (x % m)) % m;
    if(a==1)
    {
        ans = (f2 + (b* (n%m) )% m) % m;
    }
    else
    {
        f3 = bigmod(a-1, m-2, m);
        ans = (f2+(b*((f-1)*f3)%m)%m)%m;
    }

    printf("%llu\n",ans);
    return 0;

}

#include <stdio.h>
typedef long long ll;
ll MOD = 1000000007 ;
ll MOD2 = 1000000006 ;

ll gcdExtended(ll a, ll b, ll *x, ll *y);

ll power(ll x, ll y, ll p) ;

// Function to find modulo inverse of a
ll modInverse(ll a, ll m)
{
    ll x, y;
    ll g = gcdExtended(a, m, &x, &y);

        // m is added to handle negative x
        ll res = (x%m + m) % m;
        return res ;

}

// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
    // Base Case
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1; // To store results of recursive call
    ll gcd = gcdExtended(b%a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

ll main(ll argc, char const *argv[])
{
    ll A,B,n,x;
    scanf("%lld %lld %lld %lld",&A,&B,&n,&x);
    ll answer = 0;
    ll apart = 1;
    ll itna = n%(MOD2);
    if(itna==0)
    {
        printf("%lld\n",x%MOD);
    }
    else
    {
    ll second = 1;
    ll dot = power(A,n,MOD);
    answer += (dot*x%MOD)%MOD ;
    ll ert  = dot-1 ;
    ll gh = A-1 ;
    if(A!=1)
    {
    answer += (((modInverse(A-1,MOD)*ert)%MOD)*B)%MOD;
    }
    else
    {
        answer += (B*n)%MOD ;
    }
    //prllf("%lld\n",);
    printf("%lld\n",answer);
    }
    return 0;
}

#include <stdio.h>
typedef long long ll;
ll MOD = 1000000007 ;
ll MOD2 = 1000000006 ;

ll gcdExtended(ll a, ll b, ll *x, ll *y);

ll power(ll x, ll y, ll p) ;

// Function to find modulo inverse of a
ll modInverse(ll a, ll m)
{
    ll x, y;
    ll g = gcdExtended(a, m, &x, &y);

        // m is added to handle negative x
        ll res = (x%m + m) % m;
        return res ;

}

// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
    // Base Case
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1; // To store results of recursive call
    ll gcd = gcdExtended(b%a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;
        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

ll main(ll argc, char const *argv[])
{
    ll A,B,n,x;
    scanf("%lld %lld %lld %lld",&A,&B,&n,&x);
    ll answer = 0;
    ll apart = 1;
    ll itna = n%(MOD2);
    if(itna==0)
    {
        printf("%lld\n",x%MOD);
    }
    else
    {
    ll second = 1;
    ll dot = power(A,itna,MOD);
    answer = (answer +((dot*x) % MOD))%MOD ;
    ll ert  = dot-1 ;
    ll gh = A-1 ;
    if(A!=1)
    {
    answer = (answer+(((modInverse(A-1,MOD)*ert)%MOD)*B)%MOD)%MOD;
    }
    else
    {
    //    printf("yele %lld\n\n",itna);
        answer = (answer + ((B*n)%MOD))%MOD ;
    }
    //prllf("%lld\n",);
    printf("%lld\n",answer);
    }
    return 0;
}

#include <stdio.h>
typedef long long ll;
ll MOD = 1000000007 ;
ll MOD2 = 1000000006 ;

ll gcdExtended(ll a, ll b, ll *x, ll *y);

ll power(ll x, ll y, ll p) ;

// Function to find modulo inverse of a
ll modInverse(ll a, ll m)
{
    ll x, y;
    ll g = gcdExtended(a, m, &x, &y);

        // m is added to handle negative x
        ll res = (x%m + m) % m;
        return res ;

}

// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
    // Base Case
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1; // To store results of recursive call
    ll gcd = gcdExtended(b%a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;
        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

ll main(ll argc, char const *argv[])
{
    ll A,B,n,x;
    scanf("%lld %lld %lld %lld",&A,&B,&n,&x);
    ll answer = 0;
    ll apart = 1;
    ll itna = n%(MOD2);
    if(itna==0)
    {
        printf("%lld\n",x%MOD);
    }
    else
    {
    ll second = 1;
    ll dot = power(A,itna,MOD);
    answer += ((dot*x) % MOD) ;
    ll ert  = dot-1 ;
    ll gh = A-1 ;
    if(A!=1)
    {
    answer = (answer+(((modInverse(A-1,MOD)*ert)%MOD)*B))%MOD;
    }
    else
    {
        answer = (answer + (B*itna))%MOD ;
    }
    //prllf("%lld\n",);
    printf("%lld\n",answer);
    }
    return 0;
}

#include <stdio.h>
typedef long long ll;
ll MOD = 1000000007 ;
ll MOD2 = 1000000006 ;

ll gcdExtended(ll a, ll b, ll *x, ll *y);

ll power(ll x, ll y, ll p) ;

// Function to find modulo inverse of a
ll modInverse(ll a, ll m)
{
    ll x, y;
    ll g = gcdExtended(a, m, &x, &y);

        // m is added to handle negative x
        ll res = (x%m + m) % m;
        return res ;

}

// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
    // Base Case
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1; // To store results of recursive call
    ll gcd = gcdExtended(b%a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

ll main(ll argc, char const *argv[])
{
    ll A,B,n,x;
    scanf("%lld %lld %lld %lld",&A,&B,&n,&x);
    ll answer = 0;
    ll apart = 1;
    ll itna = n%(MOD2);
    if(itna==0)
    {
        printf("%lld\n",x%MOD);
    }
    else
    {
    ll second = 1;
    ll dot = power(A,itna,MOD);
    answer += (dot*x%MOD)%MOD ;
    ll ert  = dot-1 ;
    ll gh = A-1 ;
    if(A!=1)
    {
    answer += (((modInverse(A-1,MOD)*ert)%MOD)*B)%MOD;
    }
    else
    {
        answer += (B*n)%MOD ;
    }
    //prllf("%lld\n",);
    printf("%lld\n",answer);
    }
    return 0;
}

#include <stdio.h>
typedef long long ll;
ll MOD = 1000000007 ;
ll MOD2 = 1000000006 ;

ll gcdExtended(ll a, ll b, ll *x, ll *y);

ll power(ll x, ll y, ll p) ;

// Function to find modulo inverse of a
ll modInverse(ll a, ll m)
{
    ll x, y;
    ll g = gcdExtended(a, m, &x, &y);

        // m is added to handle negative x
        ll res = (x%m + m) % m;
        return res ;

}

// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
    // Base Case
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1; // To store results of recursive call
    ll gcd = gcdExtended(b%a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;
        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

ll main(ll argc, char const *argv[])
{
    ll A,B,n,x;
    scanf("%lld %lld %lld %lld",&A,&B,&n,&x);
    ll answer = 0;
    ll apart = 1;
    ll itna = n%(MOD2);
    if(itna==0)
    {
        printf("%lld\n",x%MOD);
    }
    else
    {
    ll second = 1;
    ll dot = power(A,itna,MOD);
    answer += (dot*x%MOD)%MOD ;
    ll ert  = dot-1 ;
    ll gh = A-1 ;
    if(A!=1)
    {
    answer += (((modInverse(A-1,MOD)*ert)%MOD)*B)%MOD;
    }
    else
    {
        answer += (B*itna)%MOD ;
    }
    //prllf("%lld\n",);
    printf("%lld\n",answer);
    }
    return 0;
}

#include <stdio.h>
typedef long long ll;
ll MOD = 1000000007 ;
ll MOD2 = 1000000006 ;

ll gcdExtended(ll a, ll b, ll *x, ll *y);

ll power(ll x, ll y, ll p) ;

// Function to find modulo inverse of a
ll modInverse(ll a, ll m)
{
    ll x, y;
    ll g = gcdExtended(a, m, &x, &y);

        // m is added to handle negative x
        ll res = (x%m + m) % m;
        return res ;

}

// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
    // Base Case
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1; // To store results of recursive call
    ll gcd = gcdExtended(b%a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;
        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

ll main(ll argc, char const *argv[])
{
    ll A,B,n,x;
    scanf("%lld %lld %lld %lld",&A,&B,&n,&x);
    ll answer = 0;
    ll apart = 1;
    ll itna = n%(MOD2);
    if(itna==0)
    {
        printf("%lld\n",x%MOD);
    }
    else
    {
    ll second = 1;
    ll dot = power(A,itna,MOD);
    answer = (answer +((dot*x) % MOD))%MOD ;
    ll ert  = dot-1 ;
    ll gh = A-1 ;
    if(A!=1)
    {
    answer = (answer+(((modInverse(A-1,MOD)*ert)%MOD)*B))%MOD;
    }
    else
    {
        answer = (answer + (B*itna))%MOD ;
    }
    //prllf("%lld\n",);
    printf("%lld\n",answer);
    }
    return 0;
}

long long int modular_pow(long long int base, long long int exponent, long long int modulus)
{
    long long int result = 1;
    while (exponent > 0)
    {
        if (exponent % 2 == 1)
            result = (result * base) % modulus;
        exponent = exponent >> 1;
        base = (base * base) % modulus;
    }
    return result;
}
int powercal(int x, unsigned int y)
{
    int res = 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = res*x;

        // n must be even now
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}
int main ()
{
    long long int  n,a,b,x,ans,e12,ef,e,e1,e2,pr,en;
    long long int p=1000000007;
    scanf ("%I64d %I64d %I64d %I64d",&a,&b,&n,&x);
    //printf ("%I64d\n",(-1)%3);
    if (a!=1)
    {
        e1=b%p;
        pr=modular_pow(a,n,p);//(a^n)%p
        e2=(pr+(p-1))%p;
        printf ("%I64d ",e2);// (a^n-1) %p
        en=modular_pow(a-1,p-2,p);
        e2=((e2%p)*en)%p;
        e12=(e1*e2)%p;
        e=((x%p)*pr)%p;
        ef=(e12+e)%p;
        printf ("%I64d",ef);
        return 0;
    }
    else
    {
        e=((n%p)*(b%p))%p;
        ef=(e+(x%p))%p;
      printf ("%I64d",ef);
      return 0;
    }
}

long long int modular_pow(long long int base, long long int exponent, long long int modulus)
{
    long long int result = 1;
    while (exponent > 0)
    {
        if (exponent % 2 == 1)
            result = (result * base) % modulus;
        exponent = exponent >> 1;
        base = (base * base) % modulus;
    }
    return result;
}
int powercal(int x, unsigned int y)
{
    int res = 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = res*x;

        // n must be even now
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}
int main ()
{
    long long int  n,a,b,x,ans,e12,ef,e,e1,e2,pr,an,en;
    long long int p=1000000007;
    scanf ("%I64d %I64d %I64d %I64d",&a,&b,&n,&x);
    //printf ("%I64d\n",(-1)%3);
    if (a!=1)
    {
        an=powercal(a,n);
        en=(an-1)/(a-1);
        e1=((b%p) * (en%p))%p;
        pr=modular_pow(a,n,p);
        e2=(pr*(x%p)) %p;
        ef=(e1+e2)%p;
        printf ("%I64d",ef);
        return 0;
    }
    else
    {
        e=((n%p)*(b%p))%p;
        ef=(e+(x%p))%p;
      printf ("%I64d",ef);
      return 0;
    }
}

long long int modular_pow(long long int base, long long int exponent, long long int modulus)
{
    long long int result = 1;
    while (exponent > 0)
    {
        if (exponent % 2 == 1)
            result = (result * base) % modulus;
        exponent = exponent >> 1;
        base = (base * base) % modulus;
    }
    return result;
}
int main ()
{
    long long int  n,a,b,x,ans,e12,ef,e,e1,e2,pr;
    long long int p=1000000007;
    scanf ("%I64d %I64d %I64d %I64d",&a,&b,&n,&x);
    //printf ("%I64d\n",(-1)%3);
    if (a!=1)
    {
        e1=(b/(a-1))%p;
        pr=modular_pow(a,n,p);
        e2=(pr+(p-1))%p;
        e12=(e1*e2)%p;
        e=((x%p)*pr)%p;
        ef=(e12+e)%p;
        printf ("%I64d",ef);
        return 0;
    }
    else
    {
        e=((n%p)*(b%p))%p;
        ef=(e+(x%p))%p;
      printf ("%I64d",ef);
      return 0;
    }
}