Maximize The Beautiful Value
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t, n, k, a[ 100005 ] ;
ll ans, sum[ 100005 ] , result= - 1e15 , num;
int main ( ) {
scanf ( "%lld" , & t) ;
while ( t-- ) {
memset ( a, 0 , sizeof ( a) ) ;
memset ( sum, 0 , sizeof ( sum) ) ;
num= 0 , ans= 0 , result= - 1e15 ;
scanf ( "%lld %lld" , & n, & k) ;
for ( ll i= 1 ; i<= n; i++ ) {
scanf ( "%lld" , & a[ i] ) ;
sum[ i] = sum[ i- 1 ] + a[ i] ;
num+ = a[ i] * i;
}
for ( ll i= k+ 1 ; i<= n; i++ ) {
ll x= a[ i] * k;
ll y= sum[ i- 1 ] - sum[ i- k- 1 ] ;
ans= y- x;
result= max ( result, ans) ;
}
printf ( "%lld\n" , num+ result) ;
}
return 0 ;
}
幸运数字Ⅱ
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll l, r;
const int N= 1e5 + 5 ;
ll a[ N] , cnt= 0 ;
void dfs ( ll x) {
if ( x> 1e9 ) return ;
a[ cnt++ ] = x;
dfs ( x* 10 + 4 ) ;
dfs ( x* 10 + 7 ) ;
}
int main ( ) {
dfs ( 4 ) ;
dfs ( 7 ) ;
a[ cnt] = 4444444444 ;
sort ( a, a+ cnt+ 1 ) ;
scanf ( "%lld %lld" , & l, & r) ;
int p1= lower_bound ( a, a+ cnt+ 1 , l) - a, p2= lower_bound ( a, a+ cnt+ 1 , r) - a;
ll ans= 0 ;
for ( int i= p1; i<= p2; i++ ) {
ans+ = ( min ( a[ i] , r) - l+ 1 ) * a[ i] ;
l= a[ i] + 1 ;
}
cout<< ans<< endl;
return 0 ;
}